How to Show Memory Usage in Linux
Understanding Memory Usage
Memory usage in Linux refers to the amount of memory allocated to running processes, applications, and system services. It’s essential to monitor memory usage to identify potential issues, optimize system performance, and ensure system stability. In this article, we’ll explore the various ways to show memory usage in Linux.
Method 1: Using top Command
The top command is a popular tool for monitoring system performance. You can use it to view memory usage in real-time.
- How to use
topto view memory usage:- Open a terminal and type
top. - Press
Ctrl + Cto interrupt the process. - The
topwindow will display the current system load, including memory usage. - To view memory usage in detail, use the following command:
top -b -n 1 -p <pid> -l memory- Replace
<pid>with the process ID of the process you want to view. -benables brief mode, which displays the process’s memory usage in kilobytes.-n 1limits the output to one line per process.-p <pid>specifies the process ID.-l memorydisplays the process’s memory usage in kilobytes.
- Replace
- Open a terminal and type
Method 2: Using htop Command
htop is a more advanced tool for monitoring system performance. It provides a more detailed view of memory usage.
- How to use
htopto view memory usage:- Open a terminal and type
htop. - Press
Ctrl + Cto interrupt the process. - The
htopwindow will display the current system load, including memory usage. - To view memory usage in detail, use the following command:
htop -b -n 1 -p <pid> -l memory- Replace
<pid>with the process ID of the process you want to view. -benables brief mode, which displays the process’s memory usage in kilobytes.-n 1limits the output to one line per process.-p <pid>specifies the process ID.-l memorydisplays the process’s memory usage in kilobytes.
- Replace
- Open a terminal and type
Method 3: Using ps Command
The ps command is used to view process information, including memory usage.
- How to use
psto view memory usage:- Open a terminal and type
ps -eo pid,ppid,cmd,mem,cpu,etimes - Press
Ctrl + Cto interrupt the process. - The
pswindow will display the current system load, including memory usage. - To view memory usage in detail, use the following command:
ps -eo pid,ppid,cmd,mem,cpu,etimes | grep <pid>- Replace
<pid>with the process ID of the process you want to view. - The
grepcommand searches for the specified process ID.
- Replace
- Open a terminal and type
Method 4: Using top -H Command
The top -H command is similar to top, but it displays memory usage in a more detailed format.
- How to use
top -Hto view memory usage:- Open a terminal and type
top -H - Press
Ctrl + Cto interrupt the process. - The
topwindow will display the current system load, including memory usage. - To view memory usage in detail, use the following command:
top -H -b -n 1 -p <pid> -l memory- Replace
<pid>with the process ID of the process you want to view. -benables brief mode, which displays the process’s memory usage in kilobytes.-n 1limits the output to one line per process.-p <pid>specifies the process ID.-l memorydisplays the process’s memory usage in kilobytes.
- Replace
- Open a terminal and type
Method 5: Using htop -H Command
htop -H is similar to htop, but it displays memory usage in a more detailed format.
- How to use
htop -Hto view memory usage:- Open a terminal and type
htop -H - Press
Ctrl + Cto interrupt the process. - The
htopwindow will display the current system load, including memory usage. - To view memory usage in detail, use the following command:
htop -H -b -n 1 -p <pid> -l memory- Replace
<pid>with the process ID of the process you want to view. -benables brief mode, which displays the process’s memory usage in kilobytes.-n 1limits the output to one line per process.-p <pid>specifies the process ID.-l memorydisplays the process’s memory usage in kilobytes.
- Replace
- Open a terminal and type
Method 6: Using ps -o mem Command
The ps -o mem command is used to view memory usage in kilobytes.
- How to use
ps -o memto view memory usage:- Open a terminal and type
ps -o mem - Press
Ctrl + Cto interrupt the process. - The
pswindow will display the current system load, including memory usage in kilobytes. - To view memory usage in detail, use the following command:
ps -o mem | grep <pid>- Replace
<pid>with the process ID of the process you want to view.
- Replace
- Open a terminal and type
Method 7: Using top -H -b -n 1 -p <pid> -l memory Command
The top -H -b -n 1 -p <pid> -l memory command is similar to top, but it displays memory usage in a more detailed format.
- How to use
top -H -b -n 1 -p <pid> -l memoryto view memory usage:- Open a terminal and type
top -H -b -n 1 -p <pid> -l memory - Press
Ctrl + Cto interrupt the process. - The
topwindow will display the current system load, including memory usage in kilobytes. - To view memory usage in detail, use the following command:
top -H -b -n 1 -p <pid> -l memory
- Open a terminal and type
Conclusion
Memory usage in Linux is an essential aspect of system performance and stability. By using the methods outlined in this article, you can easily view memory usage in real-time, including kilobytes, to identify potential issues and optimize system performance. Remember to use the top, htop, ps, and htop -H commands to view memory usage in detail, and use the ps -o mem command to view memory usage in kilobytes.
