For Linux users and system administrators, it is crucial to check their memory usage through the command-line interface. There are several commands that you can use to get information on physical memory, buffer, and swap memory usage in Linux.
Determining memory usage is a skill every Linux user needs to keep a check on their systems. There are various tools available in Linux for this purpose, and these commands do not require expertise to use them. Furthermore, the commands work with several flags and give a brief or detailed overview of the memory as required by the Linux user.
In this tutorial, we will take a deep dive into five different ways of checking memory usage in Linux.
For a system administrator, it is crucial to know about the critical occupation of memory. The list is shown below:
- RAM – Occupation percentage greater than 80% is critical.
- SWAP – Occupation percentage greater than 70% is critical.
1. Free command to Check Memory Usage
The first command you can use to check memory usage is the
free command. It shows the information about the total memory, as well as used and free memory. In addition, the free command is used with the
-h flag to print out the information in a human-readable format.
As a result, you will get an output consisting of several columns. Let’s look at each column in detail:
- total – The total memory available in the Linux system.
- used – Used memory by the applications installed in the system.
- free – The remaining memory in the system.
- shared – This column shows backward compatibility.
- buff/cache – The memory used by the kernel buffer and cache.
- available – The estimated memory available that can be used by the new applications.
Additionally, you can also use the free command with the
-m flag to view the total memory in the MB format.
In addition, here is a list of flags that you can use in the free command to modify the output.
- -h: human readable output.
- -b,-k,-m,-g: display output in bytes, KB, MB, or GB.
- -o: displays output in the old format (no -/+buffers/cache line).
- -s : memory update every [delay] seconds.
2. Top Command
Secondly, you can also use the
top command to view the free memory in Linux. It is a command-line utility to show real-time information about the usage of the processes.
To run the command, type “top” and press the “Enter” key.
As a result, the output will look something like this:
This command displays the following list of information:
- average load
- tasks running
- number of users logged in
- number of CPUs/CPU utilization
- memory/swap system processes
3. Atop Command
Similar to the
top command, you can also use the
atop command. It is a command-line utility to monitor the system resources in Linux systems. Firstly, you will have to install the command as shown below:
sudo apt install atop
After that, type the command to view the Linux resource information.
You will get the following output as a result of the command execution:
4. /proc/meminfo File to Check Memory Usage
Another way to check memory usage is to view the contents of the
/proc/meminfo file. This file is also accessed by the other system commands like “free” and “top” to view the memory usage.
Furthermore, you can use the cat command to view the contents of this file.
The output would look something like this:
5. ps_mem Script to Check Memory Usage
Apart from the system commands and system files, you can also run a Python script to check the memory usage. Previously, we have covered how to set up Python 3 in Linux systems. You can use it as a guide to run the
ps_mem is a Python script that shows per-program RAM usage. It works with both versions of Python. Furthermore, you can also use the pip command to install the script directly.
sudo pip3 install ps_mem
However, make sure to use the keyword sudo with the pip command as it requires root privileges.
As a result of execution, the output will show the memory usage in ascending order:
Private + Shared = RAM used Program 11.9 MiB + 20.2 MiB = 32.1 MiB nginx (4) 8.2 MiB + 42.4 MiB = 50.6 MiB systemd-journald 55.8 MiB + 307.2 MiB = 363.0 MiB php-fpm7.4 (6) 233.9 MiB + 234.0 MiB = 467.9 MiB redis-server 578.2 MiB + 578.6 MiB = 1.1 GiB mysqld --------------------------------- 2.2 GiB =================================
6. Vmstat Command
The third command that you can use is the
This command will report on the processes, memory, paging, block IO, traps, disks, and CPU as shown in the image below:
The output is not in the human-readable format but gives the following information:
- r: number of processes in run time.
- b: number of processes in sleep.
- swpd: virtual memory used.
- free: idle memory.
- buff: memory used as buffers.
- cache: memory used as cache.
- si: memory swapped in/from disk.
- so: memory swapped to disk.
- bi: Blocks received from a device (blocks/s).
- bo: Blocks sent to a device (blocks/s).
- in: number of interrupts per second.
- cs: number of context switches per second.
- CPU – This column shows the CPU time in percentage.
- us: Time spent on non-kernel code. (user time, including nice time)
- sy: Time spent on kernel code. (system time)
- id: Time spent idle.
- wa: Time spent waiting for IO.
- st: Time that was stolen from a virtual machine.
To view the output in the single-column use the
-s flag with the
vmstat command. This will display the output in a more organized format.
sudo vmstat -s
Furthermore, it also gives detailed information such as ticks, pages, swaps, interrupts, context switches, boot time, and forks.
In conclusion, you have learned about various Linux commands, files, and scripts in this article. Furthermore, you can use it to check Linux memory usage. The commands can also be viewed in the human-readable format using various flags.
To get more information on these commands, simply type the
man command with
If you have any questions or remarks, please leave a comment below.