Check Free Memory on Linux for Running Program
Check Free Memory on Linux for Running Program
To determine the memory usage of Linux machine, you can easily google for this. But, several results may confuse you that which one is the most efficient way. After my hard finding, I found that "free" command is the best choice that fits best for checking memory usage. A free command can display the total amount of free and used physical memories, and swap memory in the system, as well as the buffers that used by the kernel.
Just type free -g
, this will show you the total, used, and free memory.
free displays amount of free and used memory in the system. -g as optional, memory will be shown in Gigabytes.
For example,
total used free shared buffers cached
Mem: 62 54 8 1 2 45
-/+ buffers/cache: 6 56
Swap: 99 4 95
- Total memory is 62 GB
- Used memory is 6 GB (2nd row)
- Free memory is 56 GB (2nd row)
or
total used free shared buff/cache available
Mem: 31 0 23 0 6 29
Swap: 15 0 15
- Total memory is 31 GB
- Free memory is 29 GB (+ buffers/cache)
To automatically determine the amount of free memory of multiple remote machines one-by-one, you may use the following code shell script which I wrote for my system.
#!/bin/bash
for i in compute-1 compute-2 compute-3 compute-4 compute-5 compute-6
do
CPU=`ssh $i "grep -c '^processor' /proc/cpuinfo"`
MEM1=`ssh $i "free -g|grep 'buffers/cache'"`
MEM2=`ssh $i "free -g|grep 'Mem'"`
echo "$i $MEM1" > mem.info.$i
echo "$i $MEM2" >> mem.info.$i
#MEMCHECK=`grep 'buffers' mem.info.$i`
if [ $(grep -c "buffers" mem.info.$i) -ne 0 ];then
FREEMEM=`awk 'FNR == 1 {print $5}' mem.info.$i`
else
FREEMEM=`awk 'FNR == 2 {print $8}' mem.info.$i`
fi
echo "Node: $i : Number of Processor = $CPU cores and Free Memory = $FREEMEM GB"
rm mem.info.$i
done
P.S. You need a password-less SSH for accessing all machines that you list in for loop.
Rangsiman Ketkaew