Wednesday, 14 August 2013

Clear Linux swap

We all know that when machine start using swap memory this means some sort of trouble. Obviously, machine exhausted all RAM available and because of that now it has to use swap space. Needles to say, this has huge performance impact. 
Anyway, if you have any kind of constant monitoring of your servers / services health,this is surely one of the parameters you will be watching and, most likely, even put alert on it - setting monitoring system to alert you when machine uses up most of the RAM memory (let's say over 80% or 90%) or when it starts using swap. However, there is one small drawback - once load on machine eases up and machine returns back to normal operation (below 80% of RAM usage, for example) Linux kernel will not necessarily clear out swap. This means that if you perform top or htop command  you will still see swap used data as if machine is still using it. And, since it is most likely that information about machine status for monitoring (at least CPU, RAM and SWAP usage)  you will be collecting from those, in such cases you will get false alert / alarm for that machine. 
In cases such as this you might want to force Linux Swap to clear instead of waiting for system to do it on it's own sometimes in the future. If so, this is how you can do it.
Firstly, make sure that you have situation I described above - machine is actually not using swap but it has been using it in near past and it was not cleared by the system yet. Use vmstat to see what is actually going on and how is machine using RAM and Swap memory.
#vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 0  0 1522940 2190736 150168 2495816    1    1     1     1    0    0 20  5 74  1
 2  0 1522940 2176216 150168 2495932    0    0     0     0 3846 2967 38 15 47  0
 3  0 1522940 2178020 150168 2496036    0    0     0     0 3980 3089 47 17 36  0
 2  0 1522940 2156256 150176 2496104    0    0     0   488 3029 2339 40 13 44  3
 1  0 1522940 2164828 150176 2496156    0    0     0    52 3114 2263 31 14 55  0
 4  0 1522936 2164212 150176 2496212    0    0     0     0 3175 3562 41 11 48  0
 1  0 1522936 2161976 150176 2496252    0    0     0     0 3322 2179 28 12 60  0
 2  0 1522936 2161664 150176 2496304    0    0     0    24 3144 2242 37 10 53  0
 3  0 1522936 2161916 150180 2496336    0    0     0   336 3017 2065 35 13 51  2
 1  0 1522928 2160348 150180 2496368    0    0     0     0 3085 2551 33 12 55  0
 0  0 1522928 2177716 150180 2496428    0    0     0     0 2762 1940 22  9 69  0
 0  0 1522928 2164068 150180 2496480    0    0     0     0 3203 2572 30 10 60  0 

Well, in this case, even though when running top / htop machine showed extensive Swap usage (1,5 GB out of 2,0 GB allocated for Swap), as you can see machine was not currently using any Swap at all (si (swapin) and so (swapout) are 0). 
So, instead of waiting for Linux kernel to automatically "swap in" those pages from disk to RAM as needed, you can easily force this action by momentarily disabling and then re-enabling swap:
sudo swapoff -a
sudo swapon -a
It is good practice to combine previous two commands:

sudo swapoff -a && sudo swapon -a

However, do be warned! This will make your machine temporarily unstable and if machine gets overloaded again while swapoff operation is still running and start using up RAM again, you may totally stall / crash machine. Therefore, be sure to do this only if really needed and can't wait for system do do it in "normal" way and when you are sure that for some time machine will not be needing whole RAM.

No comments: