How much SWAP space on a 2-4GB system?
Solution 1:
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'.
Solution 2:
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.
Solution 3:
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
Original table:
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
Solution 4:
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.
Solution 5:
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.