I ran a process which used up mostly all my RAM and Swap... I was forced to kill the process, but now I see something strange, my Swap usage has decreased a little bit, but it is still half used while my RAM is barely being used at all... In fact quite a lot more Swap is being used than RAM, and even though the RAM usage level is low, my computer is being really slow and I am assuming that this is linked to the Swap levels still being high? If so, how do I clear my Swap if it doesn't automatically? Or what do I do? I have already looked at this question, it does not appear to only be talking about Swap and I don't want to affect the RAM, so a solution would be clearing Swap, but only Swap...

Last time this happened I was forced to just restart my machine, but I should have to. I am running Ubuntu GNOME 15.10 with GNOME 3.18.


Once data goes into swap, it is normal for it to stay there even once your memory starts to free up again. It is a good thing and there is no need for concern.

Due to hard disk access being significantly slower than memory, your system will avoid swapping data in or out of swap when it doesn't think it is necessary for system performance or stability. So, data will only go into swap when the system is running out of free memory or there is a lot of pressure on the disk cache, and your system decides the time-consuming process of placing data into swap will pay off with better or more reliable system performance afterwards.

Transferring data out of swap is (for traditional hard disks, at least) just as time-consuming as putting it in there. So your system will be reluctant to remove data from swap if it's not actually being used. If you have data in swap and it's not being used, then it's actually a good thing that it remains in swap, since it leaves more free memory for other things that are being used, potentially speeding up your system.

The best way to completely avoid swapping is to buy more physical RAM if that's possible. When it swaps, your system is giving you the best performance it can with the amount of physical memory it has.

All that said, if you are sure that the low-memory/high cache event that caused the swap in the first place won't happen again and you don't mind waiting, you can force the system to release all of its swap data with the following commands.

sudo swapoff -a
sudo swapon -a

This will disable then re-enable swap, forcing data out of it in the process. As explained, there is probably no real reason to do this - beyond curiosity or seeing how your swap is working.

  • So when my Swap stays at like 90% used, and my RAM is at 10%... Why does my computer become incredibly slow? – user364819 Apr 12 '16 at 10:55
  • Two possibilities. One is that the slowness is unrelated, which I suspect to be the case. Another is that whatever event that caused all that data to be pushed to swap put so much pressure on available memory that your system was forced to push some more active data to swap just to avoid killing processes, and that is now being swapped back because it's needed. That slowness would only last a short time though if that's the case, and it can still be attributed to whatever event used up all your RAM in the first place. – thomasrutter Apr 13 '16 at 0:44
  • I am sure that it is related because I have been running some tests and my computer is always fine and fast except when I have Swap usage, and more Swap is used, the slower it gets... And it lasts seemingly until I restart my machine and Swap is cleared, so it is rather strange, is there no way of further analysing this or seeing what is actually going on? – user364819 Apr 13 '16 at 9:35
  • Try to think of the swap usage as your system's way of responding to the problem rather than the cause of it. The root cause will be whatever memory or cache-intensive event caused the swapping to occur (you haven't given much info about this) - fix that or get more physical RAM. To see how your system would perform were it not for swap, try disabling swap and running that problem process again. Assuming it does the same thing, it may crash sooner, and if it doesn't you'll possibly get the same or more slowness. – thomasrutter Apr 14 '16 at 1:51
  • I ran a command giving a huge number to bc, then when it became unresponsive and used up all my RAM and a lot of my Swap, I killed it, the RAM went down to normal levels, but the slowness stayed and so did the levels of Swap usage. – user364819 Apr 14 '16 at 9:58

The slowness will quickly clear up on its own as the swapped out data is brought back in when it is needed. It won't be swapped out again as long as you have plenty of free ram, but plenty of other data that you have not accessed might still remain in swap, waiting for you to actually need it again.


If you have a fast cpu, try Zram.

You can read about it in detail here.


Clearing swap would do nothing for the slowness of your computer and does not need to be done except if you believe that some incorrectly behaving program has kept some sensitive data (e.g. passwords) in swappable memory that may have ended on the swap partition. If you don't need to wipe potentially sensitive data out of swap, you never need to manually clean or format the swap once it's working.

The most probable reason your system feels slow is that the system is in fact going nearly out of RAM. This may happen even if the system monitor shows that the system still has available memory because memory need is often sporadic and the memory may be needed only for a less than a second and will not show up in any slow refreshing system monitor.

The most simple way is to open a terminal and run command sudo vmstat -SM 10 (you may need to install vmstat first by running sudo apt install vmstat):

Output will look something like this (new line appears every 10 seconds):

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0   1609   6565  13230    0    0    88   195   17  178 44 10 45  1  0
 1  0      0   1599   6565  13232    0    0     0   226 4621 18688 22  6 72  0  0
 4  0      0   1566   6565  13234    0    0     0    67 5217 20819 27  7 65  1  0
 2  0      0   1533   6566  13241    0    0     0   847 6021 23359 35  6 58  0  0

The fields you want to watch are swap si and so which means number of megabytes in 10 second period that are swapped in (from swap device to RAM) or swapped out. Usually the so does not matter for system speed but si will cause slowness and is caused by system having too small RAM for the processes that you're trying to run in paraller. The possible solutions are (1) close one or more applications that you're using, or (2) get more physical RAM.

Press Ctrl+C to exit vmstat.

Note that vmstat is not able to tell if system goes near zero free memory for short moments so free, buff and cache may have big numbers even if memory is slow. sar is probably the only program that can tell for sure if your memory is running out but that program is pretty hard to configure (not impossible, though - just install it and start with man sar).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy