My database server is using 191MB of swap space - but also shows 182MB of free RAM
The box is a dedicated MySQL server, running Debian Lenny.
How can I determine who and why is filling the swap space instead of using the free RAM?
Linux swaps data from RAM based on how likely it is (in Linux's personified opinion) that it will need to be referred to any time soon. You can alter your swappiness to increase/decrease the likelihood of this happening.
That's not easy to answer - take a look here: https://help.ubuntu.com/community/SwapFaq.
You can use
As for why - one possible situation is the following. Say you have 1GB or RAM. Then you load 2GB worth of applications (including OS into this). You will have 1GB RAM used and 1GB swap used. Then you unload 1.5GB of applications. You would assume you would have 0.5GB RAM free and all swap free. However, it completely might not be the case. For example, if you loaded 3 applications:
then used A actively, it's probable that both B and C would be swapped out to disk, to make A use RAM and thus fast. When you close A and switch to C, you would have 100 MB RAM used and B still swapped out (900 MB swap used), as it is not needed.
To say it simple - kernel tries to use RAM and swap the way it thinks it is going to be efficient. Swap usage while there is still free RAM doesn't necessarily mean your system is working slower then it could.
Look at the +/- buffers line for your true ram minus the filesystem buffers. Modern OS's load commonly used files into memory and the kernel will release them as needed.
Running top I would see I have 1.7gigs free of memory but if i look at free -m i see I really have 2.8gigs free. The other 1.1 gig is filesystem cache.
Just a point about swapped memory, to support ErikA's comment to James' answer:
Data swapped out of RAM will exist both in RAM and on the swap disk until the RAM is claimed for another purpose, so are rather like mmap'ed files. As I understand it, if a process then decides to use this data before it is reclaimed, it therefore does not have to be swapped in again. Swapping is usually good, but there are two main reasons why you might not want it:
Opportunistic swapping can be eliminated with