Linux – How Much SWAP Space for a 2-4GB System?

debianlinuxswap

How should I decide what size to make my swap on a new Linux machine (Debian) with 2-4 GB of RAM? Do I really need swap space?

Best Answer

There are lots of ways you can figure out how much swap use in a machine. Common suggestions use formulas based on RAM such as 2 x RAM, 1.5 x RAM, 1 x RAM, .75 x RAM, and .5 x RAM. Many times the formulas are varied depending on the amount of RAM (so a box with 1GB of RAM might use 2 x RAM swap (2GB), while a box with 16GB of ram might use .5 x RAM swap (8GB).

Another thing to consider is what the box will be used for. If you're going to have a huge number of concurrently running processes running on the box, but a significant number of them will be idle for periods of time, then adding extra swap makes sense. If you're going to be running a small number of critical processes, then adding extra swap makes sense (this might seem counter-intuitive, but I'll explain in a minute). If you're running a box as a desktop, then adding extra swap makes sense.

As for whether you should include swap, yes, you should. You should always include swap space unless you really know what you're doing, and you really have a good reason for it.

See, the way the Linux kernel works, swap isn't only used when you have exhausted all physical memory. The Linux kernel will take applications that are not active (sleeping) and after a period of time, move the application to swap from real memory. The result is that when you need that application, there will be a momentary delay (usually just a second or two) while the application's memory is read back from swap to RAM. And this is usually a good thing.

This allows you to put inactive applications to "sleep", giving your active applications access to additional RAM. Additionally, Linux will use any available (unallocated) RAM on a machine as disk cache, making most (slow) disk activity faster and more responsive. Swapping out inactive processes gives you more disk cache and makes your machine overall faster.

Lastly, let's face it, disk space is cheap. Really cheap. There's really no good reason at all not to swipe a (relatively) small chunk of space for swap. If I were running with 2GB - 4GB of RAM in a machine, I'd probably setup my swap space to be at least equal to the RAM. If it were less than 2GB of RAM, then I'd still go with at least 2GB of swap.

UPDATE: As an excellent comment mentioned (and I forgot to include), if you're running a laptop or a desktop that you might want to put in 'hibernate' mode (Suspend to Disk), then you always want at least as much swap as you have memory. The swap space will be used to store the contents of the RAM in the computer while it 'sleeps'.