brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: VmSwap in /proc shows which processes have memory in swap

Memory
~/tips/tip-memory-vmswap-per-process

for f in /proc/[0-9]*/status; do

List the programs that are actually sitting in swap, biggest first.

free says swap is in use but not by whom. Each process has that number in its status file.

This prints the swap per process, biggest first:

for f in /proc/[0-9]*/status; do
  awk '/^Name:/{n=$2} /^VmSwap:/ && $2>0 {print $2, n}' "$f" 2>/dev/null
done | sort -rn | head

The first column is kilobytes, the second the process name. The loop reads every status file, keeps the Name line and prints it when VmSwap is above zero.

No sudo is needed to read those files on a default setup. For a single process:

grep VmSwap /proc/1234/status

Swap use on its own is no problem. Linux may move pages of an idle service out and leave them there, which frees RAM for things that are busy. I only care when the machine is also swapping back and forth all the time, which vmstat 1 shows in the si and so columns.

The kernel documentation notes that shared memory in swap is not counted in VmSwap, and that the figure is not exact. So the total of this list will not match free. Restarting a process just to get its pages out of swap rarely helps anything.

Further reading: proc_pid_status man page

More Memory tips lsmem --summary sudo apt install smem sudo pmap -x 1234 sudo systemctl edit indexer.service leaks -atExit — ./myprogram --some-option All Memory tips (12) All topics

Link