Swap partition size on a 64 GB RAM computer for memory-intensive work

Solution 1:

You probably only need a small amount of swap. When you have sufficient RAM for your computer's typical working set, which I'm pretty sure you do, you only need swap for two things:

  1. You need swap to get information that will likely never be accessed out of RAM to free up more space for disk cache. Many applications run on system startup and will never be accessed again. You don't want any pages they dirtied stuck in RAM forever. So you need swap to hold them.

  2. You need swap to cover allocations that will never be filled. This space simply has to be available, even though it will not be used. Without it, the system will have to refuse to allocate memory even when it has plenty of free physical RAM because it has insufficient backing store to permit all of its allocations to be used at once.

Neither of these requires a large amount of swap. 16GB, for example, should be more than enough. The purpose is not to let you run bigger working sets at the cost of speed. The purpose is to let you use your 64GB effectively and not have to clog it with junk or reserve it for edge cases that will never happen.

(I agree with Bert that 4GB is quite likely to be sufficient.)

Solution 2:

RedHat recommends 4 GB on a machine with 64 GB.

However, sizing swap is more of an art than a science. It depends on what the machine is being used for, how much disk space and memory you have, and other factors. Remember, you can always add more swap later.

Using the 2X physical memory rule is outdated with the amount of memory systems have these days. But running with zero swap is not recommended unless you know what you are doing. The recommendation of 4 GB is a good starting point.

Solution 3:

On Linux, you need enough swap so that the total virtual memory available (RAM + SWAP) is enough for all the processes you want to run at once and their maximum virtual footprint.

If you have less swap than this, or no swap at all, then the following situation happens: the system runs out of memory trying to allocate a page. But, this is still a soft failure even though there is no swap, because the system has plenty of "victim" pages that can be removed in order to make room: namely, the pages of all file-backed memory mappings, such as executables and shared libraries!

As your system demands more and more space for data (which cannot be swapped out), it will increasingly evacuate the executable code (shared libraries and executables), leading to terrible thrashing, as the working set is trimmed into a tighter and tighter set of pages.

Swap space softens this problem by providing a place for anonymous (not file mapped) pages to be swapped out: the pages used for memory allocations, so that executable code can stay in memory.

Even so, if you don't frequently run memory-intensive tasks, you may be able to get away with running swapless most of the time, and manually configure a swap file (instead of a dedicated partition) when you need it. To make a swap file on the fly, become root and:

dd if=/dev/zero of=/path/to/swapfile size=$((1024 * 1024)) count=32768  # 32 Gb.
mkswap /path/to/swapfile
swapon /path/to/swapfile

When you don't need it any more:

swapoff /path/to/swapfile
rm /path/to/swapfile

Notes:

  1. You definitely do not need to configure at least as much swap as you have RAM. This rule of thumb dates back to operating systems where it was a hard requirement due to the way swapping was designed.

  2. There are ways to make Linux fail hard when no memory is available, namely via manipulating the values of these sysctl entries:

    vm.overcommit_memory
    vm.overcommit_ratio
    

Solution 4:

There are more considerations. If you need/want suspend to work then you need at least the size of your RAM and then some. However it sounds unlikely you need it given that you seem to mainly build a computational work horse.

In this case please consider using a swap file instead of a partition. You don't need to worry about sizing much, getting rid of or adding it later doesn't require any repartitioning. There is no (noticable) performance penalty using a file over a partition. If you ever happen to need it, have a look at the size and this will then also give you good hints.