YouTubeLinkedInTwitterLinkFacebook

How to Calculate Memory Usage in Linux: 

Introduction

If you are managing Linux servers, knowing about memory use is very important. Good memory use helps your servers work better and more reliably. 

In this guide, we'll explain the ways you can use to calculate and keep track of how much memory is being used in Linux.

Understanding Key Memory Metrics:

Total Memory: The sum of physical memory installed in your system.

Used Memory: The portion of memory currently utilized by processes.

Free Memory: The amount of memory that is not in use.

Buffers/Cache: Memory used by the Linux kernel for buffering and caching, which enhances performance.

Important Note:

As a system administrator, it's important to understand that buffer and cache memory should not be counted as used memory. This is because buffer and cache are used to improve system performance. So, we should not think of this as memory being used up.

Using the free Command:

A very easy and useful tool you can use is the free command. Here's how to do it: Type free -mw in your terminal. This command shows memory details in megabytes, which is simpler to understand. The result will show different things like total memory, how much is used, how much is free, shared, buffer/cache, and how much memory is available.

To calculate memory usage in percentage:

To find out how much memory is being used in percentage on a Linux system, you can use a simple formula:  Subtract the free memory from the total memory, divide this by the total memory, and then multiply by 100. 

This tells you how much memory is being used in percentage.



Memory Utilization=( Total Memory−Free Memory )×100

                     -------------------------

                           Total Memory 

I have created a shell script that assists in determining the amount of memory being used in Linux systems. This script conveniently displays the memory usage as a percentage.

#!/bin/bash

#Author: www.virtualnetworkingconcept.com

Hostnames=$(uname -n | cut -d. -f1)

os_type=$(uname -s)


# Define color variables

GREEN="\033[0;32m"

YELLOW="\033[0;33m"

NC="\033[0m" # No Color


# Define RAM threshold

ramthreshold=90.0


case "$os_type" in

  Linux)

# Fetch memory data from /proc/meminfo

MemFree=$(grep MemFree /proc/meminfo | awk '{print $2}')

Buffers=$(grep Buffers /proc/meminfo | awk '{print $2}')

Cached=$(grep -i Cached /proc/meminfo | grep -v SwapCached | awk '{print $2}')

MemTotal=$(grep MemTotal /proc/meminfo | awk '{print $2}')

SReclaimable=$(grep SReclaimable /proc/meminfo | awk '{print $2}')

SUnreclaim=$(grep SUnreclaim /proc/meminfo | awk '{print $2}')

# Calculate RAM utilization

total=$(echo "scale=0; $MemTotal/1024" / 1024| bc -l)

used_memory=$(echo "scale=0; ($MemTotal - $MemFree - $Buffers - $Cached - $SReclaimable) / 1024 " | bc -l)

used_memory_percentage=$(echo "scale=2; ($MemTotal - $MemFree - $Buffers - $Cached - $SReclaimable) / $MemTotal * 100" | bc)


if (( $(echo "$used_memory_percentage >= $ramthreshold" | bc -l) )); then

# Print the table header in a box

#echo "Memory Utilized = ( ( Total - Free ) / Total * 100 ) =" $used_memory_percentage "%"

printf "+------------------+-------------------+------------------+-----------------+--------+\n"

printf "| %-16s | %-17s | %-16s | %-14s | %-06s |\n" "HostName" "Total Memory (GB)" "Used Memory (MB)" "Used Memory (%)" "Status"

printf "+------------------+-------------------+------------------+-----------------+--------+\n"


# Print the table rows

printf "| %-16s | %-17s | %-16s | %-15s | ${YELLOW}%-06s${NC} |\n" "$Hostnames" "$total" "$used_memory" "$used_memory_percentage %" "High"


# Print the bottom border of the table box

printf "+------------------+-------------------+------------------+-----------------+--------+\n"

else

# Print the table header in a box

#echo "Memory Utilized = ( ( Total - Free ) / Total * 100 ) =" $used_memory_percentage "%"

printf "+------------------+-------------------+------------------+-----------------+--------+\n"

printf "| %-16s | %-17s | %-16s | %-14s | %-06s |\n" "HostName" "Total Memory (GB)" "Used Memory (MB)" "Used Memory (%)" "Status"

printf "+------------------+-------------------+------------------+-----------------+--------+\n"


# Print the table rows

printf "| %-16s | %-17s | %-16s | %-15s | ${GREEN}%-06s${NC} |\n" "$Hostnames" "$total" "$used_memory" "$used_memory_percentage %" "Normal"


# Print the bottom border of the table box

printf "+------------------+-------------------+------------------+-----------------+--------+\n"

fi

    ;;

  *)

    echo "This script is not supported for $os_type - $Hostnames"

    ;;

esac

Script output: Below is the referenced script output for calculating memory usage in percentage. 

# sh check_memory_usage.sh

+------------------+-------------------+------------------+-----------------+--------+

| HostName         | Total Memory (GB) | Used Memory (MB) | Used Memory (%) | Status |

+------------------+-------------------+------------------+-----------------+--------+

| server           | 1                 | 213              | 11.00 %         | Normal |

+------------------+-------------------+------------------+-----------------+--------+

Conclusion:

This guide and the shell script simplify help to check the real time memory usage on Linux systems. Now, you can easily determine the memory utilization in percentage, which aids in efficiently managing your Linux systems. You can view my YouTube video below, which demonstrates real-time memory usage calculation in Linux.