How to Get the Percentage of Memory Free with a Linux Command

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

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

overall CPU usage and Memory(RAM) usage in percentage in linux/ubuntu

You can use top and/or vmstat from the procps package.
Use vmstat -s to get the amount of RAM on your machine (optional), and
then use the output of top to calculate the memory usage percentages.

%Cpu(s):  3.8 us,  2.8 sy,  0.4 ni, 92.0 id,  1.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 24679620 total, 1705524 free, 7735748 used, 15238348 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 16161296 avail Mem

You can also do this for relatively short output:

watch '/usr/bin/top -b | head -4 | tail -2'

A shell pipe that calculates the current RAM usage periodically is

watch -n 5 "/usr/bin/top -b | head -4 | tail -2 | perl -anlE 'say sprintf(\"used: %s   total: %s  => RAM Usage: %.1f%%\", \$F[7], \$F[3], 100*\$F[7]/\$F[3]) if /KiB Mem/'"

(CPU + Swap usages were filtered out here.)

This command prints every 5 seconds:

Every 5.0s: /usr/bin/top -b | head -4 | tail -2 | perl -anlE 'say sprintf("u...  wb3: Wed Nov 21 13:51:49 2018

used: 8349560 total: 24667856 => RAM Usage: 33.8%

How to get overall RAM usage (e.g. 57%) on Linux

If you are looking for a bash script, I just came up with this.

There may be more efficient ways of doing it but I believe this is what you are looking for:

#!/bin/bash

# grab the second line of the ouput produced by the command: free -g (displays output in Gb)
secondLine=$(free -g | sed -n '2p')

#split the string in secondLine into an array
read -ra ADDR <<< "$secondLine"

#get the total RAM from array
totalRam="${ADDR[1]}"

#get the used RAM from array
usedRam="${ADDR[2]}"

# calculate and display the percentage
pct="$(($usedRam*100/$totalRam))"
echo "$pct%"

if you save this into a file (call it pct.sh), then you can run it by

$./pct.sh
33%

Note
bash does not support floats, only integers (IIRC) so the precision of the calculation is rounded.

If you need more precision, you might need to use a different shell that supports floats, like zsh

Credit where credit is due: created with help from:
1
2
3

Determine 'Free' Memory in Linux

You can use AWK to parse the output of the free command, and get a percentage.

free | grep Mem | awk '{print $4/$2 * 100}'

Linux command for percentage of memory that is free

Using LINUX top command to compute used memory percentage

You have a few problems here.

First, this doesn't do what you want it to do.

USEDMEM=/home/modadm/top-output.txt | grep "Mem" | cut -c 25-31

You can't pipe a filename into a command. You actually want to pipe the contents of the file into the command. You can do that with 'cat'. However, grep is actually designed to search within a file so you can do

USEDMEM=$(grep "Mem" /home/modadm/top-output.txt | cut -c 25-31)

Note that $(cmd) is how you execute a command in a subshell. i.e., you can run some commands to compute the value of a variable in your script. You can also use `cmd` (backticks; usually on the tilde key) but that syntax is less clear.

Again, you probably want to calculate this result in a subshell. Also, don't use $ when assigning to variables.

$USEDPCT='echo $USEDMEM / $MAXMEM * 100 | bc'

This can be rewritten as

USEDPCT=$(echo "scale=3; $USEDMEM / $MAXMEM * 100" | bc)

Finally, you want to pipe the contents of the variable into the mail program. The pipe is expecting a program to be on the left hand side. You do this by echo'ing the value of the variable into the pipe.

echo "$USEDPCT" | mail -s "Test Email from MOD Server" test@test.com

To put everything back together:

#!/bin/bash
top -n 1 -b | grep "Mem" > /home/modadm/top-output.txt
MAXMEM=$(grep "Mem" /home/modadm/top-output.txt | cut -c 7-14)
USEDMEM=$(grep "Mem" /home/modadm/top-output.txt | cut -c 25-31)
USEDPCT=$(echo "$USEDMEM / $MAXMEM * 100" | bc -l)
echo "$USEDPCT" | mail -s "Test Email from MOD Server" test@test.com

How to calculate system memory usage from /proc/meminfo (like htop)

htop author here. These are the calculations I make to get the numbers for the green, blue and yellow bars in the memory meter:

  • Total used memory = MemTotal - MemFree
  • Non cache/buffer memory (green) = Total used memory - (Buffers + Cached memory)
  • Buffers (blue) = Buffers
  • Cached memory (yellow) = Cached + SReclaimable - Shmem
  • Swap = SwapTotal - SwapFree

In the htop source code: linux/LinuxProcessList.c and linux/Platform.c.

htop screenshot



Related Topics



Leave a reply



Submit