When troubleshooting a system or application experiencing a slowdown or bad behavior, one of the first things to check is system memory usage.
This article explains how to check RAM usage on Linux using several different commands.
free is the command most often used to check Linux system memory usage. It displays information about the total, used, and free up memory.
In general, free is called with the -h option which means printing output in a human-readable format:
total used free shared buff/cache available Mem: 3936 1087 252 130 2596 2427 Swap: 0 0 0
Here’s what each column means:
- total – The total amount of memory the application can use.
- used – Memory used. This is calculated as: used = total – free – buffer – cache
- free – Free memory / Not used.
- shared – This column can be ignored; it is only displayed for backward compatibility.
- buff / cache – Combined memory used by the kernel buffer and the page and slab cache. This memory can be reclaimed at any time if needed by the application.
- available – Estimated memory available for starting a new application, without exchanging.
Free commands print information for physical memory and swap system.
top is a command-line utility that displays real-time information about the currently running process. It also shows a system summary, including memory usage.
To run the command just type above:
The output will look like this:
The output header includes information about total, free, and used physical and system memory swaps.
The% MEM column provides information about the part that is used from the physical memory available for each running process.
/ proc / meminfo
The simplest way to check RAM memory usage is to display the contents of the virtual / proc / meminfo file. This file is used by free, top, ps, and other system information commands.
Use less or paint to view the contents of the / proc / meminfo file:
This file includes a large amount of information about system memory and swap usage:
MemTotal: 4030592 kB MemFree: 401804 kB MemAvailable: 2507504 kB ...
Information from the / proc / meminfo file can be broken down and used in shell scripts.
ps_mem is a Python script that reports RAM memory usage per program. It works in Python 2 and 3 and can be installed with pip:
sudo pip3 install ps_mem
This script requires administrator rights. To launch it, type sudo ps_mem in your terminal:
The output will include memory usage of each program that runs 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 know which programs are taking up the most of your system’s memory.
We have shown you several commands that you can use to check system memory usage.
If you have questions or comments, please leave a comment below.