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: virtualnetworkingconcept.com 

# Get the CPU load averages

total=$(free -mw | awk '/^Mem/  {print $2}')

buffers=$(free -mw | awk '/^Mem/  {print $6}')

cache=$(free -mw | awk '/^Mem/  {print $7}')

free=$(free -mw | awk '/^Mem/  {print $4}')

# Calculate percentages

used_memory=$(echo "scale = 2; ( $total - $free - $buffers - $cache ) " | bc -l)

used_memory_percentage=$(echo "scale = 2; ( $total - $free - $buffers - $cache ) /  $total * 100 " | bc -l)

# Print the table header in a box

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

echo "+-------------------+------------------+-----------------+"

echo "| Total Memory (MB) | Used Memory (MB) | Used Memory (%) |"

echo "+-------------------+------------------+-----------------+"

# Print the table rows

printf "| %-17s | %-16s | %-15s |\n" "$total" "$used_memory" "$used_memory_percentage %"

# Print the bottom border of the table box

echo "+-------------------+------------------+-----------------+"

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

# sh check_memory_usage.sh

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

| Total Memory (MB) | Used Memory (MB) | Used Memory (%) |

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

| 1837              | 166              | 9.00 %          |

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

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.