Memory usage monitoring is among the critical tasks that Linux system administrators and any user who would want the system to run as smoothly as possible must consider. However, Linux is rich in in-built commands that analyze memory usage to identify problems and optimize performance. Five essential commands used to check memory usage in Linux have been highlighted in this article.

 

1. free Command

free is perhaps the most candid representation of memory usage. It lists totals, used, and available along with that part of swap space of the system.

Usage:

$ free -h

The -h flag formats these memory sizes in MBs and GBs so they can be more humanely readable. The details appearing here are:

  • Total Memory: Total amount of RAM present on the computer.
  • Used Memory: The current usage of the memory.
  • Free Memory: Amount of memory that is completely free.
  • Buffers/Cache: The amount of memory taken up by the system for caching purposes.

 

2. top Command

top is an interactive command that displays the system's real-time activity along with each running process's memory usage.

Usage:

top

The key sections in the top output are as follows:

  • %MEM: It is the percentage of physical memory a process is using.

  • RES: Resident Set size, the actual amount of memory a process is consuming.

Finally, you can press q to quit the top interface. For more user-friendly alternatives, you could run the htop command if it's installed.

 

3. vmstat Command

The vmstat or Virtual memory statistics command displays detailed information about the memory, CPU, and disk usage.

Usage:

vmstat 1 5

This command Renews the statistics every one second for five iterations. Important memory-related fields here are:

  • free: Available memory.
  • buff: indicate a portion of memory used for buffers.
  • cache: shows the amount of memory used for caching.

 

4. /proc/meminfo File

The /proc/meminfo virtual file reports information on the system memory usage in detail. It is quite useful for scripting or detailed analysis.

Usage:

cat /proc/meminfo

Search for fields such as:

  • MemTotal: Total physical memory.
  • MemFree: Available physical memory.
  • Buffers and Cached: Memory used for buffers and caches.

 

5. sar Command

The sar (System Activity Report) command is part of the sysstat package. It can be used to show historic memory usage. Therefore, it is well-suited to find out about memory usage trends over time.

Usage:

sar -r

Output will include:

  • kbmemfree: Free memory in kilobytes.
  • kbmemused: Used memory in kilobytes.
  • %memused: Percentage of memory in use.

To use sar, make sure the sysstat package is installed on your system.

 

Conclusion

Monitoring memory usage is one of the best ways to keep a Linux system healthy. The various ways reviewed in this paper include the free, top, vmstat, /proc/meminfo , and sar commands, among the best one can use to monitor and manage memory on their system. Regular usage of these commands will empower you to avoid performance-related issues and ensure that applications keep running smoothly. Start exploring these commands today to take full control of your system's memory management!