How to decide on Swap size [duplicate]

I wanted to just confirm how much the size of Swap partition should be. Lots of places tell that the swap size should be twice the size of RAM. Somebody also told me that it shouldn't be more than 8 GB.If twice the size of RAM is more than 8 GB how big should the Swap partition be?


Solution 1:

The text below was obtained from the nixCraft page Linux: Should You Use Twice the Amount of Ram as Swap Space?. (Please follow the link and read the source for more information)

Note that nixCraft page cites text purportedly coming from the OpenBSD FAQ, but there is no matching text in the current document; this may however be something that used to be in there in 2006 (so the 2 x RAM rule was already obsolete more than 10 years ago):

Nonsense rule: Twice the size of your main system RAM for Servers

According to OpenBSD FAQ:

Many people follow an old rule of thumb that your swap partition should be twice the size of your main system RAM. This rule is nonsense. On a modern system, that's a LOT of swap, most people prefer that their systems never swap. You don't want your system to ever run out of RAM+swap, but you usually would rather have enough RAM in the system so it doesn't need to swap.

More from the nixCraft page:

Red hat recommends setting as follows for RHEL 5:

The reality is the amount of swap space a system needs is not really a function of the amount of RAM it has but rather the memory workload that is running on that system. A Red Hat Enterprise Linux 5 system will run just fine with no swap space at all as long as the sum of anonymous memory [memory mapping without file or device backing it: memory obtained from the OS stack and heap] and system V shared memory is less than about 3/4 the amount of RAM. In this case the system will simply lock the anonymous and system V shared memory into RAM and use the remaining RAM for caching file system data so when memory is exhausted the kernel only reclaims pagecache memory.

Considering that: 1. At installation time when configuring the swap space there is no easy way to predetermine the memory a workload will require, and 2. The more RAM a system has the less swap space it typically needs, a better swap space [might be along this recommendation for CentOS Linux 7.x and RHEL 7]:

Recommendation for Swap Space in RHEL/CentOS 7

And so the nixCraft page concludes:

Conclusion

If Linux kernel is going to use more than 2GiB swap space at a time, all users will feel the heat. Either, you get more RAM (recommend) and move to faster storage to improve disk I/O. There are no rules, each setup and configuration is unique. Adjust values as per your requirements. Select amount of swap that is right for you.

Furthermore, this Red Hat article: Do we really need swap on modern systems? (2017) says:

Our size recommendation for most modern systems is ‘a part of the physical RAM’, for example, 20%. With this, the painfully slow phase of operation in our example will not last as long, and the OOM kicks in earlier.

Of course, there are scenarios when different behaviour is desired. When aware of the behaviour, such swap configurations are ok, as well as running the system without any swap. Such a system is supported by us as well - but the customer should know the behaviour in the above situations.

Solution 2:

The only hard rule for swap space I know of is 1x RAM to Hibernate. The System Monitor or CLI free command will tell you how much swap space you're using with your particular hardware configuration and usage patterns. With 2GB of RAM my system rarely uses any swap space at all and never more than .4GB that I've seen.