Site icon DesignLinux

How to Check Memory Usage in Linux

When troubleshooting system or application slowdown or misbehavior, one of the first things to check is the system memory usage.

This article explains how to check RAM usage in Linux using several different commands.

free Command

free is the most commonly used command for checking the memory usage of a Linux system. It displays information about the total, used, and free memory.

Generally, free is invoked with the -h option that means print the output in human-readable format:

free -h
              total        used        free      shared  buff/cache   available
Mem:           3936        1087         252         130        2596        2427
Swap:             0           0           0

Here’s what each column mean:

The free command prints information for the physical memory and the system swap .

top Command

top is a command-line utility that displays real-time information about the running processes. It also shows the system summary, including memory usage.

To invoke the command simply type top:

free -h

The output will look something like this:

The header of the output includes information about the system’s total, free, and used physical and swap memory.

The %MEM column provides information about the used share of the available physical memory for each running process.

/proc/meminfo

The simplest way to check the RAM memory usage is to display the contents of the /proc/meminfo virtual file. This file is used by the free, top, ps , and other system information commands.

Use less or cat to view the contents of the /proc/meminfo file:

cat /proc/meminfo

The file includes a large amount of information about the systems memory and swap usage:

MemTotal:        4030592 kB
MemFree:          401804 kB
MemAvailable:    2507504 kB
...

The information from the /proc/meminfo file can be parsed and used in shell scripts.

ps_mem Script

ps_mem is a Python script that reports per-program RAM memory usage. It work withs both Python 2 and 3 and can be installed with pip:

sudo pip3 install ps_mem

This script requires administrator privileges. To launch it, type sudo ps_mem in your terminal:

sudo ps_mem

The output will include the memory usage of each running program 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
=================================

This script is useful when you want to find out which running program is taking most of your system memory.

Conclusion

We have shown you several commands that you can use to check the system memory usage.

If you have any questions or remarks, please leave a comment below.

terminal
Exit mobile version