Linux comes with different set of commands to check memory usage. The free command displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel. The vmstat command reports information about processes, memory, paging, block IO, traps, and cpu activity. Finally, you can use the top or atop/htop commands which provides a dynamic real-time view of a running system. top and friends can display system summary information as well as a list of tasks currently being managed by the Linux kernel. Let us see various commands for Linux to check memory usage.
Linux check memory usage using /proc/meminfo file
The /proc/meminfo file stores statistics about memory usage on the Linux based system. The same file is used by free and other utilities to report the amount of free and used memory (both physical and swap) on the system as well as the shared memory and buffers used by the kernel.
MemTotal: 8120568 kB MemFree: 2298932 kB Cached: 1907240 kB SwapCached: 0 kB SwapTotal: 15859708 kB SwapFree: 15859708 kB
To display free memory size in MB (megabytes):
$ free -m
total used free shared buffers cached Mem: 750 625 125 0 35 335 -/+ buffers/cache: 254 496 Swap: 956 0 956
Displays a line containing the totals memory in MB:
$ free -t -m
total used free shared buff/cache available Mem: 31731 6905 13427 903 11398 23479 Swap: 122067 0 122067 Total: 153799 6905 135495
Understanding the free command outputs
The Mem columns contain the following information:
- Total : The total amount of RAM installed in my system. In this case 30Gi.
- Used : The total amount of RAM used. It is calculated as: Total – (free + buffers + cache)
- Free : The amount of unused or free memory for your apps.
- Shared : Amount of memory mostly used by the tmpfs file systems. In other words, Shmem in /proc/meminfo.
- Buff/cache : It is sum of buffers and cach. Buff is amount of memory used by Linux kernel for buffers. Cache is memory used by the page cache and slabs.
- Available : This is an estimation of how much memory is available for starting new applications on Linux system, without swapping.
The Swap columns contain the following information:
- Total : The total amount of the swap partition or file installed in my system. For example, 119Gi is the size of my swap space on Linux.
- Used : The total amount of swap sapced used.
- Free : The amount of unused or free swap space for Linux systems.
So how much free ram I have?
Type the following command:
$ free -m
A list of free command options
- -h : Human readable output. In other words, show all output fields automatically scaled to shortest three digit unit and display the units of print out. For example, B used for bytes, Ki means kibibyte, Mi is mebibyte, Gi is gibibyte, Ti means tebibyte, and Pi for pebibyte.
- -b,-k,-m,-g: display output in bytes, KB, MB, or GB.
- -l : show detailed low and high memory statistics.
- -o : use old format (no -/+buffers/cache line).
- -t : see total for RAM + swap usage on Linux.
- -s : update every [delay] seconds.
- -c : update [count] times.
Type the vmstat command at shell prompt:
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu---- r b swpd free buff cache si so bi bo in cs us sy id wa 1 0 0 131620 35432 341496 0 0 42 82 737 1364 15 3 81 1
Type top command at the shell prompt:
The program atop is an interactive monitor to view the load on a Linux system. This program can display the amount of used and free memory. It is similar to top command but comes with additional advanced options. By default, the atop command is not installed on most Linux distributions.
The program htop is an interactive process viewer. It is similar to top, but allows to scroll the list vertically and horizontally to see all processes and their full command lines.
By default, the htop command is not installed on most Linux distributions.
GNOME Desktop: GUI tool to see memory usage
The “Gnome System Monitor” application enables you to display basic system information and monitor system processes, usage of system resources, and file systems. You can also use System Monitor to modify the behavior of your system. You can start System Monitor by visiting System menu > Choose Administration > System Monitor option. Or type the following command at the shell prompt:
Check out related media
(Video 01: Top five commands to see used and free memory under Linux)
A note about the performance
- RAM – An occupation percentage of 90% is considered as critical.
- SWAP – An occupation percentage of 80% is considered as critical.
- To solve performance related problems, add more RAM and increase the swap space (or move swap space to another disk controller).
In this quick tutorial, you learned about various Linux commands that you can use to check the server or desk memory usage. Read man pages of free, vmstat, top, atop, and htop commands for more information by typing the following man command:
$ man free
$ man top
$ man vmstat
|Category||List of Unix and Linux commands|
|Firewall||CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04|
|Network Utilities||dig • host • ip • nmap|
|OpenVPN||CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04|
|Package Manager||apk • apt|
|Processes Management||bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time|
|Searching||grep • whereis • which|
|User Information||groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w|
|WireGuard VPN||CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04|