Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

memory

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.

like image 265
Timothy Clemans Avatar asked May 14 '12 15:05

Timothy Clemans


People also ask

How do I find out my RAM percentage?

Keeping in mind the formula, MEM%= 100-(((free+buffers+cached)*100)/TotalMemory).

Which command will display the amount of free memory in the Linux system?

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.

Which command will show you free memory on host?

top command Check the KiB Mem and KiB Swap lines on the header. They indicate total, used and free amounts of the memory.


1 Answers

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) }' 
like image 154
Levon Avatar answered Sep 18 '22 08:09

Levon