Do we need swap on modern systems? (2017)
redhat.com
redhat.com
I've seen a lot of tools getting this wrong, even earlyoom which, AFAIK only measures memory and swap usage, but not the sustained rate of swapping. My computer can run just fine with all of it's swap used while ram is half empty.
Any implementation of overcommit is inherently unstable and susceptible to a "bank-run" situation, adding or removing swap will not change that. If you want a robust system, preallocate memory for everything and set limits via cgroups.
Memory that is swapped out is a small write operation, which generally is much more resource and wear intensive than a read; a program memory page and disk cache page are not equivalent.
Additionally, the swapped out program memory may be required again and cause an unpredictable delay in program operation; when a user has to wait for a menu to open while it is swapped back in that is suboptimal use of memory.
A modern operating system should have compressed memory rather than swap. Take the pages that would be swapped out for being rarely accessed, if they compress well then free the page and store it in an area for compressed pages. This will get most of the expanded cache benefit from swap without delays, wear, or possibility of the system grinding to a halt.
SSD space is dirt cheap (unless you get it from Apple), I'm not losing any sleep over losing a few gigs to a swap file to keep that from ever happening.
The reason this worked is that Android starts killing processes only after you pretty much run out of ram. So if you have a task that needs a lot of RAM even though the phone has it if it just closed a few apps you can't use that ram until after you've hit the allocation fault and let a bunch of apps die.
If applications made more efficient use of memory, the OS wouldn't need to terminate processes.
But then, apps in that environment were made for no swap. If you take away swap on a desktop system it could be another story.
Also, it's not just about space. SSDs have a very limited number of write cycles before they fail.
I don't lose sleep over space on my SSD either, but I don't want thrashing either. As a user I get no real feedback on the memory usage of my applications until everything gets slow. Fortunately it's easy to configure Firefox to unload idle tabs, but it might be better if -at least for GUI apps- I could confidently configure no swap.
I would much rather have an application get killed by OOM killer than swapping. Swapping absolutely kills performance. Not having enough RAM is a faulty state and swapping hides that from admins resulting in hard to debug issues. The OOM killer leaves handy logs, swapping just degrades your service and needs to be correlated with RAM usage metrics.
My experience is also that swap will be used no matter how low (or was it high?) you set the swappiness number if the memory throughput is high enough, even if there is enough RAM available.
>cache impacted by those applications’ file I/O
Which cache? The disk one, that depends on the available memory, with pretty much all free memory being a disk cache. In the cases of swapping, there is no disk cache left, effectively.
Generally speaking, systems do not swap out pages only under memory pressure. That design would be ineffective. When memory pressure is high enough, you've already lost.
Any normal workload has hot and cold pages.
I think I would rather have the server’s SSD be an Optane drive (or some other high-endurance flash memory) with a swap partition, and use some other means of monitoring and being alerted to high memory usage.
My rule of thumb is on an average load there should be no swapping, meaning that vmstat or whatever should be showing mostly 0's in the swap column. That doesn't mean it has 0 bytes in swap, in fact it probably is using some swap. It means nothing in swap is in the working set. For example, the server I'm looking at now is showing 0 swap activity, has 2GB of RAM and is using 1.3GB of swap. When a peak hits you will get some small delays of course, but it's likely no one will notice.
Doing anything else leaves money on the table.
Laptops have adapted to constant-price DRAM - 8GB was a decent memory size 10 years ago, and 16 is probably the equivalent today, with lots of swapping to compressed RAM using CPU cycles that get cheaper every year, and some swapping to flash that is 10-20x faster than the hard drives of 10-25 years ago. Servers not so much for various reasons, not all rational.
The lessons we learned in the decades when RAM was always getting bigger and cheaper every year don’t always apply nowadays.
I recommend not doing hibernation unless you absolutely need it.
> S1 Sleep Supported false S2 Sleep Supported false S3 Sleep Supported false S4 Sleep Supported true
Which... seems quite wrong given I can put my laptop to sleep lol. But thanks for the pointer, even though I was aware LTT did the video on sleep it'd fallen out of my memory (pun not intended).
Edit: I think I partially solved it. Running powercfg /a reveals...
The following sleep states are available on this system: Standby (S0 Low Power Idle) Network Connected Hibernate Fast Startup
The following sleep states are not available on this system: Standby (S1) The system firmware does not support this standby state. This standby state is disabled when S0 low power idle is supported.
Standby (S2)
The system firmware does not support this standby state.
This standby state is disabled when S0 low power idle is supported.
Standby (S3)
The system firmware does not support this standby state.
This standby state is disabled when S0 low power idle is supported.
Hybrid Sleep
Standby (S3) is not available.
The hypervisor does not support this standby state.
(I have hyper V enabled.) So it does look like S0 sleep is the culprit.This is one of the best parts about hibernation. Shutting down is saving state. Booting? The kernel starts booting as usual, entirely the same as a normal boot. It gets quite far along, has already loaded a bunch of drivers, and then it finds the hibernation state & loads that.
Where-as with most s3 and s2idle suspends, there really is a lot of system level bios support required to make things happen. Many systems just do it wrong, not to spec, or desktops often not at all. These are non-issues in hibernation.
To your point though, I did have a laptop on which the wifi would disappear every other hibernation. I'd tend to hibernate it, immediately wake it up, and hibernate again, so when I really turned it on it would be good to go. For a while I had been packing a USB wifi card because I hadn't put it together. So yeah I've seen issues. A kernel upgrade half a decade ago seemed to have fixed that, but yeah I guess it's some evidence of problems. Still, the amount of time I've spent trying to get systems to suspend has been long & sad & difficult, with little evidence of what's happening. Hibernation has been a pretty reliable & consistent tool that I feel like I can almost always rely on.
I find boot times quick enough on modern laptops that shutting down and restart is fine.
If I want to leave session alive during the day I just lock it and leave it running. Hibernate is enabled for if battery gets low but I hardly ever manually hibernate.
How mobile are you with your laptop? If I had to restart (i.e., lose all my xterms' state, etc.) every time I'd stopped using my laptop for a day or so it would totally kill my workflow
S3 and s2idle both require good bios support. Many desktops flat out won't suspend. But hibernate? To hibernate requires nothing. The kernel writes it's state as it shuts down, and a kernel when loading looks for hibernation state & loads it if found. The boot path looks normal, works normally, until a good way through the kernel initializing itself. This makes it so much more reliable & available, being not dependent on bios support, not requiring special handling.
Not losing any battery when hibernating is excellent. Just yesterday I turned on a laptop for the first time in a month, and it has a full charge & my full previous state. I love that so much.
I am curious how the kernel handles making space for the hibernation state. Having to dump main memory into some space feels like it has to create a lot of pressure. If swap is already being used, thats got to be quite the effort to hibernate. It seems like it should be failing to hibernate sometimes! Somehow though I've never hit any issues, never heard of any issues.
I don’t recall seeing an option to hibernate in Windows 11 either, but I may be mistaken.
Hibernation seems less essential on a laptop than a desktop, but I don’t miss it in either case. It’s very rare that a power outage makes me reboot normally on a desktop.
If an OS+apps' working set exceeds RAM, performance will slow to a crawl in any case.
Best (and only?) argument I've seen for having swap, is that in a gradually building out-of-memory situation (say, a memory leak), swap buys the admin more time to respond / diagnose the problem.
That is a valid reason. Use swap where it applies.
But for eg. desktop systems: pointless. Some app eating all the RAM? Crash right now, please! Then it's usually obvious which app was the culprit. As opposed to slowing everything to a crawl slowly over time & leaving the user to wonder what's going on.
Swap is also used by the system's hibernate function to store a copy of RAM to disk.
If you have 64GB of RAM and no combination of your apps, cache, etc will ever approach that, then probably nothing will ever swap. But for more limited systems or when running apps with higher memory pressure, swap does increase system performance.
4 and 8 gb ram seem still most common.
No swap just means you think everything ever allocated should always stay in ram. In pretty much every system there's going to be a few GB that were allocated but never really needed. I'd rather that not be true but since it is I'd like that stuff paged out to give the os more file cache.
> Some app eating all the RAM? crash right now, please?
It's ambiguous which is using "all the RAM." If you have game in one window and browser in another, and you start seeing high memory pressure, which should crash? Or if you're building software and you've fanned out to dozens of processes building dependencies concurrently but all using very little memory independently, which ones should be OOM-killed?
Alternatively you can see processes that haven't been woken or taken focus in awhile and page their memory out to swap to release main memory for applications that need it, and avoid crashing or losing data.
But for the swap to work efficiently, you still need to have enough RAM to run the things you want to run right now. The slowing to a crawl occurs when the running apps try to touch pages all over the place, requiring very frequent swapping in/out. Then yes, it's time to terminate something and free up some space.
As an example: I run a small Hetzner cloud instance (for about $4.50/month) for quick prototyping when I am away from home. It has a nowadays-considered-wimpy 2GB of RAM. It also exposes a Jupyter server that I (or friends) can use for quick experiments from anywhere in the world.
Some time ago I added torch and torchvision modules for a quick size comparison on a couple of models (just number of trainable parameters) and "pip install torch" got OOM-killed before I realized that I have zero swap and added it (and could have deleted it after the install).
This is not a common example, but a general observation that while using swap in the production servers, running one thing in a highly predictable fashion (uhm, sure) might mask the problem you want to expose, swap is extremely useful for general-purpose computers that occasionally run things that cause memory use spikes and are not particularly sensitive on whether they take a few milliseconds or a few seconds. My 2c.
That sounds like a misunderstanding how memory is allocated and this may be the root of it. There are many instances where this would be completely expected. The most trivial perhaps being an application that mmap() a file much larger than memory. That is a perfectly valid thing to do.
After the offending process is killed, every other useful process has had all its memory swapped out, since the offending process was so urgent in its requests for memory. Then, since the system is so lazy at swapping memory back in, each process has degraded performance for the next several minutes of usage. So as an ultimate fix, I have a shell script on each of my machines that does "swapoff -a; swapon -a" to swap everything back within a minute.
My own experience makes me wonder, what RAM configurations are people running where they reportedly find swap so useful? Is everyone else running so many useful applications simultaneously that they use almost all their RAM?
In cases where going from 32GB to 64GB adds $400+ to the price of my computer (cough Apple cough) I think it’s worthwhile to have swap and deal with occasional (if any) performance penalties.
The other related issue is whether mmap is obsolete. It is, but most application authors haven't gotten the message yet. Extending your address space over a bunch of files and making the kernel figure it out doesn't work optimally on modern hardware.
Heaven help you if swapping significantly degrades the irreplaceable persistent storage.
Edit: Also, TFA is clearly written with servers in mind, although I think the implications are interesting for e.g. your average laptop user installing Linux and wondering how much hard drive space to give up.
Makes sense for personal scenarios.