Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

9 Answers 9

up vote 49 down vote accepted

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 the si (swapin) and so (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.

procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 0  0  78588 230788   9596  72196    0    0     0     0  543  652 12  6 78  0
 0  0  78588 230780   9596  72196    0    0     0     0  531  410  1  0 99  0
 0  0  78588 230796   9596  72196    0    0     0     0  300  335  1  1 97  0
 1  0  78588 230788   9608  72224    0    0    40     0  737  762  4  4 84  8
 5  0  78588 230788   9608  72224    0    0     0     0  415  385  9  3 84  0
 0  0  78588 230540   9616  72224    0    0     0    44  611  556 55  5 31  0
 0  0  78588 230532   9616  72224    0    0     0     0  574  662  1  6 89  0

Yet here in top you can see I have swap space allocated:-

Mem:    475236k total,   245076k used,   230160k free,     9720k buffers
Swap:   491512k total,    78588k used,   412924k free,    72476k cached
share|improve this answer
1  
“no reason to be concerned”, up to the point when I continue to use some application that was swapped out. then all hell breaks loose. –  törzsmókus May 13 at 12:14
6  
This post doesn't answer the question. –  emchristiansen Jun 25 at 20:45
2  
The premise of the question is based on misconceptions. This post addresses the misconceptions. –  popey Jun 26 at 10:11
    
I don't see a misconception, I see a disagreement about what "used" means. You are meaning "used" to refer to actively swapping in/out, while the original question appears to be posed with "used" meaning that there is data that has been swapped out, which may later need to be swapped back in. There are valid reasons to want to flush the swap after an event that swaps out a lot of other memory, essentially pre-empting the active swapping that the user knows will occur later (especially if the later 'de-swapping' is likely to occur while the disk is under load for other reasons). –  drfrogsplat Oct 10 at 2:09
    
After running a memory intensive application, my other applications are unrepsonsive when I switch to them, presumably due to being swapped. Some way of telling the system to un-swap as much data as possible (followed by a coffee break) would be better than having to wait 20s every time I Alt+Tab. –  Mark K Cowan Nov 1 at 11:18

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

sudo swapoff -a
sudo swapon -a

Be careful doing this, as you may make your system unstable, especially if its already low on RAM.

share|improve this answer
2  
That did the trick, the swap was copied to ram. I guess you just need to make sure to have at least the amount of swap used as free ram before running swapoff, right? –  jfoucher Aug 6 '10 at 8:22
5  
The swap was not copied to RAM. It was merely disabled. You should ensure the box is not swapping before doing this. If you do, then the kernel will see the memory disappear and the OOM-Killer will probably start killing off applications arbitrarily. There's no reason you should need to 'clean' out the swap like this. (see my other comment) :) –  popey Aug 6 '10 at 10:06
9  
Actually the swap is, basically, copied to ram, or more accurately, the pages are swapped back in from disk to RAM before the swap can be disabled. –  SpamapS Aug 7 '10 at 15:00
1  
The pages are copied to RAM only if they are still needed by some process. Sometimes a swapped page belongs to a process that has already been finished, and the kernel doesn't care to remove these pages from swap immediately. –  BrunoJCM Dec 30 '13 at 14:57

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.

share|improve this answer

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:

#!/bin/bash

free_data="$(free)"
mem_data="$(echo "$free_data" | grep 'Mem:')"
free_mem="$(echo "$mem_data" | awk '{print $4}')"
buffers="$(echo "$mem_data" | awk '{print $6}')"
cache="$(echo "$mem_data" | awk '{print $7}')"
total_free=$((free_mem + buffers + cache))
used_swap="$(echo "$free_data" | grep 'Swap:' | awk '{print $3}')"

echo -e "Free memory:\t$total_free kB ($((total_free / 1024)) MB)\nUsed swap:\t$used_swap kB ($((used_swap / 1024)) MB)"
if [[ $used_swap -eq 0 ]]; then
    echo "Congratulations! No swap is in use."
elif [[ $used_swap -lt $total_free ]]; then
    echo "Freeing swap..."
    sudo swapoff -a
    sudo swapon -a
else
    echo "Not enough free memory. Exiting."
    exit 1
fi
share|improve this answer
    
This was really helpful, thank you! –  Luke Cousins Sep 15 at 21:33

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:

The default setting in Ubuntu is swappiness=60. Reducing the default value of swappiness will probably improve overall performance for a typical Ubuntu desktop installation. A value of swappiness=10 is recommended, but feel free to experiment.

By changing this value to 10 or even 0, you can add a significant & perceivable speed boost to an older system with a slow drive. Note that setting this value to 0 does not turn swap off, it just sets it to the least aggressive swap setting and avoids hitting the disk until absolutely necessary.

I see no reason not to set this to 0 since anything that hits disk is slower than RAM. I have a 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:

The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk. Because disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory.

  1. swappiness can have a value of between 0 and 100
  2. swappiness=0 tells the kernel to avoid swapping processes out of physical memory for as long as possible
  3. swappiness=100 tells the kernel to aggressively swap processes out of physical memory and move them to swap cache

Below are basic instructions for checking swappiness, emptying your swap and changing the swappiness to 0:

To check the swappiness value:

cat /proc/sys/vm/swappiness

To turn swapoff (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.

sudo swapoff -a

To set the new value to 0:

echo 0 | sudo tee /proc/sys/vm/swappiness  

To turn swap back on:

sudo swapon -a
share|improve this answer

There is a common misunderstanding that having too much swap available will lead to performance degradation because the system will start to use it. Swap is only used when required, swapping out memory data which is not actively used (long time sleeping/blocked apps)) will leave more RAM for disk I/O caching and running apps. If the data is not being accessed SWAP is the best place to be.

share|improve this answer

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.

share|improve this answer

Simplest and the quickest way is :

free -h //to check swap usage

sudo swapoff -a sudo swap0n -a

share|improve this answer

There is a solution on the ArchWiki, which improves SpamapS' method by adding Memory check before empty the swap.

share|improve this answer
1  
It would be preferable to post a summary here, instead of just a link. Please edit your post accordingly. –  Scott Severance Dec 25 '11 at 16:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.