Linux: Should You Use Twice the Amount of Ram as Swap Space?

Linux and other Unix-like operating systems use the term “swap” to describe both the act of moving memory pages between RAM and disk and the region of a disk the pages are stored on. It is common to use a whole partition of a hard disk for swapping. However, with the 2.6 Linux kernel, swap files are just as fast as swap partitions. Now, many admins (both Windows and Linux/UNIX) follow an old rule of thumb that your swap partition should be twice the size of your main system RAM. Let us say I’ve 32GB RAM, should I set swap space to 64 GB? Is 64 GB of swap space required? How big should your Linux / UNIX swap space be?

Old dumb memory managers

I think the ‘2x swap space’ rule came from Old Solaris and Windows admins. Also, earlier memory managers were very badly designed. There were not very smart. Today, we have very smart and intelligent memory manager for both Linux and UNIX.

Nonsense rule: Twice the size of your main system RAM for Servers

According to OpenBSD FAQ:

Many people follow an old rule of thumb that your swap partition should be twice the size of your main system RAM. This rule is nonsense. On a modern system, that’s a LOT of swap, most people prefer that their systems never swap. You don’t want your system to ever run out of RAM+swap, but you usually would rather have enough RAM in the system so it doesn’t need to swap.

Select right size for your setup

Here is my rule for normal server (Web / Mail etc):

  1. Swap space == Equal RAM size (if RAM
  2. Swap space == 2GB size (if RAM > 2GB)

My friend who is a true Oracle GURU recommends something as follows for heavy duty Oracle server with fast storage such as RAID 10:

  1. Swap space == Equal RAM size (if RAM
  2. Swap space == 0.50 times the size of RAM (if RAM > 8GB)

Red Hat Recommendation

Red hat recommends setting as follows for RHEL 5/6:

The reality is the amount of swap space a system needs is not really a function of the amount of RAM it has but rather the memory workload that is running on that system. A Red Hat Enterprise Linux 5 system will run just fine with no swap space at all as long as the sum of anonymous memory and system V shared memory is less than about 3/4 the amount of RAM. In this case the system will simply lock the anonymous and system V shared memory into RAM and use the remaining RAM for caching file system data so when memory is exhausted the kernel only reclaims pagecache memory.

Considering that 1) At installation time when configuring the swap space there is no easy way to predetermine the memory a workload will require, and 2) The more RAM a system has the less swap space it typically needs, a better swap space

  1. Systems with 4GB of ram or less require a minimum of 2GB of swap space
  2. Systems with 4GB to 16GB of ram require a minimum of 4GB of swap space
  3. Systems with 16GB to 64GB of ram require a minimum of 8GB of swap space
  4. 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:

Fig.01: Recommended System Swap Space For Linux

Swap will just keep running servers…

Swap space will just keep operation running for a while on heavy duty servers by swapping process. You can always find out swap space utilization using any one of the following command:
cat /proc/swaps
swapon -s
free -m
top

See how to find out disk I/O and related information under Linux. In the end, you need to add more RAM, adjust software (like controlling Apache workers or using lighttpd web server to save RAM) or use some sort of load balancing.

You can rfer Linux kernel documentation for /proc/sys/vm/swappiness to fine tune swap space. It is also possible to add a swap space on Linux without creating a new partitions.

A note about Desktop and Laptop

If you are going to suspend to disk, then you need swap space more than actual RAM. For example, my laptop has 1GB RAM and swap is setup to 2GB. This only applies to Laptop or desktop but not to servers.

Kernel hackers need more swap space

If you are a kernel hacker (debugging and fixing kernel issues) and generating core dumps, you need twice the RAM swap space.

Conclusion

If Linux kernel is going to use more than 2GiB swap space at a time, all users will feel the heat. Either, you get more RAM (recommend) and move to faster storage to improve disk I/O. There are no rules, each setup and configuration is unique. Adjust values as per your requirements. Select amount of swap that is right for you.

What do you think? Please add your thoughts about ‘swap space’ in the comments below.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
74 comments… add one
  • Anand Jun 11, 2017 @ 4:32

    Swap is good, and should only be defined with Hard disk capabilities.

    Similarly, like ORACLE guru mentioned, it most important, that movement of data/data from your DMA, CPU and RAM.

    The calculation is dependent on Motherboard specs, RAM size and its frequency and finally on capabilities of hard disk.

    Solid State Drives with very small size upto 16 GB or 32 GB, can only be used with combination and swapping between general Hard drive and SSD + RAM.

    My experiment, was ad-hoc, but it was fruiteful and I am using for setup with private clould with Hyper-V and Oracle-VC.

    Thank You,

  • Mikko Rantalainen Nov 21, 2016 @ 10:16

    In my experience, your swap size should depend on what you do with the system and how long you’re prepared to wait for things to happen in worst case.

    (1) You’re kernel developer and willing to wait for hours to not lose any debug info: you need at least 2x the RAM of the system.
    (2) The system is a typical server: if you’re constantly swapping you need to purchase more RAM. Swap should be reserved for purely memory leaks and stuff that’s needed less often than daily. The swap usage depends heavily on workload and software quality. Usually around 4GB or less is a sane value.
    (3) The system is a workstation or a normal desktop: the same rule applies as to server (case 2) but you need to limit swap partition size to up to the maximum wait time in worst case scenario. For example, if your swap device has random 4KB read performance around 10 MB/s (most real desktop devices are going to *slower*) and you’re willing to wait 60 seconds if the system is getting slow before hitting the hard reset button your maximum swap partition size should be 600 MB. Exception: if you want to use “hibernate to disk” you’ll need at least size of the RAM plus the amount of swap you normally use. If you’re willing to tweak the “suspend to disk” scripts, you may create two swap partitions and disable the one that is used for suspend to disk while the system is running. Enable that partition on-the-fly via scripts if suspend to disk is requested.

  • King Stuart Dec 19, 2015 @ 0:35

    Indeed it is instructive that if swap is less than the totality of system RAM, then ostensibly whence a hibernation even becomes occurring, the hibernation scheme may fail. Thus, I always tell people to use the following minimum formula:

    (RAM Size in GB * (2^30)) + (2^29)

  • Willie M Apr 29, 2015 @ 1:56

    I have 4GB of RAM and had a 8GB swap space. My computer ran like crap. Now that I change it to 4GB it runs a lot better. So I believe what you are saying on this blog!

  • QuyetDX May 20, 2014 @ 13:32

    How much minimal size can swap space be allocated? Is it equal to page size or somewhat?

  • DG Jan 24, 2014 @ 23:42

    I’m amazed, years after the initial question was posed, how little *science* has been presented amidst scores of “disks are cheap” and “rule of thumb” comments utilizing logical arguments valued around the same level as divination based on entrail-readings… Basically the only logic-based explanations I see here relating RAM to swap space in a modern computing environment are those related to hibernation. The “2xRAM” rule, in the absence of hibernation requirements, seems obviously bogus as illustrated simply by the imaginary scenario of a system with 16GB RAM and 32GB swap, which swaps rarely but occasionally, and, with no alteration to workload, is treated to an upgrade to 24GB RAM. As the existing 32GB of swap should be sufficient even for hibernation, and workload, as stated, does not increase, by what logic should the extant swap space be expanded to 96GB (as the 2xRAM rule would dictate)?

    Throwing an additional drive on your little media center box is one thing, but what about Linux systems in the Real World doing Real Work — provisioning additional RAM for 100 VMs in an ESX cluster can be done on-the-fly without a reboot trivially, expanding each system’s drive and repartitioning, or adding additional drives, while admittedly scriptable, is not nearly so trivial. This is where having some Real Science behind the decision would be most welcome.

  • great white May 3, 2013 @ 13:14

    If a use a laptop, there is some more space needed 4 example 4 hibernation?
    Thus swap = RAM + Space 4 hibernation
    Am I correct?
    i.e. If I have 4GB of RAM in that situation 2GB for swap (min) + 4GB to store hibernation of RAM. Thus I use 6GB linux swap file for any case, especially for I use really RAM voracious math applications that create many huge matrixies. When going sleep needed to stop working without loosing data thus thats the only way be sure to save a present session with data and pending calculations.

  • john Jan 29, 2013 @ 20:48

    Can someone advise on this ?
    I have a VPS with 1 GB of Ram available. At the same time there is a 256MB of SWAP space available, which is always running almost at 99% of usage, while RAM usage is about 60% and the CPU usage at 0,5. I am getting very often server downtimes with the server throating 500 error.
    Should the swap space be higher ? It is possible that there are other misconfigurations ?

  • Ramel.d Oct 29, 2012 @ 16:32

    2x swap space is not necessarily nowadays. Having sufficient RAM and little swap space is good enough for the server to run all of its processes. To stop processes from using swap space we need to install more RAM. Having more RAM, makes the swap space less important in the system.

  • Mathew Aug 15, 2012 @ 20:55

    If I have solid-state-device and hard disks, what is best place for performance to make a swap space. Machine has 64 Gb RAM and 128 Gb solid-state device. Should I be using the solid state device for swap space or it is a total waste?

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.