If your Linux server or workstation seems to be suffering from a dip in performance then a good starting point when troubleshooting is to check the current memory usage. With that in mind, this article will explore some of the ways you can check RAM usage on Linux to help you diagnose what may be causing your Linux system to slow down.
Many of the commands used to check Linux free memory shown here will already be present on your Linux system, which is convenient!
View Memory Usage with /proc/meminfo
Proc
is a virtual filesystem on Linux which contains runtime system information. Many Linux system utilities are calls to files in this directory in order to retrieve and display information about the system. The meminfo
file contains details about the memory usage of the Linux system:
$ cat /proc/meminfo
MemTotal: 1992964 kB
MemFree: 93032 kB
MemAvailable: 1137284 kB
Buffers: 52144 kB
Cached: 1113000 kB
SwapCached: 0 kB
Active: 505572 kB
Inactive: 1202796 kB
Active(anon): 1172 kB
Inactive(anon): 562036 kB
Active(file): 504400 kB
Inactive(file): 640760 kB
Unevictable: 23008 kB
Mlocked: 18472 kB
SwapTotal: 0 kB
SwapFree: 0 kB
This is a quick and simple way to check memory usage and should work on any distribution. Rather than list the whole file contents you can use grep to filter the output. For example, to view only the total memory on the system, run the following:
$ cat /proc/meminfo | grep MemTotal
MemTotal: 1992964 kB
As this is text output there are many ways in which this data can be retrieved using scripts:
$ awk '{ printf "%.2f", $2/1024/1024 ; exit}' /proc/meminfo
1.90
In the example above, awk
has been used to parse the output of /proc/meminfo
and convert the memtotal value into Gb.
Check Linux Memory Usage using Free
The free
command is another convenient way to view Linux memory usage details. Running it without any arguments will output the following:
$ free
total used free shared buff/cache available
Mem: 1992964 666712 179912 9628 1146340 1147644
Swap: 0 0 0
Running it again, but with the -h
option produces a more readable result, with the memory unit displayed:
$ free -h
total used free shared buff/cache available
Mem: 1.9Gi 651Mi 175Mi 9.0Mi 1.1Gi 1.1Gi
Swap: 0B 0B 0B
The following information is displayed:
total | Total installed memory (Physical Memory installed on the system) |
used | Memory currently in use |
free | Unused memory on the system |
shared | Memory shared by multiple processes on the system |
buffers | Memory reserved by the OS to allocate as buffers when process need them |
cached | Recently used files cached in RAM |
buff/cache | Buffers + Cache |
available | How much memory is available, without swapping. |
A great thing about the free command is that it can output memory usage statistics using different memory units. The help option shows the numerous memory units available:
Options:
-b, --bytes show output in bytes
--kilo show output in kilobytes
--mega show output in megabytes
--giga show output in gigabytes
--tera show output in terabytes
--peta show output in petabytes
-k, --kibi show output in kibibytes
-m, --mebi show output in mebibytes
-g, --gibi show output in gibibytes
--tebi show output in tebibytes
--pebi show output in pebibytes
-h, --human show human-readable output
--si use powers of 1000 not 1024
-l, --lohi show detailed low and high memory statistics
-t, --total show total for RAM + swap
-s N, --seconds N repeat printing every N seconds
-c N, --count N repeat printing N times, then exit
-w, --wide wide output
--help display this help and exit
-V, --version output version information and exit
To output Linux memory usage in gigabytes the following can be used:
$ free --giga
total used free shared buff/cache available
Mem: 2 0 0 0 0 1
Swap: 0 0 0
Now, lets take a look at how to use the top
command to look at memory usage.
View Memory usage using the Top Command
The top command is a well known Linux command for viewing real time Linux system information, including memory usage. To run top
:
$ top
The output should look similar to the following:
top - 13:31:56 up 40 min, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 200 total, 1 running, 199 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.2 sy, 0.0 ni, 99.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 1946.3 total, 396.6 free, 644.6 used, 905.1 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 1124.2 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 103420 13200 8512 S 0.0 0.7 0:06.29 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_par_gp
The section at the top of the output displays overall system memory usage:
MiB Mem : 1946.3 total, 396.6 free, 644.6 used, 905.1 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 1124.2 avail Mem
You can also see the memory usage of individual processes by checking out the %MEM
column. If you have worked with VMware ESXi hosts you will notice this is similar to esxtop.
Summary
In this article you have learned a number of ways to check memory usage on Linux, including how to use the top and free commands which tend to be present on all Linux distributions, and how to list the contents of the /proc/meminfo file to get detailed memory usage statistics.