free

2024-09-24

Understanding the Basics

The simplest form of the command, free, provides a snapshot of your system’s memory usage. Let’s break down a typical output:

              total        used        free      shared  buff/cache   available
Mem:          1996        162        1677          11         157        1786
Swap:         2047          0        2047

Adding Detail with Options

The free command offers many useful options to customize the output:

free -h
free -m
free -g
free -h -s 2  # Display memory usage every 2 seconds.
free -h -t
free -h -o
free -h -w 60

Interpreting the Output for Troubleshooting

High used memory and low available memory could indicate a memory leak, a process consuming excessive resources, or insufficient RAM. High Swap usage could point to insufficient RAM, requiring you to either upgrade your RAM or optimize your processes to consume less memory. The free command provides the foundational data to investigate these scenarios further. By combining free with other commands like top (for monitoring processes) and ps (for listing processes), you can effectively diagnose and resolve memory-related issues.

Beyond the Basics: Working with Output

The output of free can be further processed using other commands like awk to extract specific values or create custom reports. For example, to get just the available memory in megabytes:

free -m | awk '/Mem:/ {print $7}'

This example uses awk to filter the output for the line containing “Mem:” and then print the 7th column (available memory in MB). Similar techniques can be used to extract and manipulate any other data field from the free command’s output.