I read many places that the rule of thumb for swap space is to double the amount of physical RAM. However, 32 GB does seem a LOT. Do I need that much? Do I need it at all with this high amount of physical RAM?
I read many places that the rule of thumb for swap space is to double the amount of physical RAM. However, 32 GB does seem a LOT. Do I need that much? Do I need it at all with this high amount of physical RAM?
Here's a very good recommendation from Red Hat: Recommended System Swap Space
An excerpt from the same link:
And another excerpt from an older version of the page:
RHEL 8 table:
Original RHEL 5 table:
It entirely depends on what you plan to do with the machine. For example if it were a Sap server then yes, I would add 32gb swap ( we have boxes at work with 128gb ram and 32gb swap ). If you were manipulating massive pictures and video then it makes sense to have a little swap. 32gb is probably overkill.
However I would not say zero swap.
In the unlikely event that you run out of RAM - perhaps opening a big file, perheps a long running tab in firefox, it doesn't matter, in that event your kernel OOM killer will kick in and start killing applications to get memory back. Under those circumstances it's entirely possible that you will lose data as applications get killed. However if you have a bit of swap then the system will carry on, grabbing swap and allowing the system to continue. System slows down as heavy swapping happens, you notice and investigate before all swap exhausted. Also disk is very cheap, so why not have swap?
Anyone who says "you don't need swap" without asking you what you're actually doing with your computer is making assumptions. Whilst you may well do very little with your computer that eats RAM, it's still best to ask the questions about what you're planning to do with it before making the rash judgement that you don't "need" swap. In my humble opinion
Argh. The answers on this post are so very wrong, and this comes up as one of the first results in a Google search for "How much swap?"
First, a good point of reference is the Ubuntu Swap FAQ
This FAQ makes an important point that no one here mentions, and that is (emphasis mine)
So, swap is not just for hibernation! This activity, swapping out unused pages, can be beneficial in some scenarios and detrimental in others. Beneficial because it can free up memory for the system to use elsewhere, but detrimental when that swap out is too aggressive, and swaps out pages you actually do want. This is especially true on a desktop system where applications can sit idle for long periods of time, but the user still expects immediate performance when bringing it back to life. The control for how aggressive this is, is referred to as swappiness
The Ubuntu Swap FAQ discusses this lightly under What is swappiness and how do I change it? The default value on an Ubuntu system is 60, while the FAQ recommends a value of 10 for a desktop machine.
So, I would say that it is generally very important to not have a system with absolutely no swap space, as some people were recommending on this question, unless you know for certain very specific usage scenarios for your system and are very aware of the consequences it could have. (Personally, there is no situation where I would run a system without swap) In an Out of Memory scenario with no swap, the system will start killing processes (generally the one that caused the OOM)
Probably not. If you plan to hibernate your computer (suspend-to-disk) then you'll need at least 16 GB. If you won't hibernate, it is enough to let a few gigabytes for swap (4-8 max.)
From experience I can say this:
For what I have seen using 4GB of ram for 3 years, Ubuntu has had a swap usage of around 60MB but only when doing some very intensive tasks. A couple of weeks ago I started using a PC that had 16GB of ram and the swap usage has stayed in 0% for ever. I have done multiple compiles, video rendering and other intensive tasks. There has never been a change in swap. Not even a 1KB change.
Basically, in Ubuntu, the more memory RAM you have, the less likely you will use or need swap for any task. 16GB of ram, or even 8GB of ram is more than enough. I have done with the 16GB PC 8 Virtualbox PCs (each between 1GB to 2GB of ram). I have compiled and rendered a 720p video and not even in those conditions has the swap changed.
With that said, the rule of twice as much memory ram should not apply on Linux based systems, even more so if you have more memory. You should however have the same size of swap equal to your ram size or if you are planning to hibernate, since the process of hibernation grabs everything in ram and puts it on swap, which is why you need a minimum size equal to your ram size for swap.
The other detail is that some apps (not all) will still force to read from swap. So having a bit is good. Disabling it altogether is very bad in the case you get to the point of out of memory or OOM. At this point if you do not have swap, there is no way the kernel can swap in/out different ram app usages and will eventually start killing the less used ones.
If it helps with home desktop decision making, after updating this answer in more than 4 years, I have used a 16gb, 32gb and now a 64gb ram PC. Testing with only 128MB of swap revealed that I had no need for larger swap except if I wanted to hibernate. I have used cloud environments on them, lxc, virtualbox, wine, you name it. I have rendered videos with openshot, kazam, handbrake, audacity. I have used gimp, blender, Inkscape and even the full LibreOffice suite. I even played on steam and in none of those scenarios the swap was used. Of course, it will eventually get used if you end up using the full ram amount in one way or another but in general the more ram you have, the less swap you'll need.
Just my 2 cents.
No, you don't need 32 GB as long as you don't use features like suspend to disk.
In most cases, I'd say you don't need any at all. Perhaps if you edit very large image files, or some other app that needs lots of ram, you might need some.
I've never seen any used at all on my system, similar to yours. In fact, at one time, I had it disabled for a week or two accidentally, and never noticed any problem. But I still keep about 5GB for my swap drive, mainly because I have over 1TB available, and don't use all the space as it is.
If you use hibernate, it's safe to have as much swap as your amount of RAM.
For a general rule of thumb on how much swap to have, read here:
https://help.ubuntu.com/community/SwapFaq
In your case I'd say 4 GB would be enough to match your RAM.
If your ram is higher than 1GB, it is usually enough for ubuntu. The "Swap = RAM x2" rule is for old computers with 256 or 128mb of ram.
So 1 GB of swap is usually enough for 4GB of RAM. 8 GB would be too much.
From Ubuntu's SwapFaq:
Why do I need swap?
Memory consuming programs Sometimes, a large program (like LibreOffice, Neverwinter Nights, or a video editor) make the entire system need extra memory. A significant number of the pages used by these large programs during its startup may only be used for initialization and then never used again. The system can swap out those pages and free the memory for other programs or even for the disk cache. In these cases, swap will be used to help the system handle any extra load.
Hibernation (suspend-to-disk) The hibernation feature (suspend-to-disk) writes out the contents of RAM to the swap partition before turning off the machine. Therefore, your swap partition should be at least as big as your RAM size. The hibernation implementation currently used in Ubuntu, swsusp, needs a swap or suspend partition. It cannot use a swap file on an active file system.
Unforeseeable Circumstances Unforeseeable events can and will happen (a program going crazy, some action needing much more space than you thought, or any other unpredictable combination of events). 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 Since mechanical hard drives are considerably slower than RAM (SSD - Solid State Drive - storage is not as slow as physical drives, but still slower than RAM), when you need a file (be it a data file like a video, executables like Firefox, or libraries), the Linux kernel reads the file into RAM and keeps it there, so that the next time you need it, it is already in RAM and data access is much faster. The portions of RAM that accelerate disk read are called "cached memory." You will notice that they make a huge difference in terms of responsiveness. The Linux kernel automatically moves RAM reserved by programs--but not really used--into swap, so that it can serve the better purpose of extending cached memory.
Optimizing Swap performance Because swap space uses a disk device, this can cause performance issues in any system that uses swap space significantly because the system itself may also be using the same disk device at the same time that it is required for swap operations. 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.
How much swap do I need?
For less then 1GB of physical memory (RAM), it's highly recommended that the swap space should, as a base minimum, be equal to the amount of RAM. Also, it's recommended that the swap space is maximum twice the amount of RAM depending upon the amount of hard disk space available for the system because of diminishing returns.
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. The only downside to having more swap space than you will actually use, is the disk space you will be reserving for it.
The "diminishing returns" means that if you need more swap space than twice your RAM size, you'd better add more RAM as Hard Disk Drive (HDD) access is about 10³ slower then RAM access, so something that would take 1 second, suddenly takes more then 15 minutes! And still more then a minute on a fast Solid State Drive (SSD)...
Example Scenarios