I have 16GB RAM. Do I need 32GB swap?

I read many places that the rule of thumb for swap space is to double the amount of physical RAM. However, 32 GB does seem a LOT. Do I need that much? Do I need it at all with this high amount of physical RAM?


Solution 1:

Here's a very good recommendation from Red Hat: Recommended System Swap Space

An excerpt from the same link:

In years past, the recommended amount of swap space increased linearly with the amount of RAM in the system. However, modern systems often include hundreds of gigabytes of RAM. As a consequence, recommended swap space is considered a function of system memory workload, not system memory.

And another excerpt from an older version of the page:

However, given that swap space is usually designated at install time, and that it can be difficult to determine beforehand the memory workload of a system, we recommend determining system swap using the following table.

RHEL 8 table:

Amount of RAM    Recommended swap space       Recommended swap space 
in the system                                 if allowing for hibernation
--------------   --------------------------   ---------------------------
⩽ 2 GB           2 times the amount of RAM    3 times the amount of RAM
> 2 GB – 8 GB    Equal to the amount of RAM   2 times the amount of RAM
> 8 GB – 64 GB   At least 4 GB                1.5 times the amount of RAM
> 64 GB          At least 4 GB                Hibernation not recommended

At the border between each range listed in the table above, for example a system with 2 GB, 8 GB, or 64 GB of system RAM, discretion can be exercised with regard to chosen swap space and hibernation support. If your system resources allow for it, increasing the swap space may lead to better performance. A swap space of at least 100 GB is recommended for systems with over 140 logical processors or over 3 TB of RAM.

Original RHEL 5 table:

Amount of RAM in the System     Recommended Amount of Swap Space
4GB of RAM or less              a minimum of 2GB of swap space
4GB to 16GB of RAM              a minimum of 4GB of swap space
16GB to 64GB of RAM             a minimum of 8GB of swap space
64GB to 256GB of RAM            a minimum of 16GB of swap space
256GB to 512GB of RAM           a minimum of 32GB of swap space 

Solution 2:

It entirely depends on what you plan to do with the machine. For example if it were a Sap server then yes, I would add 32gb swap ( we have boxes at work with 128gb ram and 32gb swap ). If you were manipulating massive pictures and video then it makes sense to have a little swap. 32gb is probably overkill.

However I would not say zero swap.

In the unlikely event that you run out of RAM - perhaps opening a big file, perheps a long running tab in firefox, it doesn't matter, in that event your kernel OOM killer will kick in and start killing applications to get memory back. Under those circumstances it's entirely possible that you will lose data as applications get killed. However if you have a bit of swap then the system will carry on, grabbing swap and allowing the system to continue. System slows down as heavy swapping happens, you notice and investigate before all swap exhausted. Also disk is very cheap, so why not have swap?

Anyone who says "you don't need swap" without asking you what you're actually doing with your computer is making assumptions. Whilst you may well do very little with your computer that eats RAM, it's still best to ask the questions about what you're planning to do with it before making the rash judgement that you don't "need" swap. In my humble opinion

Solution 3:

Argh. The answers on this post are so very wrong, and this comes up as one of the first results in a Google search for "How much swap?"

First, a good point of reference is the Ubuntu Swap FAQ

This FAQ makes an important point that no one here mentions, and that is (emphasis mine)

Sometimes, a large program (like OpenOffice, Neverwinter Nights, or a video editor) make the entire system need extra memory. A significant number of the pages used by these large programs during its startup may only be used for initialization and then never used again. The system can swap out those pages and free the memory for other programs or even for the disk cache. In these cases, swap will be used to help the system handle any extra load.

So, swap is not just for hibernation! This activity, swapping out unused pages, can be beneficial in some scenarios and detrimental in others. Beneficial because it can free up memory for the system to use elsewhere, but detrimental when that swap out is too aggressive, and swaps out pages you actually do want. This is especially true on a desktop system where applications can sit idle for long periods of time, but the user still expects immediate performance when bringing it back to life. The control for how aggressive this is, is referred to as swappiness

The Ubuntu Swap FAQ discusses this lightly under What is swappiness and how do I change it? The default value on an Ubuntu system is 60, while the FAQ recommends a value of 10 for a desktop machine.

So, I would say that it is generally very important to not have a system with absolutely no swap space, as some people were recommending on this question, unless you know for certain very specific usage scenarios for your system and are very aware of the consequences it could have. (Personally, there is no situation where I would run a system without swap) In an Out of Memory scenario with no swap, the system will start killing processes (generally the one that caused the OOM)