free Memory Usage

free cheatsheet — check RAM and swap usage on Linux. free -h for human-readable, free -m for MB, free -s 5 to watch every 5s. Understand available vs used memory.

4 min read

What it is

A command-line utility to display the amount of free and used memory in the system.

Installation

Linux

free is typically pre-installed on most Linux distributions. If not:

  • Debian/Ubuntu: sudo apt update && sudo apt install procps
  • Fedora/CentOS/RHEL: sudo dnf install procps-ng or sudo yum install procps-ng

macOS

free is not available by default on macOS. You can install it using Homebrew: brew install coreutils After installation, you might need to use gfree instead of free to avoid conflicts with the built-in free command.

Windows

free is not available on Windows. You can use tools like Task Manager or PowerShell cmdlets (Get-ComputerInfo) for memory information.

Core Concepts

  • total: Total amount of physical RAM in the system.
  • used: Amount of memory currently being used by processes.
  • free: Amount of memory that is completely unused.
  • shared: Memory used by tmpfs (RAM-based file system).
  • buff/cache: Memory used by the kernel for buffering and caching disk I/O. This memory can be reclaimed if applications need it.
  • available: An estimate of how much memory is available for starting new applications, without swapping. This includes free memory plus a portion of buff/cache that can be easily freed. This is often the most useful metric for understanding available memory.

Commands / Usage

Basic Memory Information

  • free Display memory usage in kibibytes (KiB).

    free
    

    Output example:

                  total        used        free      shared  buff/cache   available
    Mem:        8071140     3876580     1385444      211884     2809116     3673940
    Swap:       2097148           0     2097148
    
  • free -b Display memory usage in bytes.

    free -b
    

    Output example:

                  total        used        free      shared  buff/cache   available
    Mem:        8462467072  4064718848  1452642304   222119936  2945105920  3852767232
    Swap:       2199023104           0  2199023104
    
  • free -k Display memory usage in kibibytes (KiB). This is the default.

    free -k
    
  • free -m Display memory usage in mebibytes (MiB).

    free -m
    

    Output example:

                  total        used        free      shared  buff/cache   available
    Mem:            7882        3786        1353         207        2743        3588
    Swap:           2047           0        2047
    
  • free -g Display memory usage in gibibytes (GiB).

    free -g
    

    Output example:

                  total        used        free      shared  buff/cache   available
    Mem:               7           3           1           0           2           3
    Swap:              2           0           2
    
  • free -h Display memory usage in human-readable format (e.g., KB, MB, GB).

    free -h
    

    Output example:

                  total        used        free      shared  buff/cache   available
    Mem:           7.9Gi       3.8Gi       1.3Gi       207Mi       2.7Gi       3.6Gi
    Swap:          2.0Gi          0B       2.0Gi
    
  • free -l Display detailed memory information, including Huge Pages.

    free -l
    
  • free -s 5 Update memory usage every 5 seconds. Press Ctrl+C to exit.

    free -s 5
    
  • free -c 10 Display memory usage 10 times and then exit.

    free -c 10
    
  • free --wide Display the output on a wider terminal, showing all columns without wrapping.

    free --wide
    
  • free --si Display units in powers of 1000 (e.g., KB, MB, GB) instead of powers of 1024 (KiB, MiB, GiB).

    free --si
    

Displaying Specific Information

  • free | grep Mem Show only the memory line.

    free | grep Mem
    
  • free -m | awk '/^Mem:/ {print $4}' Print only the free memory in MiB.

    free -m | awk '/^Mem:/ {print $4}'
    
  • free -h | awk '/^Mem:/ {print $7}' Print only the available memory in human-readable format.

    free -h | awk '/^Mem:/ {print $7}'
    

Common Patterns

  • Checking available memory in GB, updating every 2 seconds:

    free -g -s 2
    
  • Getting total RAM in MiB and piping to a script:

    total_ram_mib=$(free -m | awk '/^Mem:/ {print $2}')
    echo "Total RAM: ${total_ram_mib} MiB"
    
  • Monitoring memory usage continuously in human-readable format:

    watch -n 1 free -h
    

    (Requires watch to be installed: sudo apt install watch or brew install watch)

  • Checking swap usage:

    free | grep Swap
    
  • Calculating percentage of used memory:

    free -m | awk '/^Mem:/ {printf "%.2f%%\n", $3/$2*100}'
    

    This calculates the percentage of used memory from the Mem: line.

Gotchas

  • free vs available: The free column is often misleading. available is a much better indicator of how much memory can actually be used for new applications, as it accounts for reclaimable cache and buffer memory.
  • Buff/Cache is not wasted: The buff/cache memory is actively used by the Linux kernel to speed up disk operations. This memory is not lost; it will be freed automatically if applications require more RAM.
  • Swap usage: High swap usage can indicate that your system is running out of physical RAM and performance may degrade significantly.
  • macOS gfree: If you install coreutils on macOS, remember to use gfree instead of free to access the GNU version of the command.
  • Output interpretation: The units can be confusing. Always pay attention to the unit displayed (bytes, KiB, MiB, GiB, or human-readable) and use the appropriate flags (-b, -k, -m, -g, -h) to get the desired output.