Centos – Swap 95%+ , but a lot of free ram memory

centosmemory

I am running centos 5.8 with cpanel.
Lately I am getting reports that my swap is full , but there is a lot of free memory to use.

top - 10:33:43 up 133 days, 17:00,  1 user,  load average: 0.05, 0.03, 0.05  
Tasks: 170 total,   1 running, 169 sleeping,   0 stopped,   0 zombie  
Cpu(s):  2.1%us,  0.5%sy,  0.0%ni, 97.2%id,  0.0%wa,  0.0%hi,  0.2%si,  0.0%st  
Mem:  24726100k total,  8255368k used, 16470732k free,   599560k buffers  
Swap:  1046520k total,   984740k used,    61780k free,  3641828k cached  

How do I solve this?
The unused ram memory should be used instead of the swap.
Or should I increase the swap ( and how do I do that ? ).

Thanks

Best Answer

That's rather curious... whilst the kernel will aggressively swap out pages that aren't in use to allow for more disk cache, it won't usually do that until most of the memory is actually being used for cache.

I can think of two possibilities here:

  1. The top output you've provided was taken at a time when a large memory-using process had just terminated, leaving a large amount of unused RAM. Decent sar output (of memory and swap usage) over an extended period would confirm or refute this.
  2. You've turned the swappiness knob waaaay the hell up, and so the kernel's merrily following your instructions and swapping everything out it can get it's hands on. Double-check /proc/sys/vm/swappiness (default is 60) and wind it back if it's higher than that (you can also set it even lower if you really want to, but don't blame me if your machine ends up performing like balls because it doesn't have any disk cache).

Also, I wouldn't worry that the machine has lots of pages swapped out -- just give the machine more swap and let it do it's thing. It's almost guaranteed that the kernel memory manager is smarter than you at optimising memory usage.