Linux free Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux free command to monitor and analyze the memory usage on your system. The lab covers the purpose and syntax of the free command, as well as how to customize the command output to get more detailed information about your system's memory usage. You will also learn how to interpret the free command's output and use it to identify potential memory-related issues on your system.

The lab is divided into three main steps: understanding the purpose and syntax of the free command, analyzing memory usage with the free command, and customizing the free command output. By the end of this lab, you will have a solid understanding of how to effectively use the free command to monitor and manage the memory on your Linux system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/wc("`Text Counting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/SystemInformationandMonitoringGroup -.-> linux/top("`Task Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/free("`Memory Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") subgraph Lab Skills linux/wc -.-> lab-422687{{"`Linux free Command with Practical Examples`"}} linux/echo -.-> lab-422687{{"`Linux free Command with Practical Examples`"}} linux/top -.-> lab-422687{{"`Linux free Command with Practical Examples`"}} linux/free -.-> lab-422687{{"`Linux free Command with Practical Examples`"}} linux/df -.-> lab-422687{{"`Linux free Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the free Command

In this step, you will learn about the purpose and syntax of the free command in Linux. The free command is a useful tool for monitoring the system's memory usage, including both physical memory (RAM) and swap space.

To use the free command, simply run the following in the terminal:

free

This will display the current memory usage on your system. The output will look similar to this:

              total        used        free      shared  buff/cache   available
Mem:        2048236     1023936      368584       72156      655716     1546700
Swap:       1048572           0     1048572

The output shows the total, used, free, shared, buffer/cache, and available memory on the system. The "Swap" section shows the total, used, and free swap space.

You can also use the following options with the free command to customize the output:

  • -h: Display the memory size in human-readable format (e.g., MB, GB)
  • -m: Display the memory size in megabytes
  • -g: Display the memory size in gigabytes
  • -t: Display the total for all memory types

For example, to display the memory usage in a human-readable format, run:

free -h

Example output:

              total        used        free      shared  buff/cache   available
Mem:           2.0G        1.0G        360M         70M        640M        1.5G
Swap:          1.0G          0B        1.0G

This step has introduced the purpose and basic usage of the free command. In the next step, you will learn how to analyze memory usage in more detail using the free command.

Analyze Memory Usage with the free Command

In this step, you will learn how to analyze the memory usage on your system using the free command.

First, let's run the free command again to see the current memory usage:

free

Example output:

              total        used        free      shared  buff/cache   available
Mem:        2048236     1023936      368584       72156      655716     1546700
Swap:       1048572           0     1048572

The output provides several important metrics:

  • total: The total amount of physical memory (RAM) or swap space available on the system.
  • used: The amount of memory or swap space currently in use.
  • free: The amount of memory or swap space that is currently unused and available.
  • shared: The amount of memory used by processes that can be shared with other processes.
  • buff/cache: The amount of memory used for file buffers and cache.
  • available: The amount of memory that is available for starting new applications, without swapping.

To better understand the memory usage, let's break down the output:

  • The "Mem:" section shows the physical memory (RAM) usage. In this example, the system has 2GB of RAM, with 1GB used and 368MB free.
  • The "Swap:" section shows the swap space usage. In this example, the system has 1GB of swap space, and none of it is currently in use.

You can also use the -h option to display the memory sizes in a more human-readable format:

free -h

Example output:

              total        used        free      shared  buff/cache   available
Mem:           2.0G        1.0G        360M         70M        640M        1.5G
Swap:          1.0G          0B        1.0G

This makes it easier to understand the memory usage at a glance.

By analyzing the output of the free command, you can get a good understanding of how your system is using its available memory resources. This information can be useful for troubleshooting performance issues or optimizing your system's memory usage.

Customize the free Command Output

In this final step, you will learn how to customize the output of the free command to suit your specific needs.

As you've seen in the previous steps, the free command provides a lot of useful information about the system's memory usage. However, sometimes you may want to focus on specific aspects of the memory usage or display the information in a different format.

Let's explore some of the available options to customize the free command output:

  1. Display memory size in different units:

    • Use the -h option to display the memory size in a human-readable format (e.g., MB, GB):
      free -h
    • Use the -m option to display the memory size in megabytes:
      free -m
    • Use the -g option to display the memory size in gigabytes:
      free -g
  2. Display the total for all memory types:

    • Use the -t option to include a total line in the output:
      free -t
  3. Display only specific memory types:

    • Use the -w option to display only the "Mem:" section (physical memory):
      free -w
    • Use the -s option to display only the "Swap:" section (swap space):
      free -s
  4. Display the memory usage in a different format:

    • Use the --json option to display the output in JSON format:
      free --json
    • Use the --bytes option to display the memory size in bytes:
      free --bytes

By combining these options, you can tailor the free command output to your specific needs. For example, to display the memory usage in a human-readable format with the total for all memory types, you can run:

free -h -t

Example output:

              total        used        free      shared  buff/cache   available
Mem:           2.0G        1.0G        360M         70M        640M        1.5G
Swap:          1.0G          0B        1.0G
Total:         3.0G        1.0G        1.4G

By understanding how to customize the free command, you can more effectively monitor and analyze the memory usage on your Linux system.

Summary

In this lab, you learned about the purpose and syntax of the free command in Linux. The free command is a useful tool for monitoring the system's memory usage, including both physical memory (RAM) and swap space. You explored how to use the free command with various options, such as -h to display the memory size in human-readable format, -m for megabytes, and -g for gigabytes. You also learned how to analyze the memory usage in more detail using the free command, understanding the different metrics it provides, such as total, used, free, shared, buff/cache, and available memory.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like