Ubuntu – How much swap should I take for 1GB to 8TB of RAM on 14.04 or higher

partitioningswap

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)

Best Answer

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