Swap memory plays a crucial role in enhancing the performance of a Linux system, especially when the system runs out of physical RAM. The swap memory acts as an extension of the RAM, using disk space to temporarily hold data that doesn't fit into the main memory. Monitoring swap memory usage helps in understanding the system's memory behavior and could guide decisions on whether to increase RAM or configure the swap space differently.

In many Linux distributions, the swap memory is automatically set up during installation. However, as the demand for applications grows or system usage changes, it might become necessary to monitor how efficiently the swap space is utilized. This can provide insights into performance bottlenecks and potential system optimizations.

Linux provides several tools to monitor swap usage, such as free, vmstat, and swapon. Each tool offers a unique view and level of detail regarding swap usage, giving system administrators the flexibility to choose based on their requirements.

Steps to display swap memory usage in Linux:

  1. Open the terminal.
  2. Use the free command to view a summary of memory usage.
    $ free -h
    total used free shared buff/cache available
    Mem: 15Gi 4.2Gi 9.5Gi 219Mi 1.4Gi 10Gi
    Swap: 4.0Gi 1.0Gi 3.0Gi

    The “-h” option displays the output in a human-readable format, showing values in MB, GB, etc.

  3. Utilize the vmstat command to view swap in/out rates.
    $ vmstat 5 5
    procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
    r b swpd free buff cache si so bi bo in cs us sy id wa st
    0 0 1024000 9742600 250100 1493928 0 0 272 418 656 1792 5 1 94 0 0
    0 0 1024000 9742896 250108 1493856 0 0 0 220 679 1801 1 0 99 0 0
    0 0 1024000 9742888 250108 1493880 0 0 0 216 693 1829 1 0 99 0 0
    0 0 1024000 9742896 250108 1493888 0 0 0 64 682 1816 1 0 99 0 0
    0 0 1024000 9742896 250108 1493888 0 0 0 60 678 1811 1 0 99 0 0

    The columns “si” and “so” display the swap-in and swap-out rates, respectively. Adjust the two numbers after “vmstat” to change the delay and count of outputs.

  4. Execute the swapon command to get details about all active swap devices.
    $ swapon -s
    Filename Type Size Used Priority
    /dev/sda5 partition 4194300 1024000 -2

    The “-s” option displays the swap device's statistics, including its size, used space, and priority.

  5. If you want a continuous real-time display of swap usage, consider using monitoring tools like htop or glances.

    Additional software installations may be necessary. Consult your distribution's package manager to obtain these tools.

Discuss the article:

Comment anonymously. Login not required.