How much swap should I take for 1GB to 8TB of RAM on 14.04 or higher?
The short answer:
Set your swap file to:
round(sqrt(RAM))
if you don't use hibernationRAM+round(sqrt(RAM))
if you do use hibernation
Set your swappiness
to 10 on a desktop, but not on a server!
The long answer:
In the past:
The rule of thumb in use for the last 25 years has been a minimum of 1xRAM and maximum 2xRAM so that is what you'll see quoted all the time.
That minimum was set back in the stone age when I was a teenager and dinosaurs still roamed the Earth and because RAM was just too expensive and you absolutely needed that swap space to be able to accomplish anything.
The maximum was set at that time because of diminishing returns: it's just too slow to have to swap so much memory as HDD access is a factor of 1000 slower then RAM: good in an emergency, but not really good for everyday use! At the time, when you ran out of swap space, it was time to add more RAM! (which is still true today).
In the present:
If you do not use hibernation and your memory is in excess of 1GByte the new rule of thumb is
round(sqrt(RAM))
whereRAM
is obviously your RAM size in GB andsqrt
the square root. :-)If you use hibernation, you need to be able to swap the entire amount of RAM+already swapped RAM to disk, thus the formula becomes:
RAM+round(sqrt(RAM))
The rule of diminishing returns still holds today for the maximum, but unless you test your actual usage, taking 2xRAM is just a waste of disk space, so don't use the maximum Unless you run out of swap space using the other methodologies.
All of these together give you the following table: (last 3 columns denoting swap space)
RAM No hibernation With Hibernation Maximum
1GB 1GB 2GB 2GB
2GB 1GB 3GB 4GB
3GB 2GB 5GB 6GB
4GB 2GB 6GB 8GB
5GB 2GB 7GB 10GB
6GB 2GB 8GB 12GB
8GB 3GB 11GB 16GB
12GB 3GB 15GB 24GB
16GB 4GB 20GB 32GB
24GB 5GB 29GB 48GB
32GB 6GB 38GB 64GB
64GB 8GB 72GB 128GB
128GB 11GB 139GB 256GB
256GB 16GB 272GB 512GB
512GB 23GB 535GB 1TB
1TB 32GB 1056GB 2TB
2TB 46GB 2094GB 4TB
4TB 64GB 4160GB 8TB
8TB 91GB 8283GB 16TB
The above is just a rule of thumb; it's not the law of gravity!
You can break this rule (unlike the law of gravity) if your particular use case is different!
Pro tip: Always allocate SWAP at the start of a HDD as the heads need to move less on the inside of the disk.
Yes: On SSDs, it doesn't really matter any more where you locate the swap area as they use quantum-tunnelling instead of moving heads and modern SSDs use all of their memory cells (even the unallocated space) to prevent quantum degradation.
How to test if your usage of swap is different from the "generic" rule:
Just execute:
for szFile in /proc/*/status ; do
awk '/VmSwap|Name/{printf $2 "\t" $3}END{ print "" }' $szFile
done | sort --key 2 --numeric --reverse | more
which will give you a list of all running programs that are swapped out (with the one using the most swap space on top)
If you're using more then a few KB: resize to more then the minimum, otherwise, don't bother...
If you're on a server, stop reading now: you're all set!
If you're on a desktop/laptop client (not server), you want your GUI to be as responsive as possible and only swap when you really need to. Ubuntu has been optimised to swap early for server use, but on your client you want editing that huge 250 Mega-pixel raw picture in gimp
to be speedy, so setting your swappiness
to 10 will keep the kernel from swapping too early, while ensuring it doesn't swap too late:
If you have a sysctl.conf
file,
sudo nano /etc/sysctl.conf
OR
If you have a sysctl.d
directory but no sysctl.conf
file, create a new file:
sudo nano /etc/sysctl.d/35_swap.conf
and in both cases add:
# change "swappiness" from default 60 to 10
# (theoretically only swap when RAM usage reaches around 80 or 90 percent)
vm.swappiness = 10
to the end of the file, save the file (Ctrl+XY+Enter in nano) and execute a:
sysctl --system
to reload the parameter or take the Window$ approach and reboot... :-)
Necessity for swap
Swap is useful for 3 reasons:
Removing clutter from RAM. From the article on kernel.org:
"A significant number of the pages referenced by a process early in its life may only be used for initialisation and then never used again. It is better to swap out those pages and create more disk buffers than leave them resident and unused"
In other words, the data stored in RAM may only be useful when a process has been started (like the startup process), and then simply reside there in RAM, cluttering space which can be used for better purposes.
It expands the amount of memory that processes may use. This is well understood. You don't want the system to run out of memory and crash due to lack of it. For this specific reason, swap must exist , serving as a protective feature.
Hibernation: When a computer hibernates, the contents of RAM go to disk.
Swap amount
Knowing the purpose for swap and depending on your machine's RAM amount , you can choose a swap size that fits your purpose. There aren't specific guidelines, only rules of thumb which you can use to tailor your swap size to fit your system.
For instance, linux.com recommends:
A rule of thumb is as follows: 1) for a desktop system, use a swap space of double system memory, as it will allow you to run a large number of applications (many of which may will be idle and easily swapped), making more RAM available for the active applications; 2) for a server, have a smaller amount of swap available (say half of physical memory) so that you have some flexibility for swapping when needed, but monitor the amount of swap space used and upgrade your RAM if necessary; 3) for older desktop machines (with say only 128MB), use as much swap space as you can spare, even up to 1GB.
From personal experience, here's what I'd do:
Considering a machine with 1 - 2 GB that doesn't need to hibernate and for casual usage, you can have 1GB to 2GB swap space. My PC with 14.04 Ubuntu has 1GB of RAM and 2GB swap. The swap amount never crosses the 800MB border, and I use my PC only for surfing the web and scripting.
With a PC that has over 2GB and you don't need to hibernate, you may keep swap from 512MB to 1GB. I have a laptop with 15.04 which I don't hibernate ever and which has a swap file (not a partition) of 512 MB. RAM itself is 6 GB but doesn't cross 2-3 GB ever. Again: casual usage.
Cosmoscalibur and Fabby already addressed swap in case you need to hibernate, so I won't go into that too extensively. My humble opinion is this:
If you plan on hibernating, RAM contents will need to be swapped out, hence you need it at least the same size as your RAM.