Linux free Command

In the realm of Linux and Unix-like operating systems, monitoring system memory is a critical aspect of system administration. The free command is a powerful tool that provides essential insights into system memory usage, allowing users and administrators to track memory availability, usage, and system performance. In this blog post, we will explore the free command in depth, covering its syntax, options, and various practical applications.

Basic Syntax

The basic syntax of the free command is simple:

bash
free options
  • options: Additional flags that modify the behavior of the free command.

Displaying Memory Usage

To display memory usage, you can use the free command without any options.

bash
free

This will display information about total memory, used memory, free memory, shared memory, buffers, and cached memory.

Human-Readable Output

The -h or --human option makes the output more human-readable by displaying sizes in a human-readable format (e.g., KB, MB, GB).

bash
free -h

Displaying Memory Statistics at Regular Intervals

The -s or --seconds option allows you to continuously monitor memory statistics at regular intervals.

bash
free -s 10

This will display memory statistics every 10 seconds.

Displaying Total and Available Memory Only

The -t or --total option displays the total memory and available memory only.

bash
free -t

Practical Applications

  1. Viewing Memory Usage:

    bash
    free
  2. Displaying Human-Readable Output:

    bash
    free -h
  3. Continuously Monitoring Memory Usage:

    bash
    free -s 10

Advanced Usage

Displaying Memory Usage in MB

You can use awk to display memory usage in megabytes (MB).

bash
free -m | awk 'NR==2{print "Total: " $2 "MB", "Used: " $3 "MB", "Free: " $4 "MB"}'

Displaying Memory Usage in Percentage

You can calculate and display memory usage in percentage using a simple command.

bash
free | grep Mem | awk '{printf("Used Memory: %.2f%"), $3/$2*100}'

Monitoring Memory Usage in a Continuous Loop

You can use a while loop to continuously monitor memory usage.

bash
while true; do free -h; sleep 10; done

This will display memory usage every 10 seconds in a loop.