How to get the percentage of memory free with a Linux command?

LinuxMemory

Linux Problem Overview


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.

Linux Solutions


Solution 1 - Linux

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) }'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTimothy ClemansView Question on Stackoverflow
Solution 1 - LinuxLevonView Answer on Stackoverflow