• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to find out what is using swap on FreeBSD?



Thanks: 3
Messages: 66

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?


Nicola Mingotti

Active Member

Thanks: 96
Messages: 201

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
$> top -w
Then, sort processes according to their swap usage pressing "o",
then write "swap" and press RETURN.

Tested working in FreeBSD-11.1.



Aspiring Daemon

Thanks: 131
Messages: 854

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.

Nicola Mingotti

Active Member

Thanks: 96
Messages: 201

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:
-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.

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

puts "\n------ SUMMING on pid ----"
sepThous = sprintf("%d", allSwap).gsub(/(\d)(?=\d{3}+$)/, '\1,')
puts "summed per pid Swap: #{sepThous}"
puts ""
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;)

Vick Khera

New Member

Messages: 3

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 :)