17

I'm going to install 64 bit 14.04 on a new machine with something like 1 to maybe 128GB RAM and even more RAM later. How much space should I allocate to the swap partition?

And especially: WHY those recommendations for these numbers?

(None of the existing answers here and here explain any of the why and are a bit dated if we look at 1GB or RAM and up)

6
  • 6
    Possible duplicate of I have 16GB RAM. Do I need 32GB swap? There are answers from 2017 (or updated in 2017), so it holds currently more recent recommendations than this one.
    – Melebius
    Sep 4, 2018 at 6:05
  • 1
    @Melebius: I disagree: Although this question is newer, it is more generic than the one you're trying to duplicate to. This Q&A encompasses the older one, but the reverse is not true! ;-)
    – Fabby
    Sep 4, 2018 at 7:35
  • 1
    @Fabby In that case, shouldn’t the other be marked as a duplicate of this one?
    – Melebius
    Sep 5, 2018 at 6:39
  • 1
    @Melebius If both questions could be used by reviewers as duplicate questions of other questions in different situations, then both questions could be kept open to handle these different situations since both questions have highly upvoted answers.
    – karel
    Sep 5, 2018 at 8:11
  • 2
    @Melebius We've had a discussion on this topic in meta already and the community decided to let each stand as is.
    – Fabby
    Sep 5, 2018 at 11:39

2 Answers 2

36

The short answer:

Set your swap file to:

  • round(sqrt(RAM)) if you don't use hibernation
  • RAM+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:

  1. If you do not use hibernation and your memory is in excess of 1GByte the new rule of thumb is round(sqrt(RAM)) where RAM is obviously your RAM size in GB and sqrt the square root. :-)

  2. 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))

  3. 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... :-)

7
  • 1
    Thanks. What do you mean by "diminishing return"? If both the RAM and the swap are fully used, then it is impossible to hibernate even if the swap is 2*RAM size, isn't it? (Because hibernation will need the size of the swap to be RAM + swap, which is impossible?)
    – Tim
    Mar 9, 2015 at 0:11
  • Yes, but I've never had this happen yet... (and I'm old! therefore 2* RAM: When all of the RAM is swapped, you can still hibernate...
    – Fabby
    Mar 9, 2015 at 8:15
  • 2
    "Diminishing returns" just means that HDD access is a factor of 1000 slower then RAM, so if you need to swap out twice your RAM you'll have died of old age before it completes... It's just there to hibernate when you're already swapping...
    – Fabby
    Mar 9, 2015 at 8:28
  • 1
    @Fabby It's CV reviewing time on the original question again: askubuntu.com/review/close/889454. See Yufenyuy Veyeh Dider's comment under the question.
    – karel
    Oct 21, 2018 at 1:41
  • 1
    @mrkskwsnck: It's by the same author... ;-)
    – Fabby
    Dec 9, 2019 at 19:11
6

Necessity for swap

Swap is useful for 3 reasons:

  1. 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.

  2. 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.

  3. 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.

1
  • :-) Only noticed now you posted something. Edited and upvoted! (As ist now is a good answer after my edit! :D :D :D )
    – Fabby
    Oct 23, 2016 at 18:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .