Linux: memory is free but swap is used. Why?

Here are Munin's diagrams.

Memory usage: http://koryagin.com/w/q/memory-day.png

Swap in/out: http://koryagin.com/w/q/swap-day.png

Why does Linux decide to increase cache and use swap when the RAM size is much bigger than the memory used by programs?


Solution 1:

Your server probably has some memory-eating and/or inactive processes having many inactive pages. Linux kernel prefers to swap out inactive portions of programs to swap since most likely the memory is better used as cache.

If you wish to change this behaviour, play with vm.swappiness sysctl tunable but be warned, outsmarting the kernel developers / distro maintainers is not that easy. :-)

Are you having performance problems on your server during that swap spike or did you ask out of curiosity?

Solution 2:

You should try to play with swappiness parameter. As you can see your system started to swap when it tried to increase file cache size, this is normal if swappines is not 0.