Why should the size of the swap partition be double of the RAM size?

Swap partition having double the memory size is just a general guideline, not a hard and fast rule. You'll need minimum 1 X your memory size if you suspend-to-disk (hibernation), and add enough spare for usage of 'swapped' memory which goes beyond your physical one. Hence the recommended 1.5 to 2 times.

I would think it also depends on the nature of your application usage and size of your physical memory etc. If you have anything beyond 2GB RAM and using non-memory intensive applications most of the time, you might not need swap partition at all, not big one anyway.

Having a big swap file and slow harddisk can be really punishing for your system too.

Here are a few useful reads:

Ubuntu SwapFAQ
SUperuser - Swap partition size for 4GB RAM
Do I Need a Swap Partition and If So, How Big?


That's an old rule of thumb that I don't think makes sense anymore, because nowadays, that may equate to an absurd amount of swap in relation to the speed of the storage.

The minimum amount of swap you should have is your typical steady state usage. (That's the amount reached after long uptime, when most of your available memory consists of page cache. The defence of swap is a good read.)

But I'm here to tell you there's a maximum too … if responsiveness is a concern at all.

Desktop scenario: Typically, what happens when you run out of memory (such as when accidentally setting off a fork bomb like make -j) is this: First, you notice a bit laggy user interface, which means, you have roughly a minute to kill the offending process, before the mouse pointer becomes unresponsive. But that's only the start! You see, then, the computer is going to sit there with its SSD LED constantly lit, churning through the rest of its swap space, for tens of minutes to hours, while you can do nothing (except maybe pulling the plug). At this point, you are going to swear you wanted the OOM killer to kick in a bit sooner, instead of wasting your day.

So having a ridiculous amount of swap space that you don't normally use is actually bad from an availability standpoint — it just delays the inevitable in the event of a catastrophic memory leak (such that OOM is inevitable), during which time your computer is, for all practical purposes, unavailable.

I usually go for ½-2 GiB swap regardless of how much RAM the computer has.

Tags:

Linux

Unix

Swap