free command in Linux with Examples

Introduction

The free command in Linux is a fundamental tool, providing an instant snapshot of system memory usage. Whether you’re a system administrator troubleshooting performance issues or a curious user monitoring resource allocation, mastering the free command is essential. In this guide, we will explore its functionality, explain its output, and provide practical examples to make you proficient in using this powerful tool.

What Is the free Command in Linux?

The free command is part of the GNU core utilities, offering a concise view of memory usage, including:

  • Total system memory
  • Used memory
  • Free memory
  • Buffers and cache
  • Swap memory

By interpreting its output, you can make informed decisions about memory optimization, application performance, and system health.

Key Features of the free Command

Memory Metrics Explained

When you run the free command, you’ll see several important metrics:

  1. Total Memory: The total amount of RAM available on your system.
  2. Used Memory: The amount of memory currently in use.
  3. Free Memory: The unused memory ready for applications.
  4. Buffers/Cache: Memory allocated for system processes but available if needed.
  5. Swap Memory: Space on the disk used when RAM is full.

Why Use the free Command?

  • Quickly assess memory utilization.
  • Identify memory bottlenecks.
  • Plan upgrades based on resource demands.
  • Monitor system performance during high workloads.

How to Use the free Command

Basic Syntax

The basic syntax for the free command is straightforward:

free command in Linux with Examples

free [options]

On the man page, the describes it

  • free – Display amount of free and used memory in the system.
  • man free – More details information about free command.

Common Options

Here are the most commonly used options with the free command:

  • -b, -k, -m, -g: Display output in bytes, kilobytes, megabytes, or gigabytes.
  • -h: Human-readable format (e.g., “2.5G” instead of “2621440”).
  • -s [seconds]: Continuously display memory usage at specified intervals.
  • -t: Show total memory, including swap and physical memory.

Examples of the free Command in Action

1. Viewing Memory in Human-Readable Format

To display memory usage in a readable format:

free -h

Output:

              total        used        free      shared  buff/cache   available
Mem:          15Gi       8Gi       2Gi       1Gi        4Gi        6Gi
Swap:         4Gi       1Gi       3Gi

2. Monitoring Memory Over Time

To track memory usage every 2 seconds:

free -h -s 2

3. Displaying Total Memory

Include the total memory line with:

free -t

4. Checking Memory in Gigabytes

View memory statistics in gigabytes for clarity:

free -g

Advanced Usage

Automating Memory Monitoring with Scripts

Create a script to log memory usage:

#!/bin/bash
while true
do
    free -h >> memory_log.txt
    sleep 60
done

Save this script as memory_monitor.sh, make it executable, and run it in the background:

chmod +x memory_monitor.sh
./memory_monitor.sh &

Integrating free with System Monitoring Tools

Combine free with tools like top or htop for a holistic view of system performance. For example:

watch -n 1 free -h

FAQ: Common Questions About the free Command

What does “available” mean in the output?

The “available” column shows the amount of memory that applications can use without swapping. It’s a more reliable indicator than “free” for assessing system health.

Why is “used” memory high even with few applications running?

Linux uses free memory for buffers and cache to improve performance. This memory is available for applications if needed.

How can I clear cache memory?

Use the following command to free up cache memory (requires root privileges):

sync; echo 3 > /proc/sys/vm/drop_caches

Is the free command available on all Linux distributions?

Yes, the free command is part of the procps-ng package, which is included in all major Linux distributions.

External Resources

For further reading and advanced configurations, check out the following authoritative sources:

Conclusion

free command is a simple command in Linux. It is the most popular in use terminal Linux Display amount of free and used memory in the system. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.