How to Check Memory Usage in Linux

By 

•

Updated on

•

8 min read

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 and swap usage in Linux, and how to find the processes behind it.

Quick Reference

CommandDescription
free -hShow memory usage in human-readable format
free -mShow memory usage in mebibytes
free -gShow memory usage in gibibytes
free -s 5Refresh memory stats every 5 seconds
ps aux --sort=-%memList processes by memory use, heaviest first
cat /proc/meminfoShow detailed memory information
sudo ~/.local/bin/ps_memShow per-program RAM usage after a pipx install

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.

The most common flags are -h (human-readable), -m (mebibytes), and -g (gibibytes). All of them count in powers of 1024, so add --si when you want powers of 1000 instead.

To print memory usage in human-readable format:

Terminal
free -h
output
               total        used        free      shared  buff/cache   available
Mem:           7.8Gi       4.3Gi       289Mi       412Mi       3.6Gi       3.4Gi
Swap:          2.0Gi       256Mi       1.8Gi

To show memory in mebibytes:

Terminal
free -m
output
               total        used        free      shared  buff/cache   available
Mem:            7937        4435         289         412        3688        3502
Swap:           2048         256        1792

To show memory in gibibytes:

Terminal
free -g
output
               total        used        free      shared  buff/cache   available
Mem:               7           4           0           0           3           3
Swap:              2           0           1

Notice that the free column now reads 0 although the previous command reported 289 MiB. The -g option rounds down to whole gibibytes, so anything below 1 GiB disappears. Use free -m or free -h whenever the numbers are small.

Here is what each column means:

  • total - The total amount of memory that can be used by the applications.
  • used - Used or unavailable memory, calculated as total - available. Because available is an estimate that includes free memory and memory expected to be reclaimable, used, free, and buff/cache are not separate buckets.
  • free - Free / Unused memory.
  • shared - Memory used mostly by tmpfs filesystems and shared memory segments, reported as Shmem in /proc/meminfo.
  • buff/cache - The combined memory used by kernel buffers, the page cache, and reclaimable slabs. Much of this memory can be reclaimed when applications need it, but not all of it is immediately available.
  • available - An estimate of the memory that is available for starting new applications, without swapping.

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:

Terminal
top

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

The %MEM column shows the percentage of total physical memory occupied by each process’s resident memory. Press Shift+M while top is running to sort the process list by that column. If you prefer a colored, scrollable interface with mouse support, htop presents the same figures.

Finding the Processes Using the Most Memory

The system summary tells you that memory is tight, but not what is holding it. To get that answer without opening an interactive tool, sort ps output by the %MEM column and keep the first few lines:

Terminal
ps aux --sort=-%mem | head -n 6
output
USER       PID %CPU %MEM    VSZ   RSS TTY   STAT START   TIME COMMAND
mysql     1184  1.7 14.2 2317048 1155284 ?  Ssl  Aug28  92:41 /usr/sbin/mysqld
redis      902  0.6  5.9 1013264 479512 ?   Ssl  Aug28  33:12 /usr/bin/redis-server
www-data  2317  0.0  1.3 412880 108964 ?    S    09:14   0:02 php-fpm: pool www
root       771  0.0  0.6 1638420  52104 ?   Ssl  Aug28   4:55 /usr/bin/containerd
root         1  0.0  0.1 168404  11228 ?    Ss   Aug28   1:38 /sbin/init

The minus sign in --sort=-%mem reverses the order, so the heaviest process comes first. RSS is the resident set size in kibibytes, the part of the process that is actually in RAM, while VSZ counts everything the process has mapped, including memory it has never touched. Here mysqld holds 1.1 GiB of resident memory and 14.2 percent of the total, which makes it the first thing to look at.

Once you have a PID, the VmRSS entry in /proc/PID/status provides another estimate of its resident memory:

Terminal
grep VmRSS /proc/1184/status
output
VmRSS:	 1155284 kB

The kernel documents VmRSS as an approximate value, and it can differ slightly from ps if the process allocates or releases memory between reads.

One caveat: RSS counts shared libraries in full for every process that maps them, so adding the column up across a process list overstates the real total. When several copies of the same program are running, the ps_mem script covered below gives a better estimate.

/proc/meminfo

The free command and the system memory summary in top read /proc/meminfo. Process-level figures shown by ps, top, and ps_mem come from files under /proc/PID/. Reading /proc/meminfo directly gives you far more system-wide detail than free prints, at the cost of having to interpret the raw fields yourself.

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

Terminal
cat /proc/meminfo

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

output
MemTotal:        8127488 kB
MemFree:          295936 kB
MemAvailable:    3586048 kB
...

The values are in kibibytes and the format is plain text, which makes the file easy to parse in shell scripts. For the same numbers already labeled and summed, vmstat -s prints a report built from this file.

Keep in mind that MemTotal is the memory the kernel can hand out, which is a little less than the RAM fitted in the machine. When the question is about the hardware itself, how many modules are installed and how large each one is, lshw and dmidecode read that from the firmware.

ps_mem Script

ps_mem is a Python script that reports per-program RAM memory usage. It works with both Python 2 and 3. On Ubuntu 23.04 and later, install it with pipx to avoid conflicts with the system Python environment:

Terminal
pipx install ps_mem

On older releases that do not mark the system Python as externally managed, pip3 still works:

Terminal
sudo pip3 install ps_mem

For a full system report, run the pipx executable with administrator privileges. Use its full path because sudo usually does not include ~/.local/bin in its command search path:

Terminal
sudo ~/.local/bin/ps_mem

If you installed it system-wide with pip3, use sudo ps_mem instead.

The output will include the memory usage of each running program in ascending order:

output
 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-fpm8.3 (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.

Troubleshooting

Why does free -g show 0 GB free?
The -g option rounds values down to whole gibibytes. If less than 1 GiB is free, the column shows 0. Use free -m or free -h for more precise output.

sudo ps_mem returns “command not found”
pipx installs the script into ~/.local/bin, and sudo replaces your PATH with the secure_path value from /etc/sudoers, which usually does not include that directory. Call the script by its full path instead, with sudo ~/.local/bin/ps_mem.

Installed RAM does not match the total shown by Linux
A small portion of physical memory is reserved by the kernel, firmware, integrated graphics, or hardware devices. That is why the reported total can be slightly lower than the RAM installed in the system.

FAQ

What is the difference between free and available in free output?
free is the amount of completely unused memory. available is an estimate of how much memory can be used for new applications without swapping. It includes memory that is currently used for buffers and cache but can be reclaimed quickly.

Why does Linux show very little free memory?
Linux uses spare memory for disk caching to speed up file access. Much of the memory shown in buff/cache can be reclaimed when applications need it. The available column estimates how much memory can be used without swapping and gives a more useful picture than free alone.

How do I check the memory usage of a specific process?
Use ps with the --pid flag: ps -o pid,rss,comm -p PID. The rss field shows the resident set size in kibibytes. For a broader view of all processes, sudo ~/.local/bin/ps_mem groups them by program name when installed with pipx.

How do I check swap usage?
The free command shows total swap usage in the Swap: row. Use swapon --show to list each active swap file or device and its usage. For one process, run grep VmSwap /proc/PID/status; this value is approximate and does not include swapped shared memory.

Conclusion

The free command answers the overview question in one line, ps aux --sort=-%mem names the process behind it, and /proc/meminfo holds the detail you need when scripting against it.

Before you add RAM or a swap file because free reports almost nothing free, read the available column first. It accounts for memory the kernel expects it can reclaim without swapping.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page