When I open a RAM-intensive app (VirtualBox set at 2GB of RAM), some swap space is generally used, depending on what else I have open at the time.
However, when I quit that last application, the 2GB of RAM is freed up, but the same swap space use remains.
For example, right now, about two hours after having closed VirtualBox, I have 1.6GB free RAM and still 770MB in swap.
How can I tell Ubuntu to stop using that swap and to revert to using the RAM?
The Linux kernel underlying Ubuntu will automatically "swap in" those pages from disk to RAM as needed, so in general I'd say just let it happen naturally.
However, if you really feel that you need to force it, (I can see a scenario where you would want to know the system will be responsive later) you can momentarily disable and re-enable swap
OR alternatively as a single line
Be careful doing this, as you may make your system unstable, especially if its already low on RAM. Be sure to have enough free RAM in your system, or you might not be able to free the swap and the OOM-Killer would end up killing some of your processes.
Note that the
swapoff
may take a while. This is particularly true if you have lot of swap to be swapped off.Just because swap is allocated, doesn't mean it's being 'used'. Whilst programs like system monitor and top will show some of your swap space being allocated (in your example 770MB) that doesn't mean that the system is actively swapping in/out.
To find out if anything is swapping in/out you can use the
vmstat
command. Leave it running a few seconds to settle down and watch thesi
(swapin) andso
(swapout) columns. If nothing is happening then there is no reason to be concerned.Here's the output of running
vmstat 1
, where you can see my machine is not swapping at all.Yet here in
top
you can see I have swap space allocated:-You can also set your "swappiness" value from the default of 60, this way the swap won't grow so large to begin with. Why the shipping default is set to 60 when the recommended value is 10 perplexes me. From the Ubuntu SwapFAQ:
By changing this value to 10 or even 0, you can add a significant and perceivable speed boost to an older system with a slow drive. Setting this value to 0 does not turn swap off for Linux kernel 3.4 and below but with 3.5+ it does so you will want to use a value of 1 if you want to keep it on its lowest setting*.
I see no reason not to set this to 0 since anything that hits disk is slower than RAM. I have 8 virtual cores, a fast SSD & 8 GB of memory and my swap is set to 0. As of this moment I have 3 virtual machines running, my memory usage is 7.1 of 7.7 GB, my used swap is only at 576KB of 952MB and all systems are running smoothly!
From the Ubuntu SwapFAQ:
Below are basic instructions for checking swappiness, emptying your swap and changing the swappiness to 0:
To check the swappiness value:
To temporarily set swap to 0 (as suggested by SpamapS):
This will empty your swap and transfer all the swap back into memory. First make sure you have enough memory available by viewing the resources tab of gnome-system-monitor, your free memory should be greater than your used swap. This process may take a while, use gnome-system-monitor to monitor and verify the progress.
To set the new value to 0:
To turn swap back on:
To permanently set swappiness to 0:
sudoedit /etc/sysctl.conf
vm.swappiness = 0
sudo shutdown -r now
# restart system* With kernel version 3.5+ setting swappiness to 0 does turn it off entirely and a setting of 1 is recommended if you want the lowest swappiness algorithm. source: https://www.percona.com/blog/2014/04/28/oom-relation-vm-swappiness0-new-kernel/
I've found that emptying swap can help a lot on systems with slow disks and limited RAM. Of course, as already mentioned, the way to do this is to run
sudo swapoff -a; sudo swapon -a
. The problem here is that if there's insufficient RAM, doing so will cause all sorts of problems.I've written a script that I call
toggle_swap
that has worked for me for the last several years. It checks for enough free RAM before actually disabling the swap. Here it is:After mucking around with swappiness for a couple of days, I've come to the conclusion that the kernel should be left to its own devices. It knows what it's doing, and it's optimized to give you the best experience.
Unless you have a really good reason for wanting that disk back, I'd leave it be.
Tell the vm to not be so swappy, by resucing the swappiness, by
or even not be swappy as much as possible by
Please read the documentation of swapfile here: https://help.ubuntu.com/community/SwapFaq
It doesn't affect performance if your swap space is occupied. The only performance penalty is is stuff is going in/out of swap. If nothing is being swapped in/swapped out, then you don't have to worry about anything.