# when does FreeBSD kernel start swapping



## m4rtin (Apr 4, 2013)

I have a swap partition enabled and when memory usage reaches more than 95%, swapping occurs. Is there a way to check when exactly swapping starts? Is it certain percentage of overall RAM? Or does it start when less than certain bytes of memory is free despite the overall amount of memory?


----------



## SirDice (Apr 4, 2013)

It starts swapping when it needs more memory than there's anything free. But keep in mind that the various caches will get used before that. It's quite common to see all memory being used, unused memory is useless memory. But most of it will be "inactive" and filesystem cache. The "inactive" memory is more or less a program cache. Programs that are stopped (quit) are kept in memory for as long as possible. That's done because it's very likely that application will get started again at some point. By keeping it in memory it speeds up the whole user experience.


----------



## Crivens (Apr 4, 2013)

The "inactive" memory is memory which is associated with a vnode object, that means it is memory which was paged in. Executables are mapped, as are files when they are being written or read. So if you watch a movie, the "inactive" memory will also increase while the file is being read from disc. So it caches more than just programms. If no memory is free (or more exact, if free memory falls below a tunable value), memory is made free by eating up the end of the inactive memory.

Only then the currently in-use memory is written to disc.

Inactive memory is basically a read cache for binaries and files because if you close the file and then re-open it later (or from a different programm), then the pages in the "inactive" area which were associated with that vnode are re-attached to the file vnode, basically pre-reading the content where is is already known.

So the kernel starts _paging_ when free memory is below a tunable value.
If FreeBSD is still able to provide _swapping_, which would mean to force entire processes into the swap space (including kernel stacks, kernel data structures, ...) - that I do not know but tried to find out for some time. 

This would traditionally occur when paging was unable to provide enough free memory fast enough to meet some criteria, like not enough memory to have 2 big processes running at the same time. Then one of them would be stopped and placed in swap for some seconds, then being woken up again with some other process getting the freeze. Should that not work, the kernel would start to kill random processes to have the rest continue.

And yes, having PID 1 or 2 killed by this surely makes for entertainment.


----------



## m4rtin (Apr 4, 2013)

SirDice said:
			
		

> It starts swapping when it needs more memory than there's anything free. But keep in mind that the various caches will get used before that. It's quite common to see all memory being used, unused memory is useless memory.



Ok, I thought that there is a certain threshold.




			
				SirDice said:
			
		

> But most of it will be "inactive" and filesystem cache. The "inactive" memory is more or less a program cache. Programs that are stopped (quit) are kept in memory for as long as possible. That's done because it's very likely that application will get started again at some point. By keeping it in memory it speeds up the whole user experience.



Am I correct, that if more "Active memory" (memory that is allocated and actually in use by programs) is needed, then both "Inact memory" (memory that is either allocated but not recently used or memory that was freed by programs) and filesystem cache will be used for this?


----------



## SirDice (Apr 4, 2013)

m4rtin said:
			
		

> Am I correct, that if more "Active memory"(memory that is allocated and actually in use by programs) is needed, then both "Inact memory"(memory that is either allocated but not recently used or memory that was freed by programs) and filesystem cache will be used for this?


Yes, up to a configurable point. After that it will start swapping. There's a lot of good info in tuning(7) if you're interesting in tuning a system.


----------

