Linux – What and why is the swap space used under linux

linuxswap

on my linux system I get these stats from top:

Tasks: 155 total,   1 running, 153 sleeping,   0 stopped,   1 zombie
Cpu(s):  1.5%us,  0.3%sy,  0.0%ni, 97.4%id,  0.7%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   8177180k total,  2025504k used,  6151676k free,    44176k buffers
Swap:  7999996k total,   495300k used,  7504696k free,   637612k cached

There it shows me that my system is using 495Mb of swap. Why is this so?
6Gigs of ram are free. And if I would disable swap entirely the system would also work.

Any explanation what the number really shows or who is swapping?

Best Answer

Even if there's no application demands on your memory, Linux will swap out unused portions of processes "in advance" of actually needing to so that it can free that memory immediately when the time comes. You can adjust the tendency to do this by adjusting vm.swappiness (/proc/sys/vm/swappiness) per the instructions here.

As for seeing what is swapped, you're theoretically able to tell from the output of top (by subtracting the virtual and resident memory columns, or using the swap column that does the same for you) but my system has 0 swap used and an apache2 process with 248m "Virtual Image", of which 9376k is supposedly "resident", leaving 239m "swapped". I'm not sure if there's an actual way to identify which specific processes or parts of processes are actually in the swap file.