53

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?

2
  • 3
    2-4 GB is not really high memory anymore 5 years later.
    – the
    Aug 7, 2014 at 17:03
  • @KasperSouren No, it is not. Was a fair amount at the time.
    – C. Ross
    Nov 23, 2015 at 0:31

12 Answers 12

54

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'.

7
  • 22
    Great answer, but you forgot just one thing. If it's a desktop or laptop, make sure that swap is at least as big as the RAM you have so you can hibernate your computer. This saves the RAM to swap. May 11, 2009 at 8:31
  • 4
    Great post. Sander beat me to the only thing I was going to add to it. I though of one additional item, if you have multiple drives, putting your swap on a drive that doesn't have the rest of your active file systems can improve performance as it won't have as many conflicts.
    – GreenKiwi
    May 23, 2009 at 15:57
  • as I found out this past summer, you need to have substantial swap space if you are running Java
    – warren
    Nov 10, 2011 at 21:40
  • If the user has an amount of RAM much larger than the amount that they will actually use (16GB/32GB) on a 100% uptime Desktop computer (no hibernation) then do you still suggest requesting processes to be put to SWAP? It seems to me there would be benefits of leaving everything on RAM unless you were ever nearing it's capacity.
    – earthmeLon
    Jun 3, 2012 at 2:59
  • 1
    @earthmeLon - I would suggest still having a little bit of swap. Disks are big and cheap these days, so the loss of a few GB of disk space to swap is negligible. Having some swap space gives you an insurance policy in case something eats up more RAM than you expect it to. Jul 30, 2012 at 23:10
17

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.

2
13

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 
8

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?

  1. 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.
  2. Allowing inactive tasks to be removed from RAM to make more room for disk cache
  3. 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.

5
  • swap space is also used if you run anything that utilizes the JVM: don't chintz and forget to allocate swap space if you're running Java!
    – warren
    Nov 10, 2011 at 21:37
  • @warren: I have Tomcat instances, each in excess of 3GiB, yet using no swap (and one is using hugepages, and thus can't be swapped). Not sure what you're talking about.
    – derobert
    Nov 10, 2011 at 21:58
  • there is a semi-obscure bug in the JVM related to minimum and maximum memory settings that I ran into this past summer (antipaucity.com/?p=984). TL;DR - every time the JVM asks for more memory from the OS, it writes itself out to swap, gets its new memory chunk, then reads itself back into RAM so that its memory is contiguous
    – warren
    Nov 10, 2011 at 22:15
  • @warren: OK, I'm running OpenJDK on 64-bit, with Xmx and Xms the same. But I'm not sure how java would force itself to swap, the only syscall close would be madvise, but that won't fail... that bug talks about running out of memory during garbage collection. Adding swap prevents that, of course, but so would adding RAM. Maybe its due to 32-bit with PAE?
    – derobert
    Nov 10, 2011 at 22:36
  • it happens with the 64-bit JVM, too: if you have Xms and Xmx not set to the same values, it can bite you badly: caused major issues for us for two+ weeks til we finally tracked it down
    – warren
    Nov 10, 2011 at 23:03
4

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.

2

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.

2

I always make as much swap (at least) as RAM. Just in case I want to hibernate the machine some day. Can work with less, but could as well not.

HDD space is cheap this days, therefore I don't hesitate giving away swap space.

2

Swap space can serve as "padding" when you would otherwise run out of memory immediately.

When a process consumes all of the available RAM and then some the machine will run very slowly, but you will usually be able to recover. It will also be easier to see which process is causing the problem.

1
  • 2
    This is a double-edged sword : on my home PC, I removed swap when I was using Firefox 2.x, because it was slowly eating all memory, and when the machine went swapping it became completely unusable; without swap, the firefox process was simply closed by the kernel :)
    – wazoox
    Jul 9, 2009 at 15:49
1

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.

1
  • Actually, I'd think that swap in the middle of the disk would be accessed the most rapidly.
    – Eddie
    May 7, 2009 at 17:00
0

I create 1GB swap and purchase more RAM the second its being used. You don't really need swap on modern computers, RAM is so cheap.

0

What are you using the system for? Personally I don't use any swap. If anything use a token 512MB or something just so things can get put to disk if resident but inactive for long periods.

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .