Linux ate my RAM Script to Check Usage

By | March 3, 2019

This post may contain affiliate links. Please read my disclosure for more info.

Linux ate my ram is a worrying thing to look at the first time you check top or free -m as it appears the linux cached memory high and showing the buff/cache too high.

Understanding how linux uses disk caching for the speed of your system, and how everything is ok, and how to check if there is an issue will help any newcomers to Linux.

Linux ate my RAM Script

Has Linux ate my ram?

Most of the time the answer is NO it hasn’t, Linux is basically using the unused memory for disk caching, so the newbie user, it makes it appear your running very low in memory, when in fact everything is ok.

Since linux is only reserving the RAM ready for application usage, for performance and is normal, you need to be able to tell your not running low on RAM.

Since both top and free -m report the memory usgaes, you need to understand the terminology from the Linux perspective, so what you call Free is really what Linux calls used:

Memory that is

Linux calls it as

used by the applications on the system

Used

Used, but placed into buffers ready to be given to anything needing it

Used (but available)

not used for anything

Free

How Much Free RAM do I Really Have?

To check how much ram is free to use for your applications, just run the free -m and check the row that says “-/+ buffers/cache” in the column that says “free“, if systems after 2016 you check the “available” column. That is your answer in megabytes:

Linux free -m before 2016

Linux free -m after 2016

If you don’t know how to read the numbers above, you will think you have ram is 99% full when it is really only 42%.

For the Math above it is just the following:

USED MEM = TOTAL RAM – SUM of FREE/AVAILABLE RAM

Then you can just use a simple Percentage Calculator:

Just put in the used memory of 622MB and the total memory of the system of 1504MB for 42%

When do you Need to Worry?

When a healthy Linux system is still working ok, you will see the following harmless behavior:

free memory is close to 0

used memory is close to total

available memory (or “free + buffers/cache”) has enough room (let’s say, 20%+ of total)

swap used does not change

Warning signs of a genuine low memory situation that you may want to look into

available memory (or “free + buffers/cache”) is close to zero

swap used increases or fluctuates

dmesg | grep oom-killer

shows the OutOfMemory-killer at work [VERY BAD]

Linux ate my RAM Script to Check Real Memory Usage:

Taking the information above on how memory is utilized and free, I wanted to create a simple script I can run that will list the real memory usage and also a summary of when I need to worry.

RHEL 6.x / CENTOS 6.x

RHEL 6.x / CENTOS 6.x

#!/bin/bash
##---------- Title :  RHEL 6 Memory Health Check ----------------------------------------## 

# Gather Variables
memtotal=$(free -m | grep Mem | awk '{print $2}')
usedmem=$(free -m | grep Mem | awk '{print $3}')
cachefree=$(free -m | grep - | awk '{print $4}')
swapfree=$(free -m | grep Swap | awk '{print $4}')
swapused=$(free -m | grep Swap | awk '{print $3}')
echo $cachefree $memtotal | awk '{print "UsedMem: " 100 - ($1/$2 * 100.0) " %"}' > /tmp/usedmem.rpt
echo $cachefree $memtotal | awk '{print "FreeMem: " $1/$2 * 100.0 " %"}' > /tmp/freemem.rpt
UsedMemPercent=$(cat /tmp/usedmem.rpt)
FreeMemPercent=$(cat /tmp/freemem.rpt)
# Print Report
echo "RHEL 6 Memory Health Check 2.0"
echo
echo "free -m"
free -m
echo
echo "swapon -s"
swapon -s
echo
echo "vmstat 2 10"
vmstat 2 10
echo
echo "Summary Report"
echo
echo "Total Memory = " $memtotal "MB"
echo "Available Free Memory = " $cachefree "MB"
echo
echo "UsedMem:" $UsedMemPercent # 100 - Mem Available / Mem Total
echo "FreeMem:" $FreeMemPercent # Mem Available / Mem Total
echo
echo "When should I start to worry?"
echo	"- free memory is close to 0, currently its" $cachefree "MB"
echo	"- used memory is close to total, currently its" $usedmem "MB used, out of" $memtotal "MB"
echo	"- swap used does not change, currently its" $swapused "MB Used, out of" $swapfree "MB"
echo	"- available memory has enough room (let's say, over 80% used of total) currently it is" $UsedMemPercent "MB"
:

RHEL 7.x / CENTOS 7.x

#!/bin/bash
##---------- Title :  RHEL 7 Memory Health Check ----------------------------------------## 

# Gather Variables
memtotal=$(free -m | grep Mem | awk '{print $2}')
usedmem=$(free -m | grep Mem | awk '{print $3}')
memfree=$(free -m | grep Mem | awk '{print $4}')
swapfree=$(free -m | grep Swap | awk '{print $4}')
swapused=$(free -m | grep Swap | awk '{print $3}')
memavailable=$(free -m | grep Mem | awk '{print $7}')
echo $memavailable $memtotal | awk '{print 100 - ($1/$2 * 100.0) " %"}' > /tmp/usedmem.rpt
echo $memavailable $memtotal | awk '{print $1/$2 * 100.0 "%"}' > /tmp/freemem.rpt
UsedMemPercent=$(cat /tmp/usedmem.rpt)
FreeMemPercent=$(cat /tmp/freemem.rpt)

# Print Report
echo "RHEL 7 Memory Health Check 2.0"
echo
echo "Total Memory = " $memtotal "MB"
echo "Available Memory = " $memavailable "MB"
echo
echo "UsedMem:" $UsedMemPercent # 100 - Mem Available / Mem Total
echo "FreeMem:" $FreeMemPercent # Mem Available / Mem Total
echo
echo "When should I start to worry?"
echo	"- available memory is close to 0 currently it is" $memavailable MB
echo	"- free memory is close to 0, currently its" $memfree "MB"
echo	"- used memory is close to total, currently its" $usedmem "MB used, out of" $memtotal "MB"
echo	"- swap used does not change, currently its" $swapused "MB Used, out of" $swapfree "MB"
echo	"- available memory has enough room (let's say, over 80% used of total) currently it is" $UsedMemPercent "MB"
:

YouTube Tutorial

This video, is a summary of above and how to deploy the script on CentOS 7, so you can see how useful the script is:

https://youtu.be/X0EVbZeO4qI

Summary

Linux ate my ram can be a worrying thing when you first look at it, but as seen it is a normal function of Linux and learning how to read the memory utilisation, is useful in Linux troubleshooting.

For other Linux related tutorials on my website please click here.

Also please have a look at my YouTube Channel and subscribe for video tutorials as they are released, and if you have any advice for others, requests or questions please leave a comment below.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.