I would like to get the available memory reported as a percentage using a Linux command line.
I used the free
command, but that is only giving me numbers, and there is no option for percentage.
Keeping in mind the formula, MEM%= 100-(((free+buffers+cached)*100)/TotalMemory).
free -b , -k , -m , -g : Display the amount of memory in bytes, kilobytes, megabytes, gigabytes respectively. You can also use free -h to show output in human-readable output. Please run free --help for more information on the options.
top command Check the KiB Mem and KiB Swap lines on the header. They indicate total, used and free amounts of the memory.
Using the free
command:
% free total used free shared buffers cached Mem: 2061712 490924 1570788 0 60984 220236 -/+ buffers/cache: 209704 1852008 Swap: 587768 0 587768
Based on this output we grab the line with Mem
and using awk pick specific fields for our computations.
This will report the percentage of memory in use
% free | grep Mem | awk '{print $3/$2 * 100.0}' 23.8171
This will report the percentage of memory that's free
% free | grep Mem | awk '{print $4/$2 * 100.0}' 76.5013
You could create an alias for this command or put this into a tiny shell script. The specific output could be tailored to your needs using formatting commands for the print statement along these lines:
free | grep Mem | awk '{ printf("free: %.4f %\n", $4/$2 * 100.0) }'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With