# Flush a swap



## clinty (Mar 25, 2009)

Hello.

This night, my system swapped. 50% of the swap were used.
I restarted some services, and all is OK.
Except some processes. They still are in swap.


```
403  ??  IWs    0:00,00 pflogd: [priv] (pflogd)
  542  ??  IWs    0:00,00 /sbin/devd
  983  v0  IWs+   0:00,00 /usr/libexec/getty Pc ttyv0
  984  v1  IWs+   0:00,00 /usr/libexec/getty Pc ttyv1
  985  v2  IWs+   0:00,00 /usr/libexec/getty Pc ttyv2
  986  v3  IWs+   0:00,00 /usr/libexec/getty Pc ttyv3
  987  v4  IWs+   0:00,00 /usr/libexec/getty Pc ttyv4
  988  v5  IWs+   0:00,00 /usr/libexec/getty Pc ttyv5
  989  v6  IWs+   0:00,00 /usr/libexec/getty Pc ttyv6
  990  v7  IWs+   0:00,00 /usr/libexec/getty Pc ttyv7
```

a 'top' give:

```
em: 202M Active, 210M Inact, 182M Wired, 35M Cache, 110M Buf, 360M Free
Swap: 500M Total, 7060K Used, 493M Free, 1% Inuse
```

Is that possible to flush the swap memory? (without reboot the machine).

Thanks!


----------



## SirDice (Mar 25, 2009)

This will probably do the trick:


```
swapoff -a && swapon -a
```

Provided you have enough internal memory to store everything.


----------



## clinty (Mar 25, 2009)

SirDice said:
			
		

> This will probably do the trick:
> 
> 
> ```
> ...


Uh... That's not dangerous to do this on production server?


----------



## DutchDaemon (Mar 25, 2009)

Read swapon(8).



> Note that swapoff will fail
> and refuse to remove a swap device if there is insufficient VM (memory +
> remaining swap devices) to run the system.  The swapoff utility must move
> swapped pages out of the device being removed which could lead to high
> ...


----------



## gordon@ (Mar 30, 2009)

clinty said:
			
		

> Hello.
> 
> This night, my system swapped. 50% of the swap were used.
> I restarted some services, and all is OK.
> Except some processes. They still are in swap.



They are still in swap because they don't need to be in memory. Having processes in swap is not bad.

Here's what probably happened. At some point, you had a process that caused a low-memory situation. Up until then, FreeBSD doesn't use swap, because it didn't need to. But in that low-memory situation, FreeBSD looked for idle pages to swap to disk. It found some and put them to swap. The only way they get out of swap is to get used. When needed the kernel will pull them back into memory.

The upshot of this is it's a good thing to have these processes swapped out. You actually have more memory available now that the idle processes aren't using active memory.


----------



## Mel_Flynn (Mar 30, 2009)

gordon@ said:
			
		

> The upshot of this is it's a good thing to have these processes swapped out. You actually have more memory available now that the idle processes aren't using active memory.



At the expense of having a higher startup time once becoming active. With a swap usage of only 7M, I wouldn't bother doing anything about it.

In fact, flushing swap is never really needed, as the system is self-healing by nature: swap gets used if memory is low and gets unused if physcial memory becomes available and the swapped processes are being activated.
The only reason to do so, is if you had a peak caused by unwanted behavior and services you want to be responsive are now all swapped out, however even then it's better to restart those services (which will activate their processes) then to force a swap flush.


----------

