How big should the swap partition be?

Solution 1:

On large systems, 8GB of physical RAM, we usually allocate 2GB of swap. These are loaded database servers running Oracle or PostgreSQL. For years, I've never seen swap hit even under heavy load. Heavy load is roughly 100 to 150 users doing around 10,000 SQL reads, and maybe 2,500 writes per minute.

We also adjust the swapiness level up to discourage swapping and we turn off unneeded processes we know we don't need running. (Custom Linux builds)

Like Eddie, says above, 1x physical RAM is a good rule of thumb for anything under 4GB. I do custom Linux builds for a large, Fortune-500 company and these are the things we typically do, and we haven't had any issues in over the last 5 or so years I've been consulting for them.

On larger systems: 64bit Linux with 32GB and 64GB of RAM, these are our Oracle database servers, and we typically keep 2GB of swap around for things like Eddie points out above - idle processes will be moved to swap depending on your swapiness settings.

Solution 2:

A seemingly timeless question. The good folks over at SlashDot discussed this a while ago. Check out what they had to say:

  • Disk space is not a problem with modern HDDs, 16GB is still a trivial amount for modern TB drives.
  • However, significant swap space can be a problem in case of poor memory management:

If I watch a 4 GB movie over a period of 2 hours, a lot of memory managers will decide that attempting to cache all of that data might be a good thing. Halfway through the movie, it will think that all those other running programs have been unused for an hour and can be safely swapped out in favor of caching more of that 4 GB file. The end result is that half your programs are swapped out after watching a movie, resulting in a sluggish system that is trashing all over the swap file.

  • In the end, the right decision should come from memory requirements, not ballpark formulas:

If you have [enough RAM to run the programs you want to use on a daily basis], then there's no reason to ever use swap.


Solution 3:

See the answer How much SWAP space on a high memory system? for some guidance, although your question is more general. Opinions vary widely on this, but generally people with 1 GiB of RAM or more seem to either run without swap entirely, or with 1xmemory as swap.

Even if you don't plan on using so much RAM that you'll force swapping to occur, there's some value in having at least 1xmemory as swap: It allows the operating system to swap out some things that aren't expected to be used immediately so that it can instead use that memory for disk or IO buffers, for example.


Solution 4:

If you expect your system to be able to hibernate and then resume successfully :-) , it's probably better to allocate more space for the swap partition than the amount of physical RAM


Solution 5:

My VPS provider doesn't even provide swap space for the servers, and mine's been running along fine without it.

Most servers I see on a daily basis at work are swap-less also.

Edit

However - if you are running a JVM, make sure you have at least as much swap space as you will ever set -Xmx because of a known issue with the JVM.

In short, when the JVM needs more memory than it's currently using (but still has not hit its Xmx), it:

  • swaps itself to disk
  • allocates new memory
  • reads itself back into RAM

This means that if you swap is less than your Xmx, it can fail.

To quote the bug report

The best thing to do is increase swap size on the machines encountering this error.

I try to play it safe when working with JVMs and set swap to equal physical memory.