What's the right amount of swap space for a modern Linux system?

Complete our survey and voice your opinion on how much swap space to allocate.
168 readers like this.
How to find files in Linux

Lewis Cowles, CC BY-SA 4.0

Swap space is one of those things that everyone seems to have an idea about, and I am no exception. All my sysadmin friends have their opinions, and most distributions make recommendations too.

Many years ago, the rule of thumb for the amount of swap space that should be allocated was 2X the amount of RAM installed in the computer. Of course that was when a typical computer's RAM was measured in KB or MB. So if a computer had 64KB of RAM, a swap partition of 128KB would be an optimum size.

This took into account the fact that RAM memory sizes were typically quite small, and allocating more than 2X RAM for swap space did not improve performance. With more than twice RAM for swap, most systems spent more time thrashing than performing useful work.

RAM memory has become quite inexpensive and many computers now have RAM in the tens of gigabytes. Most of my newer computers have at least 4GB or 8GB of RAM, two have 32GB, and my main workstation has 64GB. When dealing with computers with huge amounts of RAM, the limiting performance factor for swap space is far lower than the 2X multiplier. As a consequence, recommended swap space is considered a function of system memory workload, not system memory.

Table 1 provides the Fedora Project's recommended size for a swap partition, depending on the amount of RAM in your system and whether you want enough memory for your system to hibernate. To allow for hibernation, you need to edit the swap space in the custom partitioning stage. The "recommended" swap partition size is established automatically during a default installation, but I usually find it's either too large or too small for my needs.

The Fedora 28 Installation Guide defines current thinking about swap space allocation. Note that other versions of Fedora and other Linux distributions may differ slightly, but this is the same table Red Hat Enterprise Linux uses for its recommendations. These recommendations have not changed since Fedora 19.

Amount of RAM installed in system Recommended swap space Recommended swap space with hibernation
≤ 2GB 2X RAM 3X RAM
2GB – 8GB = RAM 2X RAM
8GB – 64GB 4G to 0.5X RAM 1.5X RAM
>64GB Minimum 4GB Hibernation not recommended

Table 1: Recommended system swap space in Fedora 28's documentation.

Table 2 contains my recommendations based on my experiences in multiple environments over the years.

Amount of RAM installed in system Recommended swap space
≤ 2GB 2X RAM
2GB – 8GB = RAM
> 8GB 8GB

Table 2: My recommended system swap space.

It's possible that neither of these tables will work for your environment, but they will give you a place to start. The main consideration is that as the amount of RAM increases, adding more swap space simply leads to thrashing well before the swap space comes close to being filled. If you have too little virtual memory, you should add more RAM, if possible, rather than more swap space.

In order to test the Fedora (and RHEL) swap space recommendations, I used its recommendation of 0.5*RAM on my two largest systems (the ones with 32GB and 64GB of RAM). Even when running four or five VMs, multiple documents in LibreOffice, Thunderbird, the Chrome web browser, several terminal emulator sessions, the Xfe file manager, and a number of other background applications, the only time I see any use of swap is during backups I have scheduled for every morning at about 2am. Even then, swap usage is no more than 16MB—yes megabytes. These results are for my system with my loads and do not necessarily apply to your real-world environment.

I recently had a conversation about swap space with some of the other Community Moderators here at Opensource.com, and Chris Short, one of my friends in that illustrious and talented group, pointed me to an old article where he recommended using 1GB for swap space. This article was written in 2003, and he told me later that he now recommends zero swap space.

So, we wondered, what you think? What do you recommend or use on your systems for swap space?

Tags
David Both
David Both is an Open Source Software and GNU/Linux advocate, trainer, writer, and speaker. He has been working with Linux and Open Source Software since 1996 and with computers since 1969. He is a strong proponent of and evangelist for the "Linux Philosophy for System Administrators."

38 Comments

I'm a bit old fashion, as I still support the idea of twice the physical RAM. That normally works as I aim for 8 GB for a two core system, which most of mine are. Disk space is even cheaper than memory, so I always have several 16GB partitions on the drive, one each for root, /var, /tmp, /usr and swap. That keeps it simple and easy to remember. The only time I run into trouble is when I try to use pan2 for Usenet. Even with that much swap space, it can't handle the retention on NewsDemon. It once ran for 84 hours and used up all of the swap space downloading headers before it crashed with a segmentation fault.

Unless I plan to over commit on the number of apps I run, I don't use swap space. As for hibernation, I never turn my systems off.

You have to aim for 4GB total. Most systems use between 500MB system ram + 1.5GB browser ram, to 1,2GB system RAM, and 1,5GB browser RAM.
In the latter, that leaves about 256MB of shared Vram.
If you optimize your system a bit, on a low end system, you can run:
<2GB ram = 1-2GB swap
4GB RAM = 0-1GB swap
>4GB RAM = 0MB swap

Windows only needs swap to restore from system crashes, which is about 50-150MB for kernel swap.

But Swap files are extremely bad for SSDs. They wear out the SSD quickly. So it's better to not have swap nor hibernation files, as most low end hardware usually has pretty good sleep states.

On systems with low amounts of RAM I'd recommend looking into zswap/zram. If you have enough RAM, in my opinion you don't need any swap at all.

I don’t use swap with my system because have SSD disks.

Zero is in the radiobutton list before voting, but does not appear in the results.

My rationale for zero: I always buy enough RAM and my biggest workload, RAM wise, is the browser or some games. And in the latter, lower texture quality performs a lot better than running into swap.

Exceptions are systems with insufficient RAM (<8G in my case), or when hibernation is desired given this uses swap.

Another thing is that when you must have swap, at least make sure it is encrypted, as otherwise it defeats the purpose of any encryption you may use. Getting this to work with hibernation is left as an exercise to the reader.

On Linux — no less than about size of RAM: To have suspend-to-disk! Usually you give a little more, for lack of a precise estimate of space requirements — which actually should be present in somebody's mind, and a great thing to learn about...

I think that it depends on your environment.
In my case, I have a lot of VMs. The majority of the VMs are either web servers or app servers. Given that we know exactly what's running inside (JVM with limited heap size, httpd, monitoring tools, ...), we decided to disable swap space and correctly size our RAM.
And I think that in a virtual environment, you already have the overhead of the hypervisor. And as a result swap in such an environment does not speed up things much. So, if you can, it is better to rely on RAM.
On a physical server, swap space may have more sense.

This is not something worth spending much thought about. When I have looked at what my system is actually using with a monitoring tool, swap only rarely comes into play.

It really depends on your usage and context.

Way back in the old days of 486/Pentium systems, I would never create more than 64MB of swap. Despite recommendations of 2x RAM, I thought it was crazy to create that much swap on systems with 128MB or more. Given the slow speed of hard drives, that much swapping, regardless of RAM size, would cripple performance. I always said "if you need more than 64MB of swap, then you really need more RAM".

I maintain a similar philosophy today, but with larger numbers due to much much larger RAM sizes and much much faster storage. I will typically allocate 2-4GB of RAM to swap, primarily because I'd rather suffer a performance hit (from swapping) than have the kernel start terminating processes when memory runs out. But I still believe that if you encounter any significant amount of swapping, then you really need more RAM. On a modern computer with 8-16GB of RAM, I rarely, if ever encounter swapping.

When creating virtual machines, that's a little different. I often create them with limited RAM (1-2GB) because they're not running a GUI (generally accessed via ssh sessions), run minimal daemons in the background and I don't want to take more than necessary from the host environment. I will create a large (8GB) swap partition in order to handle a worst-case scenario (where I've woefully underestimated my RAM requirements) and monitor the amount of swapping that takes place. I'll increase the RAM allocation as necessary to keep swapping to a minimum under expected loads and leave the swapfile to be available when loads are higher than expected.

Either equal to the amount of installed RAM or none. There are two factors to consider, when making this decision.

If the only available drive is SDD or if you're operating on an encrypted distro, then no Swap.

144GB ram in my machine and I'm only using 40GB.

I disabled swap for now. 100GB for cache is good enuf for me.

Lol I could only imagine 288gb swap. That's why it differs from setup to setup so much.

I generally avoid using Swap on a system with 4GB+ of RAM... I also prefer to fully shut down instead of ever hibernating. I suppose it could be useful on a system with a less or slower RAM, but realistically, I just don't think it's necessary for the majority of use cases.

Hi, I have the impression the current amount is at least as much as you have of run plus 5%. Otherwise sistem will not hibernate or suspend correctly.

I still almost double the ram: even with 32Gb of ram I often run out of memory while loading heavy 3d scenes.

So the rule I suggest: depends on what you do with your computer... ;)

The only situation where I see swap space a must is editing large photos, like big panorama, with gimp: a lot of ram is needed, and if you don't have enough swap space, your system freezes and you are going to loose all your work.

At university, at the computer laboratory where we spent our time analysing DNA sequences, there was a big DNA sequence alignment experiment I was trying to perform for phylogeny production. The lab server computer had about 19GB of SWAP ( and probably 12GB of RAM ) I had helped set up months before and could not increase it further for a series of reasons at the time (namely, I was not able to increase swap space with my then-Linux-knowledge and could not reinstall everything because the server had Windows on it, with lots of data from other people )... But I remember that the experiment algorithm I was using to process that data needed just a little more than 20 GB... So as we had a very large HDD, I took note that for such types of computer set-ups with loads of HDD space, why not make SWAP really big?!... If we had , my experiment could have turned out to work out eventually...

I use a swapfile instead, which can grow dynamically. My swapfile is mounted on my root partition, which I have given 64GB to. IIRC I only use like 10% of that, so that basically leaves me with ~50GB swap space if really needed. Of course, I never go that high, probably the most resource-intensive thing I ever run on my computer (it's not even a computer, it's just a crappy tablet with a keyboard from my school with 4GB RAM) is Android Studio.

I only use a relatively small swapfile (a few hundred MB) to prevent enventual crashes.

I don't use hibernation : Computers boot really fast with a SSD drive (and suspend to disk isn't ideal with a SSD). Suspend to RAM is sufficient and works really well.

Oh and I forgot a quite important detail, I set vm.swappiness to 1

Interesting article. I now know that I have 2G of Swap on Ubuntu 18.10 with 8 GB of RAM. Is that too little?

I have since using Windows XP, disabled swap, never used it because I knew how to manage memory, Linux, gosh it never needed because it was so efficient at handling the memory, thus some of our friends point out under 8GB, and yeah maybe, those browsers are very memory hungry but I ran small Nix servers that use no more than 100Mb, sometimes growing to 500Mb, thus I feel if you sysadmin your machines correctly you can get away with very little ram and never even think of swap. Some feel in the Microsoft world you need to enable swap to allow kernel swapping and some page functions require it.....pffft boy are they wrong, Like I said, disabled it since XP days and never had any issues, apart from doing something stupid that grabbed all the ram at once and crashed the system, but that is down to user error. Now for the non sysadmin and technical people, leave the swap on, let the system manage it for them because really life is busy enough, though the security issues that are associated with it sometimes cause more issues, in today it's a security nightmare since the average Joe will not enable encryption or flush the swap file at each reboot. Anyway my 2 cents

To accommodate a variety of possible needs, I create a swap partition (better than a file, IMO) of 8GB for any system with 8GB RAM or higher. If you already have swap space assigned and need more, it's easy enough to add another partition. For devices with less than 8GB RAM, I use the 2X RAM rule. The proper swappiness setting eliminates any thrashing. As @Pierre123 suggested, I reset system swappiness to a more reasonable value than the default of 60. @Pierre123 uses 1. Setting it to 0 effectively shuts off swapping. I usually use 10, but YMMV. I haven't used MS Windows in almost 20 years but, when I did, I set up a swap partition instead of a file. Easier & faster to defrag and less likely to damage the file system. Today, with all the SSDs commonly found in new systems, you could avoid trouble by placing the partition on a standard HDD instead of the SSD, as many systems come with (or can be upgraded to) one of each. Thank you, @David Both, for a well-written post that clears up some confusion that many beginners and even some old-timers have about swap space.

You are welcome. I am glad that you find this article helpful.

In reply to by Tim K (not verified)

My system has 12GB RAM (4GB original , 8GB added by me) and the System76 set up allocated 13GB swap. I have often checked and I have never seen any swap in use. Based on that I think 0.5 X Ram is more than enough and even zero might be workable. In fact i have not seen over 50% of my RAM used at one time.

"As a consequence, recommended swap space is considered a function of system memory workload,"

That sounds good, but then we want a rule linking swap space to workload, or better, linking RAM size to workload.

The reasoning would be that your swap space should be big enough to handle the maximum possible demand, whereas the amount of memory is linked to your 'working set size'. A reasonable measure is to look at your uptime (run queue length). If that is 20 (long-term average fior a system with infinite RAM) that means your working set is 20 threads and you would also need that number of processor cores. A first approximation would be to figure out what programs those threads might belong to, and the amount of memory they need and install roughly that amount of RAM.

A better approach takes the size of those programs and the task switching frequency into account. Or simper:: just shrink the RAM to the point where the effective throughput and response-time is the required level, for instance, half the performance of a machine with unlimited RAM. Or you just llook for the point where the swap device is busy waiting 15 % of the time on average. With SSDs, you would install rather less RAM than with magnetic discs, because the latency of your swap device is so much lower.

A simple rule of thumb is that you need at least the amount of RAM for swap space in order to store a crash dump.

I generally go with zero or equal to the physical RAM.

When you start any significant swapping things usually so down so much it appears your app has "hung". Maybe for a server not running interactive code, but for me when sitting at the keyboard using swap is death.

I have 32 GB of memory and over a terabyte of SSD. Why would I possibly want to have virtual memory that puts additional wear on my SSD? In over 3 years, and running plenty of software including lots of Java, Eclipse, GIMP, LibreOffice, etc, I've never come close to exhausting memory. I believe that the memory pages for executable code are "backed" on "disk" by the actual executable file. So there is "some" virtual memory pages already. But read only, not putting wear on the SSD.

My rationale is this: RAM is cheap so use enough that you don't need a swap. Then allocate HDD space equal to your RAM size because HDD space is cheap too. I have 8 GB RAM on both of my systems and allocate 8 GB HDD space as swap because I have 360 GB of HDD space over 2 drives so I will never miss 8 GB.

I work almost exclusively from a laptop which is usually hibernated, rebooting preferably only once every few months (usually for kernel upgrades due to security concerns). This makes swap requirements very different from someone who never hibernates and reboots every day. Many programs today are badly written, with inefficient use of memory and/or memory leaks which accumulate over time. After a week or two of run time, Chromium web browser can easily hit 12-16 GB of memory usage by itself. Simply exiting and restarting Chromium with all the same tabs and windows will cut the memory usage by 75% or more. Add in Firefox (with lots of tabs), a couple of active virtual machines, some bioinformatics analysis code running in background (these often make poor use of memory), two or three active IDE's like Rstudio, PyCharm or (if I'm feeling particularly masochistic), Eclipse, and I am well past the 32 GB of RAM on my laptop. There is little to no swapping going on because while some of the software I use is well written and very efficient with it's use of memory, other programs are not, eating lots of storage because they keep things in memory that they no longer have any use for. This means I need lots of swap space for storing the "junk" that these programs keep lying around and thrashing usually isn't an issue. I currently have configured 48 GB of swap to go with my 32 GB of RAM to allow for hibernation and "junk", but with my increasing encounters with memory wasting code, 64 GB of swap may be more appropriate.

In my experience, it's all about the purpose of the box. What app you are running should tell you what you need and if you're using to much swap , you probably need more ram. IMHO

I ran my living room laptop with 8GB RAM, Ubuntu 14 & Chrome for a few years with zero swap. I had forgotten to create it & it was never an issue.

The rule of thumb is if you're swapping, add RAM.

I was managing some OpenStack servers and when they started using any swap above a baseline, it was time to reboot them because they would always be slower after that happened. Then they'd be good for another month or 2. Swap was useful as a canary.

I'm not sure if it's better to crash & reboot for low ram or have a bit of swap to allow recovery until you can reboot.

With cloud, you should have multiple VMs and compute nodes. And your apps should be HA so you can reboot nodes w/o affecting your apps. In that case, no swap on the VMs, ever.

I agree with the rational mentioned above that RAM is cheap...
Anyhow it strongly depends on the machine and its use.
For my production machine (never hibernates), the swap is zero. If it starts to swap then the performance drops drastically. It is better that the application is killed due to insufficient memory and I am forced to find a solution rather than running at lower speed without noticing.

I find that on my personal laptop system, I do not have numerous parallel tasks going on at the same time, chewing up resources and I seldom consume even half of available memory, so under these circumstances, swap is optional unless I care about suspend or hibernation operations.

I usually allow 1 to 2 times system memory to account for suspend or hibernate; otherwise I let virtual memory manage matters, and since neither disk or memory are issues, I do create swap, "just in case". In practice, with moderate utilization and applications regularly changed, I don't usually have a lot of processes chewing up memory. The guidelines are only general and do not necessarily represent all situations, certainly not my typical usage scenarios on home equipment.

These rules of thumb need to differentiate between workstations and servers and backend storage. No one hibernates a server. But you're dealing with a cluster of Linux server VMs sharing backend SAN, you can negatively impact neighboring VMs by allocating too much swap space to a VM as it'll consume disk I/O that the other VMs are sharing. However, if you're on SSD or all flash SAN, then disk I/O is less of an issue. For my personal desktop or laptop, I allocate enough swap to cover my hibernation needs. Both systems have 64Gb RAM so I have 64Gb of swap on SSD. For my servers (which are all VMs and shared disk-based SAN), I allocate only 2Gb of swap and I monitor them with Nagios. I don't want them to every use swap and impact the other servers on the same SAN. If more than 20% of the 2Gb is used, Nagios alerts and I give more memory to the VM and/or find out why the memory is being consumed so much that swapping is occurring at all.

I'll first say, laptops are an exception due to the needs of hibernation. In server environments, I run with none or minimal swap. I started experimenting years ago when I was managing VMWare ESX. Since the ESX host box already had a decent sized swap, I found that I could reduce swap (or page file for Windows) on my guests. My theory here was that only the Host OS knows the real hardware situation. Swap is really a function of hardware - limited RAM to be exact. On systems where I have abundant amounts of RAM, I never see swapping occur. That's my brief explanation.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.