How to find out which processes are using swap space in Linux?
Run top then press OpEnter. Now processes should be sorted by their swap usage.
Here is an update as my original answer does not provide an exact answer to the problem as pointed out in the comments. From the htop FAQ:
It is not possible to get the exact size of used swap space of a
process. Top fakes this information by making SWAP = VIRT - RES, but
that is not a good metric, because other stuff such as video memory
counts on VIRT as well (for example: top says my X process is using
81M of swap, but it also reports my system as a whole is using only 2M
of swap. Therefore, I will not add a similar Swap column to htop
because I don't know a reliable way to get this information (actually,
I don't think it's possible to get an exact number, because of shared
pages).
How can I know which process is using swap?
From here:
[a] /proc/meminfo - This file reports statistics about memory usage on
the system. It is used by free to report the amount of free and used
memory (both physical and swap) on the system as well as the shared
memory and buffers used by the kernel. You can also use free, vmstat
and other tools to find out the same information.[b]
/proc/${PID}/smaps, /proc/${PID}/status, and /proc/${PID}/stat : Use
these files to find information about memory, pages and swap used by
each process using its PID.[c] smem - This command (python script) reports memory usage with
shared memory divided proportionally.
Also you can refer Find out what is using your swap
#!/bin/bash
# Get current swap usage for all running processes
# Erik Ljungstrom 27/05/2011
SUM=0
OVERALL=0
for DIR in `find /proc/ -maxdepth 1 -type d | egrep "^/proc/[0-9]"` ; do
PID=`echo $DIR | cut -d / -f 3`
PROGNAME=`ps -p $PID -o comm --no-headers`
for SWAP in `grep Swap $DIR/smaps 2>/dev/null| awk '{ print $2 }'`
do
let SUM=$SUM+$SWAP
done
echo "PID=$PID - Swap used: $SUM - ($PROGNAME )"
let OVERALL=$OVERALL+$SUM
SUM=0
done
echo "Overall swap used: $OVERALL"
Linux: How do I get the swap used by a process?
Check if you have VmSwap
in /proc/$PROCESS/status
. For example: grep VmSwap /proc/1/status
Process swap usage with use ID
After a lot more trial and error, I was able to get this working.
The following is the code snippet that ended up working:
for file in /proc/*/status ; do awk -F: '/Tgid|VmSwap|Name/{printf $2}END{print ""}' $file 2>/dev/null ; done | grep kB | sort -k 3 -n -r | head -n 10 | awk 'BEGIN{printf "%-20s %-10s %10s %-5s %10s\n","Process ID","PID","Size","Units","User"};{cmd="ps -o user= -p " $2; cmd | getline result; printf "%-20s %-10s %10s %-5s %10s\n",$1,$2,$3,$4,result}'
Linux `top` command: how much process memory is physically stored in swap space?
The behavior depends on a version of procps you are using. For instance, in version 3.0.5 SWAP value equals:
task->size - task->resident
and it is exactly what you are encountering. Man top.1 says:
VIRT = SWAP + RES
Procps-ng, however, reads /proc/pid/status and sets SWAP correctly
https://gitlab.com/procps-ng/procps/blob/master/proc/readproc.c#L383
So, you can update procps or look at /proc/pid/status directly
Interpreting swap in top utility
Edit: From what I've read, SWAP column in the output of top is really just VIRT - RES
. It's an assumption that part of the process has been swapped out. It's difficult or impossible to get an accurate measure of how much swap a given process is using.
If you're on Linux or one of the UNIX flavors that support /proc
, you can use this to get the actual usage:
cat /proc/18810/status
(Where 18810 is the PID of your mysqld process.)
It'll show you a bunch of fields for actual memory usage. Here's an except from the mysqld process in my development VM:
VmPeak: 3258116 kB
VmSize: 3258116 kB
VmLck: 1416344 kB
VmHWM: 1180788 kB
VmRSS: 1180780 kB
VmData: 3189940 kB
VmStk: 88 kB
VmExe: 11608 kB
VmLib: 7312 kB
VmPTE: 2540 kB
VmSwap: 0 kB
The VmSwap of 0 indicates that it is not currently using any swap.
See http://man7.org/linux/man-pages/man5/proc.5.html or just man 5 proc
for information on the other fields. The manual says in part:
- VmSwap: Swapped-out virtual memory size by anonymous private pages; shmem swap usage is not included (since Linux 2.6.34).
Re your comment:
Apparently you use a version of Linux too old to include the VmSwap field in per-process status.
You can read /proc/meminfo and find out the total swap space in use on the server, but this doesn't tell you per process.
You can also run vmstat to watch for swap activity. If the "si" and "so" fields are zero, you're okay.
See https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Tuning_and_Optimizing_Red_Hat_Enterprise_Linux_for_Oracle_9i_and_10g_Databases/sect-Oracle_9i_and_10g_Tuning_Guide-Swap_Space-Checking_Swap_Space_Size_and_Usage.html
Related Topics
Get Program Execution Time in the Shell
Retrieve Cpu Usage and Memory Usage of a Single Process on Linux
Linux: Copy and Create Destination Dir If It Does Not Exist
How to Redirect the Output of the Time Command to a File in Linux
How to Get Unique Values from an Array in Bash
How to Add a New Device in Qemu Source Code
Difference Between ${} and $() in Bash
How to Normalize a File Path in Bash
What Are the Return Values of System Calls in Assembly
How to Redirect Output of Systemd Service to a File
Forward Host Port to Docker Container
How to Deal With a Filename That Starts With the Hyphen (-) Character
How to Loop Over the Output of a Shell Command
How to Create Docker Overlay Network Between Multi Hosts
Execute Command After Every Command in Bash