Swap memory is used in Linux to supplement physical RAM by temporarily storing data on disk. This allows the system to handle more processes than would otherwise fit in RAM. When the system runs low on physical memory, inactive data is moved to the swap space to free up RAM for active processes. Monitoring swap usage helps identify when the system is running out of RAM and relying too much on disk storage, which can slow down performance.

Most Linux distributions automatically configure swap memory during installation. However, as applications demand more resources, it becomes essential to check how much of the swap space is in use. Keeping an eye on swap activity can help determine if system performance issues are caused by excessive swapping or if more physical memory is needed.

Linux offers several commands to check swap memory usage, each providing a different level of detail. Tools like free, vmstat, and swapon are commonly used to monitor swap status. These commands provide crucial data to system administrators, allowing them to optimize the system based on the current usage of memory and swap space.

Steps to check swap memory usage:

  1. Open the terminal.
  2. Use the free command to view a summary of memory and swap 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 shows the output in a human-readable format, displaying values in MB, GB, etc.

  3. Use 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

    The columns si (swap in) and so (swap out) show the rates of data being moved to and from the swap space.

  4. Use the swapon command to display detailed statistics of active swap devices.
    $ swapon -s
    Filename        Type    Size  Used  Priority
    /dev/sda5                          partition  4194300 1024000 -2

    The -s option displays the swap device's filename, type, total size, used space, and priority.

  5. Optionally, install and use tools like htop or glances for real-time monitoring of swap usage.

    To install htop on Ubuntu:

    sudo apt install htop

    .

Discuss the article:

Comment anonymously. Login not required.