
The free command is used to view how much memory (RAM) is available. The free command without any options will display the available memory in bytes.
~]# free
total used free shared buff/cache available
Mem: 10054428 5930312 297116 217544 3827000 3592572
Swap: 8388604 45056 8343548
Typically, the -h or --human option is used to view the available memory rounded:
- B = bytes
- K = kilobytes
- M = megabytes
- G = gigabytes
- T = terabytes
- P = petabytes
~]# free --human
total used free shared buff/cache available
Mem: 9.6G 5.7G 289M 212M 3.7G 3.4G
Swap: 8.0G 44M 8.0G
The --total flag can be used to print the total memory (mem + swap).
~]$ free --human --total
total used free shared buff/cache available
Mem: 15G 5.7G 3.3G 107M 6.5G 9.4G
Swap: 8.0G 0B 8.0G
Total: 23G 5.7G 11G
The following options can be used to round the values.
- -b or --bytes
- -k or --kilo
- -m or --mega
- -g or --giga
- --tera
- --peta
~]$ free --giga
total used free shared buff/cache available
Mem: 9 5 0 0 3 3
Swap: 7 0 7
The -s or --seconds option can be used to rediplay the output every x seconds. Optionally, the --count option can be used to limit the number of times the output will be redisplayed, such as 10 times in this example.
free --seconds 1 --count 10
If swap memory free is low, refer to Linux Fundamentals - Analyzing swap memory.
The following command can be used to display the top 10 processes using swap memory.
~]$ for file in /proc/*/status ; do egrep 'VmSwap|Name' $file | sed ':label; N; $! b label; s|\n| |g' | awk '{print $4 " " $5 " " $2}'; done | sort --numeric --reverse | head
296272 kB wdavdaemon
101292 kB wdavdaemon
82872 kB httpd
81860 kB httpd
81720 kB httpd
70932 kB wdavdaemon
64560 kB httpd
47700 kB httpd
46916 kB httpd
46160 kB httpd
Likewise, you can replace VmSwap with VmData in the prior command to get "normal" memory, not the swap memory.
for file in /proc/*/status ; do egrep 'VmData|Name' $file | sed ':label; N; $! b label; s|\n| |g' | awk '{print $4 " " $5 " " $2}'; done | sort --numeric --reverse | head
Notice in the above example that numerous HTTPD processes are using swap. grep can be used to display the processes containing a certain string (httpd in this example).
~]$ for file in $( ls /proc/*/status ); do egrep '^(VmSwap|Name|Pid)' $file | sed ':label; N; $! b label; s|\n| |g' | awk '{print $6 " " $7 " " $2 " (PID " $4 ")"}'; done | sort --numeric --reverse | grep httpd
83968 kB httpd (PID 32287)
83080 kB httpd (PID 31545)
82476 kB httpd (PID 31621)
65488 kB httpd (PID 36144)
51836 kB httpd (PID 30379)
50920 kB httpd (PID 33691)
50460 kB httpd (PID 31813)
44676 kB httpd (PID 35054)
41372 kB httpd (PID 33760)
39396 kB httpd (PID 36338)
36280 kB httpd (PID 33107)
Did you find this article helpful?
If so, consider buying me a coffee over at