2

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.

4 Answers4

8

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.

thomasrutter
  • 37,804
2

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.

psusi
  • 38,031
0

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).

0

If you have a fast cpu, try Zram.

You can read about it in detail here.

Sirajus Salekin
  • 1,737
  • 13
  • 29