Linux free Command: Monitoring System Memory

LinuxLinuxBeginner
Practice Now

Introduction

Welcome to the lab on the Linux free command. In this lab, you will learn how to use the free command to monitor and analyze system memory usage. This skill is crucial for system administrators and developers who need to optimize system performance and troubleshoot memory-related issues.

Imagine you are a junior system administrator tasked with monitoring the memory usage of a busy web server. The free command will be your primary tool for this task, allowing you to quickly assess the current memory status and identify potential issues before they impact system performance.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/free("`Memory Reporting`") subgraph Lab Skills linux/free -.-> lab-388496{{"`Linux free Command: Monitoring System Memory`"}} end

Basic Usage of the free Command

Let's start by using the free command in its simplest form. This will give us an overview of the system's memory usage.

First, open a terminal if you haven't already. Then, run the following command:

free

You should see output similar to this:

              total        used        free      shared  buff/cache   available
Mem:        8167004     2524956     3300280      658636     2341768     4657560
Swap:       2097152           0     2097152

This output provides a snapshot of your system's memory usage. Let's break down what each column means:

  • total: The total amount of physical RAM and swap space.
  • used: The amount of RAM currently in use.
  • free: The amount of RAM that is completely unused.
  • shared: The amount of RAM used by tmpfs.
  • buff/cache: The amount of RAM used for buffer and cache.
  • available: An estimate of how much memory is available for starting new applications.

The output is divided into two rows:

  • Mem: Shows information about physical RAM.
  • Swap: Shows information about swap space (virtual memory).

All values are displayed in bytes by default.

Displaying Memory Information in Human-Readable Format

While the default output of free is precise, it can be hard to read quickly. Let's use the -h option to display the information in a more human-readable format.

Run the following command:

free -h

You should now see output similar to this:

              total        used        free      shared  buff/cache   available
Mem:          7.8Gi       2.4Gi       3.1Gi       642Mi       2.2Gi       4.4Gi
Swap:         2.0Gi          0B       2.0Gi

The -h option (which stands for "human-readable") automatically scales the numbers and adds appropriate unit suffixes (G for gigabytes, M for megabytes, etc.). This makes it much easier to quickly understand your system's memory usage at a glance.

Notice how the values are now displayed with units like Gi (gibibytes) and Mi (mebibytes). These are binary units, where 1 Gi = 1024 Mi, which is the standard way computers measure memory.

Displaying Memory Information in Megabytes

Sometimes, you might want to see the memory information in a specific unit. Let's use the -m option to display the information in megabytes.

Run the following command:

free -m

Your output should look something like this:

              total        used        free      shared  buff/cache   available
Mem:           7975        2466        3222         642        2286        4548
Swap:          2047           0        2047

The -m option forces free to display all values in megabytes. This can be useful when you need more precision than the -h option provides, but still want easily readable numbers.

Note that these are mebibytes (MiB), where 1 MiB = 1,048,576 bytes, not megabytes (MB) where 1 MB = 1,000,000 bytes. In practice, the difference is small enough that they're often used interchangeably.

Continuous Monitoring with the free Command

In real-world scenarios, you often need to monitor memory usage over time. The free command allows you to do this with the -s (seconds) option, which updates the display at regular intervals.

Let's monitor the memory usage every 3 seconds for a total of 5 updates:

free -h -s 3 -c 5

This command uses several options:

  • -h: Display in human-readable format
  • -s 3: Update every 3 seconds
  • -c 5: Stop after 5 updates

You should see output that updates every 3 seconds, similar to this:

              total        used        free      shared  buff/cache   available
Mem:          7.8Gi       2.4Gi       3.1Gi       642Mi       2.2Gi       4.4Gi
Swap:         2.0Gi          0B       2.0Gi

              total        used        free      shared  buff/cache   available
Mem:          7.8Gi       2.4Gi       3.1Gi       642Mi       2.2Gi       4.4Gi
Swap:         2.0Gi          0B       2.0Gi

...

This continuous monitoring can help you observe how memory usage changes over time, which is particularly useful when trying to identify memory leaks or understand the memory usage patterns of specific applications.

Press Ctrl+C if you want to stop the command before it completes all 5 updates.

Displaying Total Memory Usage

By default, the free command shows memory usage with buffers and cache separated. However, sometimes you might want to see the total memory usage including buffers and cache. For this, we can use the -t option.

Run the following command:

free -h -t

Your output should look something like this:

              total        used        free      shared  buff/cache   available
Mem:          7.8Gi       2.4Gi       3.1Gi       642Mi       2.2Gi       4.4Gi
Swap:         2.0Gi          0B       2.0Gi
Total:        9.8Gi       2.4Gi       5.1Gi

The -t option adds a "Total" line at the bottom, which sums up the memory and swap usage. This can give you a quick overview of your total system memory, including both physical RAM and swap space.

In this output:

  • The "Mem:" line shows the physical RAM usage.
  • The "Swap:" line shows the swap space usage.
  • The "Total:" line shows the sum of physical RAM and swap space.

This total view can be particularly useful when you want to understand your system's overall memory capacity and usage at a glance.

Summary

Congratulations! You've completed this lab on using the free command to monitor system memory. Let's recap what you've learned:

  1. Basic usage of the free command to display memory information.
  2. Using the -h option to display memory in a human-readable format.
  3. Using the -m option to display memory information in megabytes.
  4. Continuous monitoring of memory usage with the -s and -c options.
  5. Displaying total memory usage including swap with the -t option.

These skills will be invaluable for monitoring and managing system resources, particularly in scenarios where you need to optimize performance or troubleshoot memory-related issues.

Here are some additional free command options we didn't cover in this lab:

  • -b: Display the amount of memory in bytes
  • -k: Display the amount of memory in kilobytes
  • -g: Display the amount of memory in gigabytes
  • -w: Use wide output format
  • -s: Update the display every [SEC] seconds
  • --si: Use powers of 1000 instead of 1024

Remember, effective memory management is crucial for maintaining system performance and stability. Regular monitoring with the free command can help you detect and prevent memory-related issues before they impact your system's performance.

Other Linux Tutorials you may like