Swap partition size for 4GB RAM

I would say a good rule of thumb is indeed just as was mentioned above. 2 times the physical memory. Something to consider here, while it is possible to use a smaller swap partition, and it will suffice under most normal circumstances, if you want this system to be rock solid stable, I would indeed follow the 8 GB recommendation. In fact I recommend 2 * RAM + 1 MB so that there is absolutely room to swap out 2 entire copies of memory. This avoids the "shell game" scenario which can have negative performance repercussions. What this will do for you is guarantee a level of resiliency should you encounter an extraordinary event with your system.

I've seen scenarios where applications behave badly in unattended environments and before you know it, your system starts slowing down to a crawl.

Depending on what you are doing, you might even be able to dispense with the swap file entirely. The extra space for the OS is handy when running many applications at once. However if you only intend to run a few processes, do not intend to interact with the GUI disabling the swap file might be appropriate.

But if you are going to have a swap file I always use the sizing formula below.

[(2 x  RAM) + 1 MB] = Swap File Size

I also recommend putting your swap file on a seperate disk whenver possible as this will increase performance as the OS can swap in and out at the same time as read/writes from the data disk.

I hope this is helpful.


Linux can operate quite happily with no swap if you have enough RAM for everything you ask it to do, though it is still a good idea to have some swap as it can sometimes gain better performance when close to filling memory by pushing some not-recently-used stuff out to disk freeing up RAM to use as cache/buffer for currently active IO operations.

Also, having some swap, no matter how much RAM you have, does allow a little extra safety net should an app misbehave and need more RAM than usual, or you choose to do something unusual that needs a lot of memory.

In general I don't agree with 2xRAM on modern machines. If you have enough going on to end up with 8Gb worth of pages to swapped out to disk then your system is likely to have ground to a completely unusable speed by that point anyway. Even 1xRAM can be overkill in many cases IMO. Remember that you can add swap space in the form of a file at any time, so you can add more swap later if you find you need to. for example, to add an extra GB of swap:

dd if=/dev/zero of=/path/to/newfile bs=1024 count=1048576
mkswap /path/to/newfile
swapon /path/to/newfile

and to remove it (once no longer needed):

swapoff /path/to/newfile
rm /path/to/newfile

(remember though that swapping to files is a little slower than direct to to a partition or logical volume as you have filesystem overhead and greater potential for fragmentation to consider)

Hibernation, i.e. on laptops/netbooks, may complicate things a little as IIRC Linux stores memory state in the swap space, so you definitely need more then your RAM size as swap if you plan to use hibernation (and I don't know if that needs to be partition/volume based swap or if file based can be used - it will certainly need to be something mentioned in fstab that can be seen very early in the boot process whic the inital ram disk is in use and your real root not yet mounted).

These points may all be moot though. If you have terabytes of disk storage, allocating 8Gb to swap is likely to be very little hardship!