Lately I stamped upon some pages describing how to set up a swap on Linux machines. I was very surprised that still so many sources suggests to create a swap space equal to 2, 3 or even 4 times the amount of physical memory. Hell, let me share my thoughts about swap in Linux.

Main memory
Physical memory installed in machine, widely called as RAM.
Swap
Secondary memory storage, usually kept on disk, for extending machines virtual memory.
Virtual memory
Amount of memory that can be addressed by processes in the system. It is a sum of main memory (RAM) and swap. Process does not know where its memory pages are located – this is transparent to it.
Memory page
Memory in Linux is divided into pages. This is the smallest continuous block of memory that is managed by system.

Do you need a swap?

If I would be a lawyer I would answer: No, you don’t need a swap. Your system will work fine without it. And in general this is true, so why bother?

You must know that swap had a lot of sense when systems had low amount of memory. Memory was expensive back then. Nowadays, RAM is relatively cheap, hell, my phone has 4GB of memory and my laptop 20GB! In typical use cases you won’t need swap. What’s more, if you are doing some heavy computing that uses a lot of memory you also don’t want to rely on swap – it’s too damn slow. Buy more memory!

So when do you need it? I can think about few examples:

You must know that RAM is used not only for process specific memory, system also keeps caches and buffers there. That’s why you almost always have whole memory used at your linux box. Size of caches are scaled, so it will use free memory and if some process needs more, cache will be dropped. Thanks to buffering your box is faster – some data that would be normally read from disk resides in very fast memory. Therefore, it is wise to make some space for buffers. There are some processes that are run at machine startup and are not doing any relevant work for most of the time, they just use up your precious memory (for example, think about daemon that is responsible for backups run once a week). Linux can, and will, page out unused (but still valid) memory pages to swap guided by logic that if you will wait a little bit longer for this daemon to start its job then no one will die. In return it will gain some memory for caching/buffering or for applications. Pure profit. So yes, you would want to have a little bit of swap for that, but not as much as your physical RAM for God’s sake! 1 or 2 gigabytes will do, believe me.

There is one more swap usage that you might be interested. If you would like to hibernate your personal computer, then know that your physical memory is saved in swap file/partition. Linux is quite smart and it will use only such amount of swap as memory was declared in system. But if you want to make sure then you will be able to hibernate every time, then that’s a reason to set swap equal to your physical RAM plus a little bit more (look at previous paragraph).

For servers swap of 2 to 8 gigabytes seems to be reasonable. Servers are normally busy machines running some processes that are important to you. If something bad happens around, like bug in software (memory leak) or increased traffic that uses memory, swap will give you a chance for reaction (providing that you have some monitoring, of course). But don’t think that more swap will give you more time, that is not true. If your application is busy and that’s the reason of increased memory usage then heavy swapping will simply kill it. You gain absolutely nothing from the fact that your application exists in the system. It must do its work. And with heavy swapping it is most probably doing nothing, your users wait forever for something to happen and if you have a bad luck then it will be so loaded then you won’t be even able to login or make any significant action in reasonable time. Sometimes it is better to let Out Of Memory Killer do its work.

Swap sizes

Let’s do a quick summary on how big should be your swap.

  • 1-2 gigabytes for normal desktop usage.
  • Add your physical memory size if you want to be able to hibernate your system.
  • If you are thinking of a machine that acts like a server, go with 2-8 gigabytes instead of 1-2 from first point.

If you run a software that is supported – use sizes that are required in documentation or you will be changing this at the first time when you create a ticket.

Och, and always think! There is no golden rule in IT world.

Leave a Reply