free Command: Tutorial & Examples

Display information about free and used memory

The free command provides an up-to-date snapshot of the system's memory usage by reading kernel-maintained data from the /proc/meminfo file. It reports the total, used, free, cached, and available physical memory (RAM) and swap space. System administrators rely on free to monitor memory consumption, diagnose memory-related performance issues, and ensure the system has sufficient resources to operate efficiently.

Understanding the free command output and options is essential for interpreting how Linux manages memory, particularly the role of buffers and cache, and for detecting potential memory shortages or excessive swap usage.

How free Works

The free command extracts memory usage statistics by reading the /proc/meminfo virtual file, which is continually updated by the Linux kernel. The kernel tracks various memory metrics, including:

  • The total physical RAM installed.
  • Memory currently used by running processes and the kernel.
  • Memory used for buffers and page cache to accelerate disk I/O.
  • Free memory available for allocation.
  • Swap space usage statistics.

Linux uses free memory for disk caching and buffering to improve system performance. This memory is "used" but can be quickly reclaimed when applications require more memory. Therefore, the free command provides a more nuanced view of memory usage than simply "used" versus "free."

Explanation Of free Output

Running free without options displays output similar to the following:

total        used        free      shared  buff/cache   available

Mem:    16335456 8479144 992180 144948 5545132 7048004
Swap:    8388604 0 8388604

Each column represents:

  • total: Total amount of memory or swap space available.
  • used: Memory currently in use by processes and the kernel, including buffers and cache.
  • free: Memory completely unused.
  • shared: Memory shared between processes, often used by the tmpfs filesystem.
  • buff/cache: Memory used by kernel buffers and page cache to speed up disk access. This memory is reclaimable if needed.
  • available: An estimate of how much memory is available for starting new applications without swapping. It accounts for reclaimable memory such as buffers and cache.

The Swap row shows total, used, and free swap space. Swap acts as an extension of RAM on disk and is slower than physical memory. Swap usage indicates memory pressure or insufficient RAM.

It is important to note that the Linux kernel aggressively utilizes free memory for caching and buffering to optimize performance. Hence, the available column is the most reliable indicator of free memory for applications.

Common Command Line Parameters

The free command offers several options to customize its output:

  • -b, -k, -m, -g: Display memory usage in bytes, kilobytes (default), megabytes, or gigabytes respectively.
  • -h: Human-readable format using base 1024 units (e.g., KiB, MiB).
  • --si: Human-readable format using base 1000 units (e.g., kB, MB).
  • -t: Show an additional line with totals for RAM plus swap.
  • -s seconds: Continuously display memory statistics every specified number of seconds.
  • -c count: Repeat the display a specified number of times when used with -s.

Practical Examples Using free

  1. Basic Memory Overview

    free -h
    

    Sample output:

    total        used        free      shared  buff/cache   available
    

    Mem: 15Gi 8.1Gi 1.0Gi 14Mi 6.5Gi 6.9Gi Swap: 8.0Gi 0B 8.0Gi

  2. Display Memory in Megabytes With Total Line

    free -mt
    

    Sample output:

    total        used        free      shared  buff/cache   available
    

    Mem: 15968 8287 964 14 6706 7096 Swap: 8192 0 8192 Total: 24160 8287 9184

  3. Monitor Memory Usage Continuously Every 2 Seconds

    free -h -s 2
    

    This command will refresh memory stats every 2 seconds. Press Ctrl+C to stop.

  4. Monitor Memory Usage Using watch

    watch -n 3 free -m
    

    This runs free every 3 seconds displaying memory in megabytes.

  5. Script to Alert When Available Memory is Low

    AVAILABLE=$(free -m | awk '/^Mem:/ {print $7}')
    if [ "$AVAILABLE" -lt 500 ]; then
        echo "Warning: Low available memory: ${AVAILABLE}MB"
    fi
    
  6. Repeating Memory Status Multiple Times

    free -m -c 5 -s 10
    

    Displays memory stats 5 times at 10-second intervals.

Common Problems And Troubleshooting

  • Misinterpreting Used Memory
    Many users mistake all "used" memory as unavailable. Linux uses free memory for buffers and cache, which is reclaimable. The available column is the best indicator of usable memory.

  • High Swap Usage
    Significant swap usage can indicate insufficient RAM or memory leaks. Investigate processes consuming excessive memory.

  • Inconsistent Output Across Distros
    Older Linux versions may display different columns or naming conventions in free output.

  • Units Confusion
    By default, free reports in kibibytes (KiB, 1024 bytes), not kilobytes (kB, 1000 bytes). Use -h or --si for human-readable formats.

  • Permission Issues
    Reading /proc/meminfo requires minimal privileges but can be restricted in hardened environments, causing free to fail or report incomplete data.

Advanced Usage And Scripting

Integrate free into system monitoring or automated alert scripts. For example, extract used and total memory with awk:

    free -m | awk 'NR==2 {printf "Memory Usage: %sMB used out of %sMB\n", $3, $2}'

Use free with cron or monitoring tools to detect low memory conditions proactively.

You can combine free with other commands to generate detailed reports or logs:

    echo "$(date): $(free -m | awk '/^Mem:/ {print $3 "MB used, " $7 "MB available"}')" >> /var/log/mem_usage.log

Performance And Security Considerations

  • Running free repeatedly with short intervals (e.g., via -s) imposes minimal overhead but may affect system performance if combined with other heavy processes.
  • Access to /proc/meminfo is generally unrestricted, but some security-hardened systems may limit it to prevent information disclosure.
  • Always consider user permissions when automating monitoring scripts.

Cheatsheet

  • free — Show memory usage in KiB (default).
  • free -h — Human-readable output with units (KiB, MiB).
  • free -m — Show memory in megabytes.
  • free -g — Show memory in gigabytes.
  • free -t — Include total line for RAM + swap.
  • free -s 5 — Refresh every 5 seconds.
  • free -c 3 -s 10 — Repeat 3 times every 10 seconds.
  • free --si — Use decimal units (kB, MB).
  • Use awk to parse specific fields from output.

Alternatives And Related Commands

  • vmstat: Detailed system and memory statistics.
  • top, htop: Interactive process and memory monitoring.
  • cat /proc/meminfo: View raw kernel memory info.
  • ps: Inspect memory usage per process.
  • watch: Run commands repeatedly to monitor changes.

Each tool serves different purposes; free offers a concise snapshot useful for scripts and quick checks.

See Also

Further Reading

As an Amazon Associate, I earn from qualifying purchases.

The text above is licensed under CC BY-SA 4.0 CC BY SA