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?
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'.
Red Hat recommends the following formula for servers with lots of ram:
if MEM < 2GB then SWAP = MEM*2 else SWAP = MEM+2GB
If your system has 1 GB ram, your swap would be 2 GB, for 16 GB it would be 18 GB.
Late answer, and I think this was pretty much covered in the selected answer, but there is some good and easily digestible info in the answer provided by @ssapkota here (copy/pasted below).
Here's a very good recommendation from RedHat:Recommended System Swap Space
An excerpt from the same link:
In years past, the recommended amount of swap space increased linearly with the amount of RAM in the system. But because the amount of memory in modern systems has increased into the hundreds of gigabytes, it is now recognized that the amount of swap space that a system needs is a function of the memory workload running on that system. However, given that swap space is usually designated at install time, and that it can be difficult to determine beforehand the memory workload of a system, we recommend determining system swap using the following table.
Current table (as of December 2012):
Amount of RAM in the system Recommended swap space Recommended swap space if allowing for hibernation --------------------------- ---------------------------- --------------------------- 2GB of RAM or less 2 times the amount of RAM 3 times the amount of RAM 2GB to 8GB of RAM Equal to the amount of RAM 2 times the amount of RAM 8GB to 64GB of RAM 0.5 times the amount of RAM 1.5 times the amount of RAM 64GB of RAM or more 4GB of swap space No extra space needed
Amount of RAM in the System Recommended Amount of Swap Space 4GB of RAM or less a minimum of 2GB of swap space 4GB to 16GB of RAM a minimum of 4GB of swap space 16GB to 64GB of RAM a minimum of 8GB of swap space 64GB to 256GB of RAM a minimum of 16GB of swap space 256GB to 512GB of RAM a minimum of 32GB of swap space
It depends quite a bit on what you're doing with it. With the appropriate workload, you don't actually need any swap space, whether you have 16MB or 16384MB of RAM; indeed most embedded Linux devices run without any (what would they swap to?)
Considering RAM prices, you're probably not going to use swap for active computation. So, what's left?
- On desktops, swap space is used for suspend-to-disk. This swap space needs to be around 1 × RAM, depending on how well the memory contents compress.
- Allowing inactive tasks to be removed from RAM to make more room for disk cache
- Same, but to allow active processes to use it instead for heap/stack
(2) and (3) heavily depend on workload. On my desktops, I can easily use 4GB+ on (2), from leaving vims, xterms, etc. running on other desktops.
On most of the servers I run, there is hardly ever anything swapped out and 1–2GB of swap (regardless of RAM amount) seems OK.
Well, it depends on what services and applications you plan to run. You can watch your memory usage with free -m and adjust your swap partition accordingly over time.
Here's an interesting discussion on the subject. Personally (and after reading that discussion) I'd still leave about 1 GB for swap.
I can't speak to Debian directly, but I know that when you get up above 2 GB of RAM, the original 2xRAM equation changes, usually down to 1xRAM. I think once you hit 16 GB RAM the suggestion lowers again to .75xRAM.
Of course, I found this true for Solaris, and is actually the metric used for Oracle installations, so YMMV depending on the apps you're going to be running on there.
Over the years I have followed the rule that for Windows machines you have as much swap as you have RAM...On Novell we would tune swap higher if we had more printers...For linux the rule and practice for me has been 1GB swap for every 2GB RAM you have. And you place it at the front of the disk so it is accessed quicker.
- If it is a mail/spam gateway I use double the swap to RAM
- If it is a * server I don't use swap at all
if it is a LAMP server the 1/2 rule is in effect.
Mileage will vary depending on application and GUI usage.
I just use a small token swap (usually 256 MB, but I have even done 64 MB) because configuring a system with zero swap needs special configuration.
Disk space is cheap, but slooooooooow. As soon as the system starts swapping, performance is zero. And RAM is no longer so expensive.
Trust me, buy a slightly slower processor and buy more RAM. 2.8 GHz instead of 3.0 GHz will save you enough money to buy another 2 GB RAM, at least.