In this tutorial, you will going to learn how to check memory usage on Linux system. Sometimes system applications slow down or system takes a lot of time to load programs or services also in our day-to-day activities the system may misbehave. As a skilled system administrator, when you experience such technical problems the first thing you have to check is the memory usage.
There are different command line tools used to check memory usage on Linux. These are as follows;
- Using free command.
- Using vmstat command.
- Reading /proc/meminfo file.
- Using top command.
- Using htop command
How To Check Memory Usage on Linux
Check Memory Usage with free command
free
command displays the total amount of free and used physical and swap memory in the system, as well as the buffers and caches used by the kernel. The information is gathered by parsing /proc/meminfo
.
Syntax;
free [options]
When you run free
command in your terminal, you obtain the following output;
free
total used free shared buff/cache available
Mem: 8049908 2506972 2293160 695888 3249776 4542872
Swap: 2097148 0 2097148
According to the above output, you obtain the following columns;
total:
Total installed memory (MemTotal and SwapTotal in /proc/meminfo)used:
Used memory (calculated as total – free – buffers – cache)free:
Unused memory (MemFree and SwapFree in /proc/meminfo)shared:
Memory used (mostly) by tmpfs (Shmem in /proc/meminfo)buffers:
Memory used by kernel buffers (Buffers in /proc/meminfo)cache:
Memory used by the page cache and slabs (Cached and SReclaimable in /proc/meminfo)buff/cache:
Sum of buffers and cacheavailable:
Estimation of how much memory is available for starting new applications, without swapping. Unlike the data provided by the cache or free fields, this field takes into account page cache and also that not all reclaimable memory slabs will be reclaimed due to items being in use (MemAvailable in /proc/meminfo, available on kernels 3.14, emulated on kernels 2.6.27+, otherwise the same as free)
free
command has the following options;
Display the amount of memory in kilobytes
Use free --kilo
command to display the amount of memory in kilobytes.
free --kilo
total used free shared buff/cache available
Mem: 8243105 2663907 2249052 705523 3330146 4562198
Swap: 2147479 0 2147479
Display the amount of memory in megabytes
Use free –mega to display the amount of memory in megabytes.
free --mega
total used free shared buff/cache available
Mem: 8243 2675 2218 726 3348 4529
Swap: 2147 0 2147
Display the amount of memory in gigabytes
Use free –giga to display the amount of memory in gigabytes.
free --giga
total used free shared buff/cache available
Mem: 8 2 2 0 3 4
Swap: 2 0 2
Display the amount of memory in human-readable format
Use free -h
or free --human
to display the amount of memory in human-readable format.
free -h
total used free shared buff/cache available
Mem: 7.7Gi 2.5Gi 2.1Gi 691Mi 3.1Gi 4.2Gi
Swap: 2.0Gi 0B 2.0Gi
Display the amount of memory in monitoring mode
Use free -s <delay>
. It will continuously display the result delay
seconds apart. You may actually specify any floating point number for delay using either . or , for decimal point. usleep(3) is used for microsecond resolution delay times.
free -s 5
total used free shared buff/cache available
Mem: 8049908 2638032 2128708 715904 3283168 4391796
Swap: 2097148 0 2097148
total used free shared buff/cache available
Mem: 8049908 2607196 2163748 711612 3278964 4426924
Swap: 2097148 0 2097148
total used free shared buff/cache available
Mem: 8049908 2605328 2165652 711612 3278928 4428792
Swap: 2097148 0 2097148
total used free shared buff/cache available
Mem: 8049908 2605704 2165468 711420 3278736 4428608
Swap: 2097148 0 2097148
total used free shared buff/cache available
Mem: 8049908 2606068 2164904 711612 3278936 4428052
Swap: 2097148 0 2097148
You can stop the monitoring mode with CTRL+C keys.
Check Memory Usage with vmstat command
vmstat
(virtual memory statistics) reports information about processes, memory, paging, block IO, traps, disks and cpu activity. The first report produced gives averages since the last reboot. Additional reports give information on a sampling period of length delay
.
Syntax;
vmstat [options] [delay [count]]
When you run vmstat
command in your terminal, you obtain the following output;
vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 2087168 282024 3000788 0 0 33 35 452 198 17 7 76 1 0
FIELD DESCRIPTION FOR VM MODE.
1.Procs
- r: The number of runnable processes (running or waiting for run time).
- b: The number of processes in uninterruptible sleep.
2.Memory
These are affected by the –unit option.
- swpd: the amount of virtual memory used.
- free: the amount of idle memory.
- buff: the amount of memory used as buffers.
- cache: the amount of memory used as cache.
- inact: the amount of inactive memory. (-a option)
- active: the amount of active memory. (-a option)
3.Swap
These are affected by the –unit option.
- si: Amount of memory swapped in from disk (/s).
- so: Amount of memory swapped to disk (/s).
4.IO
- bi: Blocks received from a block device (blocks/s).
- bo: Blocks sent to a block device (blocks/s).
5.System
- in: The number of interrupts per second, including the clock.
- cs: The number of context switches per second.
6.CPU
These are percentages of total CPU time.
- us: Time spent running non-kernel code. (user time, including nice time)
- sy: Time spent running kernel code. (system time)
- id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
- wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.
- st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.
Display a table of various event counters and memory statistics
Use vmstat -s
to displays a table of various event counters and memory statistics.
vmstat -s
8049908 K total memory
2680868 K used memory
3646612 K active memory
1693260 K inactive memory
2081332 K free memory
284984 K buffer memory
3002724 K swap cache
2097148 K total swap
0 K used swap
2097148 K free swap
1151830 non-nice user cpu ticks
10700 nice user cpu ticks
455228 system cpu ticks
5462788 idle cpu ticks
53259 IO-wait cpu ticks
0 IRQ cpu ticks
2408 softirq cpu ticks
0 stolen cpu ticks
2231539 pages paged in
2390660 pages paged out
0 pages swapped in
0 pages swapped out
31588402 interrupts
102864627 CPU context switches
1618481643 boot time
1654471 forks
Check Memory Usage By Reading /proc/meminfo file
You can obtain memory usage information just by reading /proc/meminfo file direct in the command line. Use less /proc/meminfo
or cat /proc/meminfo
commands to obtain memory usage information.
less /proc/meminfo
MemTotal: 8049908 kB
MemFree: 1962320 kB
MemAvailable: 4228536 kB
Buffers: 287992 kB
Cached: 2780936 kB
SwapCached: 0 kB
Active: 3750984 kB
Inactive: 1695508 kB
Active(anon): 2517660 kB
Inactive(anon): 594344 kB
Active(file): 1233324 kB
Inactive(file): 1101164 kB
Unevictable: 138608 kB
Mlocked: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Dirty: 524 kB
Writeback: 0 kB
AnonPages: 2516172 kB
Mapped: 852760 kB
Shmem: 734444 kB
KReclaimable: 235908 kB
Slab: 374796 kB
SReclaimable: 235908 kB
SUnreclaim: 138888 kB
KernelStack: 16800 kB
PageTables: 45868 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 6122100 kB
Committed_AS: 13795160 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 33880 kB
VmallocChunk: 0 kB
Percpu: 6848 kB
HardwareCorrupted: 0 kB
AnonHugePages: 0 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
FileHugePages: 0 kB
FilePmdMapped: 0 kB
CmaTotal: 0 kB
CmaFree: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
Hugetlb: 0 kB
DirectMap4k: 338688 kB
DirectMap2M: 7948288 kB
Check Memory Usage with top command
The top
command, its main purpose is to display Linux processes but in its summary display it has three areas which are individually controlled through one or more interactive commands. One of them is Memory Usage
, this portion consists of two lines which may express values in kibibytes (KiB) through exbibytes (EiB) depending on the scaling factor enforced with the “E
” interactive command.
By default;
Line 1 reflects physical memory, classified as:
- total
- free
- used
- buff/cache
Line 2 reflects mostly virtual memory, classified as:
- total
- free
- used
- avail (which is physical memory)
When you run top
command you obtain the following;
top
top - 07:53:05 up 1 day, 18:39, 1 user, load average: 1.96, 0.88, 0.87
Tasks: 279 total, 1 running, 278 sleeping, 0 stopped, 0 zombie
%Cpu(s): 8.8 us, 3.6 sy, 0.0 ni, 87.5 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
GiB Mem : 7.7 total, 2.0 free, 2.5 used, 3.3 buff/cache
GiB Swap: 2.0 total, 2.0 free, 0.0 used. 4.2 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4612 thehero 20 0 5014836 401420 167444 S 7.6 5.0 47:56.88 chrome
1313 mysql 20 0 2016572 377060 34468 S 0.3 4.7 3:45.34 mysqld
508293 thehero 20 0 4910120 372812 234400 S 0.7 4.6 10:15.71 chrome
3316 thehero 20 0 1230968 356900 150460 S 1.3 4.4 21:32.46 chrome
The highlighted lines above are used to display memory usage using top command. The “E
” command (Extend-Memory-Scaling):
Instructs top
to force summary area memory to be scaled as:
k -
kibibytesm -
mebibytesg -
gibibytest -
tebibytesp -
pebibytese -
exbibytes
This can be changed with the “E
” command toggle.
Check Memory Usage with htop command
htop
– interactive process viewer. Similar to top command the htop
command also shows memory usage along with various other details, memory usage is shown in the top segment of htop
.
htop
In the above htop
command image, you can see that we have Mem and Swp in the top segment. These two provide you with memory usage information.
This marks the end of our tutorial on how to check memory usage on Linux. I hope this tutorial has been helpful.
For more information for the above command, use their man
pages to learn more about them.
You can also check;
How To List Running Services on Linux
How to use netstat command in Linux