Memory usage in Linux refers to the amount of RAM that is being utilized by the system and its processes at a given time. It is important because it directly affects the performance of the system.
Monitoring memory usage allows administrators to identify and troubleshoot memory-related issues, such as identifying and closing memory leaks or identifying processes using excessive memory. It also helps to ensure that the system has enough memory to handle current and future workloads.
This post will list all the methods to check memory usage in Linux.
Approach#1: How to Check Memory Usage in Linux Using Command Line Tools?
Linux has very strong command-line support to perform various operations to monitor memory usage. Various command line utilities support checking memory usage in Linux.
This command displays the total amount of free and used physical memory and the amount of memory used by the system cache and buffers.
Default Usage of free Command
By default, the free command displays the memory usage in kilobytes, and it is executed in the following way.
The output can be described in the following way.
- The first section shows the total amount of memory installed on the system and the amount of currently used and available memory.
- The second section shows the amount of memory used by the system cache and buffers.
- The third section shows the amount of memory used by the system’s swap space.
Get the Output of free Command in Human Readable Format
To check memory usage in human-readable format; you can use the -h option, for example:
$ free -h
This shows the memory usage in bytes, kilobytes, megabytes, and gigabytes.
This command provides a real-time view of the processes running on the system, including their memory usage.
Open the terminal and write the following command.
Here are some of the key fields on the top command output related to memory usage:
Mem: This field shows the total amount of memory installed on the system, as well as the amount of memory that is currently used, available, and is used for buffers and cache.
SWAP: This field shows the amount of memory used for swap space.
RES: This field shows the amount of memory used by the process resident set size (RSS)
SHR: This field shows the amount of shared memory used by the process.
This is similar to the top command, but it provides more information in a more user-friendly format. It is an interactive process viewer for Linux, similar to the top. Still, it allows you to scroll vertically and horizontally, so you can see all the processes running on the system, along with their full command lines.
As seen above, the output is the same, but it is more interactive with a colorful interface.
This command provides information on system memory usage, including the amount of free memory, the amount of memory used by processes, and the amount of memory used by the system cache.
Default Usage of the vmstat Command
To use the vmstat command to Check Memory Usage in Linux, simply open a terminal and type the command “vmstat”, and press Enter.
From the output, the users can observe the memory usage by the system, by process, by system cache, and system’s swap space. All these usages are in kilobytes.
Detailed Output of vmstat Command
For more detailed information about memory, use the “-s” flag of the vmstat command as follows.
$ vmstat -s
This file contains detailed information about memory usage on the system, including the amount of total and available memory, the amount of memory used by the system cache and buffers, and the amount of memory used by each process.
To view the contents of this file, you can use the following command in a terminal window.
$ cat /proc/meminfo
This will display the total amount of memory, the amount currently in use, and other statistics about the system’s memory usage.
You can also use the command “grep” to search for specific information within the file, such as the amount of free memory, as demonstrated in the following command.
$ grep MemFree /proc/meminfo
Approach#2: How to Check Memory Usage in Linux Using GUI Tools?
One way to check memory usage in Linux is by using a GUI tool, “System Monitor,”. On Debian-based distributions, the System Monitor tool is found by default. However, it can be installed on other Linux distributions using the following command.
$ sudo snap install gnome-system-monitor
When you launch the GNOME-System-Monitor, you can check the memory usage in the following ways:
From the Processes Tab
By default, the “Processes” tab will be opened. In that tab, a column named “Memory” can be seen, containing the amount of memory consumed by the running processes.
From the Resource Tab
Now, click on the “Resources” tab from the header section. The following graphical interpretation is observed where the second graph represents the memory and swap space consumed in run time.
Note: The System Monitor does have a third tab named “File Systems”, but it does not influence the memory therefore, it is not discussed here.
Checking memory usage on Linux is the most prominent act of a system administrator. Memory usage can be checked using various command line utilities and a GUI tool. In this post, all the possible commands demonstrated that could be used to check memory usage. Similarly, an installation and usage of a GUI tool named “GNOME-System-Monitor” are also provided.