Why does ubuntu use swap when there is enough free memory? [duplicate]

While running Virtual Box my system is accessing the hard drive a lot and running slowly.

When I checked system resources using System Monitor it shows processor usage at 15%, RAM usage at 65% and Swap usage at about 50%.

My question is why does ubuntu need to use Swap when there is sufficient amount of main memory available?

Edit -

Output of free command -

             total       used       free     shared    buffers     cached
Mem:       4075908    3056931    1018977          0       1944     308408
-/+ buffers/cache:    2746579    1329329
Swap:      4139004    1862551    2276453

Note - Virtual box seemed to work just fine before I upgraded my 12.04 Precise to 12.10 Quantal. Now its so slow its almost useless.


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.

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

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.

Source: SwapFaq - Ubuntu Wiki

Open the terminal and paste this in before you start virtualbox:

    sudo sysctl vm.swappiness=10

Start the visualization and check whether the swap usage has decreased, if that's the case then in the terminal do the following command:

    gksu gedit /etc/sysctl.conf

It will ask you for your password, type it in and a text editor will come up on the screen, scroll all the way down and after the last line add:

    vm.swappiness=10

Save the file and restart your computer.

That should do the trick!


But you don't have enough memory. In the first column, the amount of total memory available is 4075908. The amount of memory actually in use for programs excluding cache and buffers is 2746579. The amount of RAM in swap is 1862551.

This means that the amount of memory actually in use is 2746579+1862551 which equals 4609130. Generally, complete memory blocks and allocations to programs will be swapped out, these being the most efficient way of doing it.

Bottom line is, you have 4609130-4075908 or a 533222 Mb RAM requirement over what you physically have.

Have a look at how much memory you are allocating to the virtual machines that you are running at the time, and what the RAM requirements of other programmes that you may be running. Sometimes less is more - if you reduce the amount of RAM allocated to the virtual machines then you'll get no swapping, and performance will be much better.