How to find out what is using swap on FreeBSD?

I'm trying to pinpoint what process(es) is eating swap on my FreeBSD server.

I can only see how much is being used (top) but not what process is swapping its guts out.

I tried searching Google for this but didn't find anything.

Is there a way to display list of swapped data by process natively or by using some external software to achieve this?

Thanks
 
The thread is a bit old, but i had the same need today.

The fastest solution I found is, run top with the option to show swap usage
Code:
$> top -w
Then, sort processes according to their swap usage pressing "o",
then write "swap" and press RETURN.

Tested working in FreeBSD-11.1.

Bye
Nicola
 
Yeah it does show swap but I find the numbers don't add up. My system currently says 67M of swap used, but the two process that are using swap add up to less than 5M of swap. The rest of the processes show 0K. And I am running -S flag so I see all processes.
 
I agree with you PacketMan, also my per process swap measures summed up are well below
the grand total i see in "Swap Used".

"top" man page explicitely says:
Code:
-w Display approximate swap usage for each process.

I wrote this script in Ruby for reproducibility but there is one issue,
I am not sure in batch mode "top" will print all relevant rows.
Code:
[NOPARSE]
#!/usr/local/bin/ruby

topDat = `top -S -w -o swap -b`
topLines = topDat.split /\n/;

puts "\n--- summary ----"
puts topLines[3..4]
puts ""

print "--- per pid data ----"
# line 8 is where data starts
# remove last line which is white
puts "pid, swap"
allSwap = 0
(8..(topLines.length - 1)).each do |i|
  line = topLines[i]
  # add a space at beginning for ease of splitting
  line = "x #{line}"
  fields = line.split /\s+/
  puts "#{fields[1]}, #{fields[8]}"
  fields[8].sub!(/K/, " * 1024")
  fields[8].sub!(/M/, " * 1024 * 1024")
  # puts fields[8];
  newVal = eval(fields[8])
  allSwap = allSwap + newVal
end

puts "\n------ SUMMING on pid ----"
sepThous = sprintf("%d", allSwap).gsub(/(\d)(?=\d{3}+$)/, '\1,')
puts "summed per pid Swap: #{sepThous}"
puts ""
[/NOPARSE]

To know more we should read "top" code, I did it one week ago,
for another problem, it is readable. I can't do it right now, if you
can do it and discover some deep secret let me know;)
 
I observe the same lack of clarity of what is using swap with FreeBSD 11.1. The top -w output shows zero for pretty much everything, but when I kill off some java process it releases hundreds of megabytes of swap as shown by pstat -s. I would expect that the "approximate" swap usage shown by top wouldn't be zero for that process. Basically it is a useless number it shows.

I too will also add more RAM :)
 
I have noticed the same discrepancy with top's "approximate" swap reporting. Here's a fairly extreme example (FreeBSD 12.0)

Code:
top -oswap -w

Swap: 2048M Total, 2046M Used, 1528K Free, 99% Inuse

  PID USERNAME    THR PRI NICE   SIZE    RES SWAP STATE    C   TIME    WCPU COMMAND
2266 *****         1  22    0    13M      0 3676K pause    6   0:00   0.00% <tcsh>
28386 *****         1  20    0    13M      0 3132K pause    1   0:00   0.00% <tcsh>
2268 *****         1  20    0    12M      0 2900K wait     6   0:00   0.00% <su>
28388 *****         1  20    0    12M      0 2312K wait     6   0:00   0.00% <su>
  122 root          1  52    0    11M      0 1808K pause    8   0:00   0.00% <adjkerntz>
35722 root          1  52    0    12M      0 1700K wait     7   0:00   0.00% <sh>
2279 *****         1  20    0    13M      0 1380K pause    2   0:00   0.00% <tcsh>
2269 root          1  20    0    13M      0 252K pause    7   0:00   0.00% <csh>
34581 *****         1  20    0    31M  1872K  60K nanslp  11   0:00   0.00% php7
2203 smmsp         1  20    0    17M   896K  44K pause    3   0:00   0.00% sendmail
36207 mysql        47  20    0    24G    21G    0 select   2 220:32   1.28% mysqld

Swap reported by top adds up to: 17.2MB
Actual swap in use: 2046MB !!!!

Seems to be a handy switch for situations where you're trying to track down excessive swapping or memory usage, but in practice it is useless.
 
I never see that much swap use on any of my laptops. This is from my T400 with 8GB RAM running FreeBSD 12.0-RELEASE-p10:

top.png
 
I also have this issue:

Code:
last pid: 42559;  load averages:  0.34,  0.38,  0.46  up 62+23:33:48    08:33:14
74 processes:  1 running, 73 sleeping
CPU:  5.0% user,  0.0% nice,  1.3% system,  0.3% interrupt, 93.4% idle
Mem: 1913M Active, 1711M Inact, 7125M Laundry, 4585M Wired, 560M Free
ARC: 3123M Total, 2047M MFU, 442M MRU, 994K Anon, 37M Header, 596M Other
     1680M Compressed, 2633M Uncompressed, 1.57:1 Ratio
Swap: 2048M Total, 1976M Used, 72M Free, 96% Inuse

  PID USERNAME    THR PRI NICE   SIZE    RES SWAP STATE    C   TIME    WCPU COMMAND
26252 mysql         1  52    0    12M      0 2724K wait     1   0:00   0.00% <sh>
75173 root          1  49    0    11M  1068K 380K nanslp   0   0:21   0.00% cron
14085 root          1  20    0    24M  1224K  28K kqread   0   0:17   0.00% master
61453 thingty    1  22    0   662M   184M    0 accept   1   4:20   1.95% php-fpm
94591 www          27  23    0   112M    80M    0 piperd   0   2:51   1.95% httpd
64912 mysql        57  20    0  9697M  8878M    0 select   1  26.2H   0.98% mysqld
62372 thingty    1  22    0   703M   190M    0 accept   1   4:42   0.98% php-fpm
63170 thingty    1  22    0   635M   179M    0 accept   1   4:06   0.98% php-fpm
70029 thingty    1  22    0   473M   139M    0 accept   1   2:24   0.98% php-fpm
27125 thingty    1  24    0   306M   195M    0 accept   0   1:50   0.98% php-fpm
28968 root          1  20    0    61M    40M    0 select   1  43:34   0.00% vmtoolsd
61783 thingty    1  22    0   681M   191M    0 accept   1   4:36   0.00% php-fpm
94834 www          27  23    0   107M    76M    0 piperd   1   4:22   0.00% httpd
98155 www          27  20    0    95M    64M    0 piperd   1   4:05   0.00% httpd
81949 ntpd          1  20    0    18M    18M    0 select   1   3:16   0.00% ntpd
40236 root          1  20    0    10M   680K    0 select   0   3:13   0.00% devd
68914 thingty    1  32    0   354M   246M    0 accept   1   2:41   0.00% php-fpm
97045 thingty    1  20    0   338M   233M    0 select   1   2:21   0.00% php-fpm

I've asked for 90s of downtime to reboot the VM.

Will probably ask to be able to update to 2019Q4 ASAP, too.

FreeBSD 12.0-RELEASE-p10 amd64

Thinking about it, I will move to 12.1 when it's out.
 
You also don't need to reboot the VM. As long as you have enough free memory you can just do swapoff -a and swapon -a which will empty the swap back into the free memory.
 
I'm going to bet that if you restart MySQL most of the swap will have been cleared too. A combination of swapoff and swapon does work of course but is going to take a very long time if there's not enough free memory. You may even hangup the whole machine, resulting in bigger problems. It's a good way but be careful with it.
 
After the reboot, it's fixed.
Keep an eye on it though. After the reboot ARC is empty and MySQL will use very little memory. Both will consume more memory over time when the caches are warming up. That's fine but they could eventually try to use more and things will start getting swapped out again. Some swap usage is fine, even servers with lots of internal memory are going to be using some swap. But this is only fine if some unused data is getting swapped out. Having things continuously swapped out and back in again is bad.
 
Back
Top