Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find out which processes are using swap space in Linux?
Swap space is a storage area on disk used by Linux when the physical memory (RAM) is full. When the operating system needs more memory resources and RAM has no space left, inactive pages are moved to swap space. While swap space helps extend available memory, it should not be considered a replacement for more RAM due to slower disk access speeds.
There are several methods to identify which processes are using swap space in Linux. The most straightforward approaches include using system utilities like smem, the top command, or custom shell scripts.
Using the Top Command
The basic approach is to use the top command. Press f to enter the field selection menu, scroll down to find SWAP, press the spacebar to select it, then type q to return to the main display with swap usage visible.
Using SMEM
SMEM is a command-line utility that provides detailed reports on memory usage in Linux systems, including swap space consumption by individual processes.
Installing SMEM
Install smem using your distribution's package manager −
For Ubuntu/Debian −
sudo apt-get install smem
For CentOS/RHEL −
yum install smem
Basic Usage
Run the basic smem command to display process memory usage −
smem
PID User Command Swap USS PSS RSS 46740 www-data /usr/bin/php-cgi 2904 0 2 4 3623 root ssh-agent 572 4 4 4 53398 www-data /usr/bin/php-cgi 2748 4 4 8 53396 immukul /usr/bin/go-cgi 2788 4 4 8 7855 root rpc.rquotad 124 4 6 116 7380 root ssh-agent 664 4 3 112 34802 root ssh-agent 576 4 8 9
The Swap column shows swap space usage in kilobytes for each process. You can sort by swap usage using −
smem -s swap
Shell Script Approach
A custom shell script can check processes in specific directories for swap usage −
#!/bin/bash
SUM=0
OVERALL=0
for DIR in `find /proc -maxdepth 1 -type d -regex "^/proc/[0-9]+"`
do
PID=`echo $DIR | cut -d / -f 3`
PROGNAME=`ps -p $PID -o comm --no-headers 2>/dev/null`
for SWAP in `grep VmSwap $DIR/status 2>/dev/null | awk '{ print $2 }'`
do
let SUM=$SUM+$SWAP
done
if (( $SUM > 0 )); then
echo "PID=$PID swapped $SUM KB ($PROGNAME)"
fi
let OVERALL=$OVERALL+$SUM
SUM=0
done
echo "Overall swap used: $OVERALL KB"
PID=1234 swapped 2904 KB (php-cgi) PID=5678 swapped 2748 KB (apache2) Overall swap used: 26067 KB
Alternative Methods
You can also check individual process swap usage by examining the /proc/PID/status file −
grep VmSwap /proc/*/status | grep -v "0 kB"
Or use the cat command to check system-wide swap usage −
cat /proc/swaps
Conclusion
Monitoring swap usage is essential for system performance analysis. The smem utility provides the most comprehensive view of process memory usage including swap consumption, while shell scripts offer customizable monitoring solutions. Regular monitoring helps identify memory-intensive processes and optimize system performance.
