HOWTO choose the right swap partition size

From LinuxReviews
Jump to navigationJump to search

Linux can swap applications to a disk partition or file known as swap when memory is full. There is also another important reason to have a swap partition of the right size: Hibernation. You must have a swap partition which is large enough to hibernate to disk and resume after the machine has been powered off.

The right size[edit]

Blond-anime-girl-with-red-questionmark.png

There used to be a simple rule of thumb which said that you could create a swap partition which is 2x your RAM size. This was true when RAM sizes like 128 MiB and 256 MiB were common.

The story is somewhat different on machines with 4 GiB or 16 GiB or more RAM. Your machine will simply become unresponsive if you have 16 GiB RAM and applications try to use 32 GiB.

So what is the right size for a swap on modern machines running modern Linux systems?

2/3 of system RAM is a good rule of thumb for swap partitions.

You should have a swap partition and it should be at least two thirds of system RAM if you want to hibernate to disk. You want this even if you suspend to RAM most of the time since hybrid hibernation combines the best of both suspend-to-RAM and suspend-to-disk. Linux will compress RAM when it hibernates to disk. However, compression is not always that great and it depends on the contents. Some recommend 1/2 the system RAM and that will be fine in the majority of cases. However, 2/3 (RAM*0.66) is a safer bet which will always work and never give you issues.

Avoiding disk swap use[edit]

Using zswap is better than disk swap when your machine runs into memory-constrained situations.

zswap will set aside a memory area which is used to compress the systems regular memory when the machine is low on memory. This is a lot faster than swapping to regular HDDs as well as SSDs. SSDs are fast but they are not as fast as system RAM.

zswap can be enabled in a system start-up script like /etc/rc.d/rc.local with:

echo 1 > /sys/module/zswap/parameters/enabled

and you should also echo a value to /sys/module/zswap/parameters/max_pool_percent which indicates what percentage of the available RAM should be set aside for zswap. 4 is a good value for machines with larger amounts of RAM like 32 GB, 10 is a good value for systems with lower amounts like 4 GB.

You should absolutely have a regular swap even though you use zswap because hibernation requires it.

Tuning swappiness[edit]

While unrelated to the topic at had the value /proc/sys/vm/swappiness is worth considering when you look at RAM and SWAP use. This value tells the Linux kernel how eager it should be when it comes to swapping applications in memory to disk. The default value is 60. This is NOT a percentage of system RAM or any concretely measured setting. It is defined by a range between 1 and 100 where 1 says swapping should be avoided at all cost and <code<100 says applications should be swapped out to disk almost immediately even if there is plenty of free RAM available. It is also possible to set it to 0 to disable swappiness entirely.

The default setting of 60 really is quite sensible. You probably do not need to change it. We simply mention this setting because many people are interested in it for some reason.

swappiness can be change by either echoing a value to /proc/sys/vm/swappiness or sysctl vm.swappiness=number. It can be permanently set creating a configuration file in the folder /etc/sysctl.d/. Permanently setting it to 50 can be archived by creating a text file ending with .conf in /etc/sysctl.d/ with the contents vm.swappiness = 50


Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.