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

Solution 1:

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.

Solution 2:

Are you sure there is a problem to solve? Do you experience low performance? Check for instance "Optimizing memory usage" AT https://help.ubuntu.com/community/SwapFaq#Why_do_I_need_swap.3F A lot of used swap space does not mean there actually is a problem.

Solution 3:

I am not sure if your high swap usage is an indicator for a problem. Have a look at dstat or vmstat and watch if there is any activity in the swap area. I have seen similar situations on a server of one of my customers. The swap was almost full, but not used in any way (no I/O for the swap seen). In my case, it was not a problem at all!

And

swapoff

followed by

swapon

will empty your swap if you have enough free RAM.