Ubuntu – Problem with swap in ubuntu 18.04

18.04memory usageramswap

I have Ubuntu 18.04 installed on my PC which having 8GB total memory and I allowed 2GB swap space when installing Ubuntu. When in normal operation it uses about 2GB from main memory and zero of swap.

My problem is, when I'm running a memory crunching process, It will start to use swap space after exhausting all the main memory available, but when I finished memory crunching process it will keep using swap space even if the main memory available in GB's. Due to usage of swap my PC getting slow after this operation. So this will lead to restart my PC to recover from it.

Is there a way to fix this, so that I can clear swap when main memory is available? Or is it a bug?

Best Answer

Once swap has been addressed, some swap will typically remain used even if after having run a memory consuming operation, you revert to your "normal" use of about 2 GB. After having used your memory crunching process, much of the other processes will have been swapped to the swap memory. These will be reclaimed to RAM as they are needed again by other activities. They will remain in swap as long as they are not needed. Typically, you should see that gradually swap use decreases, and the system becomes faster again.

There is a way to get more quickly back to normal. You can immediately "flush" all processes from SWAP back to main memory by turning swap off and back on. This requires the commands:

sudo swapoff -a
sudo swapon -a
Related Question