# How to find used swap?



## overmind (May 18, 2011)

I want to find used space (the value that is reported by top, that is at second position on top Swap line) with *sysctl* variables.

I need that in a C program, and I want to get it with sysctlbyname() function.

Here are *sysctl* values for swap:


```
vm.swap_enabled: 1
vm.nswapdev: 1
vm.swap_async_max: 4
vm.swap_reserved: 10697715712
vm.swap_total: 8589934592
vm.swap_idle_threshold2: 10
vm.swap_idle_threshold1: 2
vm.stats.vm.v_swappgsout: 167146
vm.stats.vm.v_swappgsin: 53192
vm.stats.vm.v_swapout: 37474
vm.stats.vm.v_swapin: 18443
vm.disable_swapspace_pageouts: 0
vm.defer_swapspace_pageouts: 0
vm.swap_idle_enabled: 0
```

I've tried different computations adding *(swappgsout+swappgsin)*4096* and other things but I cannot obtain the value from top utility which means I am not doing something right.

My question is: how used swap value is calculated?


----------



## SirDice (May 18, 2011)

Have a look in the source for swapinfo(8) (it's symlinked to pstat(8)):

http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.sbin/pstat/


----------



## darwimy (May 18, 2011)

The kvm_getswapinfo(3) system call will return the used swap space (in pages).


----------



## tingo (May 19, 2011)

On a related note: how does one find out which process(es) use swap easily?


----------



## mk (May 20, 2011)

In STAT column of ps(1) output look for capital W.


----------



## Vick Khera (Jun 7, 2018)

tingo said:


> On a related note: how does one find out which process(es) use swap easily?



I've been searching for this for a while too. Apparently you cannot. The closest I've come is the [FONT=Courier New]top -w[/FONT] command, but it is very wrong and almost always just shows 0 for every process even if it is using hundreds of K of swap (confirmed by killing that process). Have you ever found anything accurate?


----------



## SirDice (Jun 7, 2018)

Note that you are responding to a thread that's more than 7 years old. You're probably better off starting a new thread.


----------

