Essential Commands to Monitor Memory Usage in Linux
Monitoring memory usage is crucial for maintaining the health and performance of a Linux system. Whether you're a system administrator or a casual user, knowing how to check memory usage can help you troubleshoot issues and optimize your system. This article covers the essential commands to monitor memory usage in Linux.
1. 'free'
The free
command provides a quick overview of the system’s memory usage. It displays the total, used, free, shared, buffer/cache, and available memory:
free -h
The -h
option makes the output human-readable, showing sizes in KB, MB, or GB.
2. 'vmstat'
The vmstat
(virtual memory statistics) command reports information about processes, memory, paging, block IO, traps, and CPU activity:
vmstat 5
This command updates the output every 5 seconds, providing a continuous view of memory and CPU usage.
3. 'top'
The top
command provides a dynamic, real-time view of the system’s running processes, including memory and CPU usage:
top
Within the top
interface, you can sort processes by memory usage by pressing M
.
4. 'htop'
htop
is an enhanced version of top
with a more user-friendly interface. It provides color-coded memory and CPU usage metrics:
htop
If not installed, you can usually add it using your package manager (e.g., sudo apt install htop
on Debian-based systems).
5. 'ps'
The ps
(process status) command can be used to display information about active processes. To sort processes by memory usage:
ps aux --sort=-%mem
This command lists all processes, sorted by the percentage of memory usage in descending order.
6. 'smem'
smem
is a command-line tool that provides a more accurate representation of memory usage per process by calculating proportional set size (PSS):
smem -r -k
If not installed, you can usually add it using your package manager (e.g., sudo apt install smem
on Debian-based systems).
7. '/proc/meminfo'
The /proc/meminfo
file contains detailed information about the system's memory usage. You can view its contents using:
cat /proc/meminfo
This file provides comprehensive data about various aspects of memory usage, including total memory, free memory, available memory, and more.
8. 'glances'
glances
is a cross-platform monitoring tool that provides a real-time overview of system statistics, including memory usage:
glances
If not installed, you can add it using your package manager (e.g., sudo apt install glances
on Debian-based systems).
9. 'dstat'
The dstat
command combines the functionality of various system monitoring tools, providing detailed statistics on CPU, memory, disk, and network usage:
dstat --top-mem
If not installed, you can add it using your package manager (e.g., sudo apt install dstat
on Debian-based systems).
10. 'sar'
The sar
(System Activity Reporter) command collects, reports, and saves system activity information. To display memory usage statistics:
sar -r 1 3
This command reports memory usage every second, three times. If not installed, you can add it using your package manager (e.g., sudo apt install sysstat
on Debian-based systems).
Conclusion
Monitoring memory usage is essential for maintaining the performance and stability of your Linux system. By mastering these commands, you can gain valuable insights into how your system uses memory, identify potential issues, and optimize resource utilization. Whether you prefer simple commands like free
and top
or more advanced tools like htop
and smem
, Linux offers a variety of options to suit your monitoring needs.