free

Basic Usage of free - Checking Memory Usage

The free command is used to display information about system memory usage on Linux systems. It helps administrators monitor RAM consumption, swap usage, and available memory.

free

Example output:

               total        used        free      shared  buff/cache   available
Mem:         8165428     3251840      912344      214520     4001244     4387200
Swap:        2097148           0     2097148
  • free shows memory usage summary
  • Mem represents physical RAM
  • Swap represents disk-based virtual memory
  • Values are shown in kilobytes by default

Human Readable Output

free -h

Example output:

Mem:   7.8Gi  3.1Gi  891Mi  209Mi  3.8Gi  4.2Gi
Swap:  2.0Gi  0B     2.0Gi
  • -h converts output into human-readable units
  • Values are shown in GB/MB instead of KB
  • This format is preferred in production environments

Key Concept: buff/cache

Linux does not leave unused memory idle. It uses it for performance optimization:

  • file caching
  • disk buffering
  • system acceleration

This memory is not wasted. It is automatically freed when applications require it.


Understanding the “available” column

available
  • Represents estimated memory available for new applications
  • More accurate than “free” memory alone
  • Takes reclaimable cache into account

Monitoring Memory in Real Time

free -h -s 2
  • -s 2 refreshes output every 2 seconds
  • Useful for live monitoring of memory usage
  • Common during performance testing or debugging

Practical Monitoring Script (Step-by-Step Explanation)

Script

#!/bin/bash

AVAILABLE=$(free -m | awk '/Mem:/ {print $7}')

if [ "$AVAILABLE" -lt 500 ]; then
    echo "WARNING: Low available memory"
fi

Step 1: Shebang

#!/bin/bash
  • Defines which interpreter runs the script
  • Ensures the script is executed using Bash
  • Required for consistent behavior across systems

Step 2: Running the memory command

free -m
  • Displays memory in megabytes
  • Produces structured tabular output
  • Used here because MB values are easier to compare numerically

Step 3: Extracting the correct value

awk '/Mem:/ {print $7}'

This part processes the output:

  • awk is a text processing tool
  • /Mem:/ selects the line containing RAM information
  • {print $7} extracts the 7th column

Column meaning in free -m:

  • $1 = label (Mem:)
  • $2 = total memory
  • $3 = used memory
  • $4 = free memory
  • $5 = shared memory
  • $6 = buff/cache
  • $7 = available memory

So this step extracts the usable memory value.


Step 4: Storing the result

AVAILABLE=$(...)
  • Executes the command inside parentheses
  • Stores result into variable AVAILABLE
  • Allows reuse later in the script

Example result:

AVAILABLE=4283

Step 5: Conditional check

if [ "$AVAILABLE" -lt 500 ]; then

Explanation:

  • if starts a condition
  • [ ] evaluates a test expression
  • -lt means “less than”
  • 500 is the threshold in MB

Logic:

If available memory is less than 500 MB, trigger warning.


Step 6: Output message

echo "WARNING: Low available memory"
  • Prints message to terminal
  • Can be extended to logging systems or alerting tools

Example extension:

echo "LOW MEMORY: $AVAILABLE MB" >> /var/log/memory.log

Step 7: Closing the condition

fi
  • Ends the if statement
  • Required syntax in Bash

What this script does

Step-by-step flow:

  1. Runs free -m to get memory data
  2. Extracts available memory using awk
  3. Stores the value in a variable
  4. Compares it against a threshold (500 MB)
  5. Prints a warning if memory is low

Why this is useful in production

This type of script is used for:

  • server health monitoring
  • cron-based checks
  • lightweight alert systems
  • DevOps automation
  • detecting memory pressure early

Possible production improvements

This script can be extended with:

  • email alerts
  • system logging (logger)
  • webhook notifications
  • integration with monitoring tools (Prometheus, Zabbix)

Example:

logger "WARNING: Low memory detected: $AVAILABLE MB"

Summary

In this guide, you learned:

  • how the free command works
  • how Linux manages memory with buff/cache
  • how to interpret available memory correctly
  • how to monitor memory in real time
  • how a memory monitoring script works step by step

The key takeaway is understanding not only the commands, but also the logic behind system memory reporting and automation.