Ora

How to See Swap Per Process in Linux

Published in Linux Swap Monitoring 5 mins read

To effectively monitor swap usage per process in Linux, you can leverage several command-line tools such as top (by enabling its dedicated SWAP column), smem for more detailed and accurate reports, or by directly querying a process's status file in the /proc filesystem.

Understanding Swap Space and Per-Process Monitoring

Swap space on Linux acts as an overflow for your RAM. When physical memory (RAM) is full, the operating system moves less frequently used pages of memory from RAM to swap space on a hard drive or SSD. While this prevents out-of-memory errors, excessive swapping can significantly degrade system performance due to the slower access speeds of disk storage compared to RAM.

Monitoring swap usage per process is crucial for:

  • Identifying memory hogs: Pinpointing applications or services that consume an unusually large amount of swap, indicating they are being heavily paged out of physical memory.
  • Performance troubleshooting: Diagnosing system slowdowns related to I/O bottlenecks caused by constant swapping.
  • Resource optimization: Understanding which processes are under memory pressure allows for better resource allocation or application tuning.

Detailed Methods to View Swap Per Process

Here are the primary methods to inspect individual process swap usage:

1. Using top for Real-time Monitoring

The top utility provides a dynamic, real-time view of running processes. By default, the SWAP column might not be visible, but it can be easily enabled:

  1. Open your terminal and type top.
  2. Once top is running, press the f key to enter the fields management screen.
  3. Use the down arrow key to navigate through the list of available fields until you locate SWAP = Swapped Size (KiB).
  4. Press the d key (or spacebar on some systems) to select this field for display. If you wish to reposition the column, use the left or right arrow keys to move it to your preferred location in the display order.
  5. Press Enter or q to exit the fields management screen.

You will now see a SWAP column in the top output, indicating the amount of swapped memory (in KiB) for each process. Processes with high SWAP values are likely being heavily pushed out of physical RAM.

Alternatively, the more user-friendly htop utility, which can often be installed via your distribution's package manager (sudo apt install htop or sudo yum install htop), provides a more intuitive interface and can also be configured to show swap usage per process.

2. Utilizing smem for Comprehensive Analysis

The smem utility offers a more accurate and detailed breakdown of memory usage, including swap, by reporting Unique Set Size (USS) and Proportional Set Size (PSS), which are better indicators of a process's true memory footprint than RSS (Resident Set Size). smem can also directly report swap usage.

First, you might need to install smem if it's not already on your system:

  • Debian/Ubuntu: sudo apt install smem
  • CentOS/RHEL/Fedora: sudo yum install smem or sudo dnf install smem

Here are some useful smem commands:

  • Show swap for all processes, sorted by swap usage:
    sudo smem -swk
    • -s: Sort by swap usage.
    • -w: Show swap usage.
    • -k: Show sizes in kilobytes.
  • Show a detailed report including USS and SWAP for all processes:
    sudo smem -P "" -c "command pid uss swap" | head -n 20

    This command lists the command, PID, USS, and SWAP for the top processes.

  • Get swap usage for a specific process (e.g., Firefox):
    sudo smem -P firefox -c "command pid uss swap"

Here's an example of smem -swk output:

PID User Command Swap (KiB)
12345 user1 /usr/bin/firefox 20480
67890 user2 /usr/bin/gnome-she 5120
54321 root mysqld 1024
... ... ... ...

For more information, consult the smem man page: man smem or visit the smem GitHub repository.

3. Inspecting /proc/<PID>/status Directly

For a specific process, you can directly query its status file within the /proc filesystem to find its VmSwap entry. This method requires knowing the Process ID (PID).

  1. Find the PID of your target process: You can use ps aux and grep for this.

    ps aux | grep <process_name>

    For example, to find the PID of a mysqld process:

    ps aux | grep mysqld

    The second column in the ps aux output is the PID.

  2. Read the VmSwap entry: Once you have the PID, substitute it into the following command:

    cat /proc/<PID>/status | grep VmSwap

    For example, if mysqld has PID 54321:

    cat /proc/54321/status | grep VmSwap

    This will output a line similar to:

    VmSwap:     1024 kB

    This indicates that the process with PID 54321 currently has 1024 KiB of its memory swapped out to disk.

Interpreting Swap Usage and Troubleshooting

  • High VmSwap values or large numbers in top's SWAP column for critical applications can indicate that your system is under memory pressure and might benefit from more RAM.
  • Sporadic high swap usage for certain processes might be acceptable, but sustained high swap for active applications often points to performance bottlenecks.
  • Total system swap usage can be checked with free -h to see how much of your overall swap space is being utilized.

If you identify a process that is consistently swapping heavily:

  • Restart the application: Sometimes, a memory leak or inefficient memory management can be temporarily resolved by restarting the application.
  • Allocate more RAM: If your system frequently swaps, adding more physical RAM is often the most effective solution.
  • Tune application configuration: Some applications allow you to configure their memory limits, potentially reducing their swap footprint.
  • Adjust swappiness: The vm.swappiness kernel parameter (default is often 60) determines how aggressively the system swaps. A lower value (e.g., 10 or 20) makes the system prefer keeping data in RAM longer. This can be changed temporarily with sudo sysctl vm.swappiness=10 or permanently by editing /etc/sysctl.conf.