Swap Size
Why Do I Need Swap?
- Memory consuming programs - Sometimes, a large program make the entire system need extra memory.
- Hibernation - The hibernation feature (suspend-to-disk) writes out the contents of RAM to the swap partition before turning off the machine.
- Unforeseeable Circumstances - In these cases, swap can give you an extra delay to figure out what happened, or to finish what you are working on.
- Optimizing memory usage - Hard drives are considerably slower than RAM, so when you need a file, the Linux kernel reads the file into RAM and keeps it there, in cached memory.
- Optimizing Swap performance - Because swap space uses a disk device, this can cause performance issues. One way to reduce this problem is to have swap space on a different physical drive so that the competition for that resource is either reduced or eliminated.
Ubuntu
- For less than 1GB of RAM, it's highly recommended that the swap space should, as a base minimum, be equal to the amount of RAM, and maximum twice the amount of RAM
- For more modern systems (>1GB), your swap space should be at a minimum be equal to your physical memory (RAM) size if you use hibernation, otherwise you need a minimum of
round(sqrt(RAM))
and a maximum of twice the amount of RAM
Red Hat and CentOS
Red hat recommends setting as follows for RHEL 5/6:
- Systems with 4GB of ram or less require a minimum of 2GB of swap space
- Systems with 4GB to 16GB of ram require a minimum of 4GB of swap space
- Systems with 16GB to 64GB of ram require a minimum of 8GB of swap space
- Systems with 64GB to 256GB of ram require a minimum of 16GB of swap space
CentOS Linux 7.x and RHEL 7 recommends the following rules:
| System RAM | Recommended swap | With hibernation |
| --- | --- | --- |
| <= 2 GB | 2 x RAM size | 3 x RAM size |
| > 2 GB - 8 GB | Equal to RAM size | 2 x RAM size |
| > 8 GB - 64 GB | At least 4 GB | 1.5 x RAM size |
| > 64 GB | At least 4 GB | Not recommended |
Fedora
The Fedora 28 Installation Guide defines current thinking about swap space allocation:
- If RAM is less than 1 GB, swap size should be at least the size of RAM and at most double the size of RAM
- If RAM is more than 1 GB, swap size should be at least equal to the square root of the RAM size and at most double the size of RAM
- If hibernation is used, swap size should be equal to size of RAM plus the square root of the RAM size