6 ms·
Do we need swap on modern systems? (2017)
- sitkack 3y agoYes, you still need to increase virtual address space.
- jeffbee 3y agoVirtual size does not imply swap. You can have a gigantic virtual space with few realized pages.
- sitkack 3y agoOC doesn't increase virtual address space the same way that adding swap does.
- Karellen 3y agoAre you sure "virtual address space" is the right terminology for what you're meaning?
- paulryanrogers 3y agoDefinitely yes, if you've a Mac with 8GB RAM. And possibly even with 16GB. Heaven help you if swapping significantly degrades the irreplaceable persistent storage.
- Wowfunhappy 3y agoOkay, but the question is more interesting if we consider machines with >= 32gb, rather than models which have strangely small amounts of memory. 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.
- deleted 3y ago[deleted]
- laweijfmvo 3y agoI’ve used machines with hundreds of GBs of RAM that crash without at least a couple GBs of swap.
- jprete 3y agoThat's interesting. Why do you think they crashed? I wouldn't think a few gigs of swap would make that much difference unless there's something up with the OS code paths for a system with no swap.
- sheertwitter 3y agoOn many more efficient linux distros you can get away with a reduced or eliminated swap file. When SSDs first became popular capacities were relatively small, and I do remember not wanting to give up 8gb of a 120gb drive to swap that would be unused on a system with 8gb of memory. I setup a few systems that way and never had an issue. That said, you are setting yourself up for instability so I wouldn't ever do this on anything mission critical. Also, the balance of storage to memory is not the same in 2024 as it was 12 years ago.
- jeffbee 3y agoI think there are two related questions that aren't strictly "swap". To discuss the question, you need to generalize beyond "swap" to all file-backed pages. Under pressure to reclaim space the kernel might evict any page that is backed by a file, but people who want to "disable swap" don't want this either. So if you are against swap you need to make sure that anything that came from a file including your programs and libraries are locked into memory and can't be evicted for reclaim. 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.
- deleted 3y ago[deleted]
- jauntywundrkind 3y agoIs there anything on this assertion that map is obsolete to dive into? That's a bold & very different claim.
- jeffbee 3y agoIf you are just trying to get along and make your program work, there's nothing morally wrong with mmap, but it is far from optimal and optimality is where the interesting discussions happen. Making the kernel deal with your magic page tables, and asking it to infer what to evict, what to read ahead of time are not the best approach. The page fault cost did not matter much when they invented mmap, back when disk accesses took tens of milliseconds. Today it matters a great deal. Using `fio` on my system, mmap is the slowest way to access the SSD.
- RetroTechie 3y agoImho: if you 'need' swap, you're running the wrong software. 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.
- throwawaaarrgh 3y agoI'm sorry, but you don't understand swap. It's useful to retain all kinds of memory in different scenarios, like cache, or windows that haven't been accessed in a while. It can free up fast memory, reducing contention and speeding up the system. And you can control snappiness to decide what and when things get swapped. 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.
- donkeybeer 3y agoI don't know how representative firefox hardware survey would be of the general population, but https://data.firefox.com/dashboard/hardware https://data.firefox.com/dashboard/hardware 4 and 8 gb ram seem still most common.
- AnotherGoodName 3y agoEven in the 64gb case you're still losing out since the OS will use free memory as file cache. 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.
- 3y ago
- hiAndrewQuinn 3y agoIirc OpenBSD will dump all its RAM to swap in the event of a crash, or something, so that's a strong argument in favor of it from a practical perspective. If you're the kind of person who likes to investigate those things in depth.
- E39M5S62 3y agoI've been using zram on my personal workstation/laptops for the last year or so. It really is https://downloadmoreram.com/ https://downloadmoreram.com/ for Linux.
- gymbeaux 3y agoThe Rick roll is a nice touch
- dsr_ 3y agoIf you're on a laptop, swap is where your hibernation contents go. (This is probably a bad idea, but that's what happens.) So you need slightly more than RAM, at a minimum, or you don't do hibernation. I recommend not doing hibernation unless you absolutely need it.
- user_7832 3y agoWhat’s wrong with hibernation, ssd wear? (Asking because I hibernate all the time as sleep is still buggy on my framework)
- al_borland 3y agoIs it the S3 sleep issue LTT highlighted, or something else? https://m.youtube.com/watch?v=OHKKcd3sx2c https://m.youtube.com/watch?v=OHKKcd3sx2c
- deleted 3y ago[deleted]
- user_7832 3y agoI think it might be this. Funnily running powercfg –energy in cmd reported > 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.
- PhilipRoman 3y agoSwap is the only mechanism through which a non-mmapped page in memory can be swapped out, therefore if running without swap and there exists any ram which is accessed less commonly than the next-most-commonly-accessed area of disk currently not in cache, the memory utilization is suboptimal. 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.
- shrimp_emoji 3y agoThese are the esoteric and technical arguments that scare me into still using a swapfile and which maybe answer why I see my swap space being used when I have like 60 GB of RAM free (neither of which make any sense otherwise). :p
- juujian 3y agoSame boat. But hey, last year I had to get a 30GB dataset into memory so you never know... Though now I'm smart enough to optimize better.
- radicalbyte 3y agoOh you're a Chrome user too? :-)
- colanderman 3y agoSimpler answer is: at one point stuff got put into swap to make room for a memory-hungry task, and it hasn't been referenced since, so no need to pull it back into RAM.
- bsdetector 3y ago> if running without swap and there exists any ram which is accessed less commonly than the next-most-commonly-accessed area of disk currently not in cache, the memory utilization is suboptimal. 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.
- jsheard 3y agoThe alternative is what Android and iOS do - applications keep running on a best-effort basis and are summarily executed if physical RAM runs out. 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.
- AnotherGoodName 3y agoFun thing I've had to do on Android. When the app does a really memory intensive task (client side video encoding in this case) we forked that process and trapped the sigsegv fault from the oom in the parent process and simply restarted after a sleep. It fixed a huge percentage of app deaths for us. 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.
- wilkystyle 3y agoWhich is incredibly frustrating if you need to switch between your browser and another app and the browser keeps getting killed and has to reload every time you come back
- MaxBarraclough 3y agoI'd blame bloatware rather than the OS. As globular-toast's comment points out, using an SSD for swap could cause significant wear, i.e. shortening of the device's usable lifetime. If applications made more efficient use of memory, the OS wouldn't need to terminate processes.
- easeout 3y agoThis used to happen back when phones could only keep a few apps backgrounded at a time, but I haven't seen iOS kill the most recently used background app since the first couple years backgrounding was supported at all. 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.
- syoc 3y agoI strongly dislike swap on servers. I can understand some use cases laptops and one-off situations. 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.
- xorcist 3y agoSwap (paging) can also help performance. It exists for a reason. Having metrics on paging helps you tune your application, so it is also good for observability. It a feature that can be misused, but it is not a good recommendation to turn it off without knowledge of the specific situation.
- xxs 3y agonone of my laptops/desktop/gaming rigs or servers feature swap. Swapping along with a managed language and garbage collection is next to indistinguishable from a application crash.
- zbentley 3y agoIs it? What about the page cache impacted by those applications’ file I/O?
- xxs 3y agoit is, trying running stop-the-world type of a garbage collection along with page-in-out, etc. >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.
- eimrine 3y agoI used to have swap on MLC SSD 128Gb bought per $100 but do not remember year. I used it on a laptop with really heavy use of swap for web-browsing. The SSD stopped being functional after few years of such usage. It refuses to write anything but I can read my files having few KB/s somehow. I never watched SMART on this drive but I believe swapping killed it.
- sesm 3y agoDo we even need virtual memory? I think there should be an easy opt-out from virtual memory for user selected programs.
- xxs 3y agovirtually memory you need, the latter has nothing to do w/ swapping.
- mrob 3y agoWe need virtual memory to avoid the consequences of memory fragmentation. If every process shares the same address space, you might be unable to start a process despite having plenty of free ram, because that ram is scattered all over the address space. Virtual memory allows the process to see a contiguous range of free memory even when the physical memory is fragmented.
- sesm 3y agoYou make assumptions that programs start and stop often and allocate memory multiple times at runtime. Both assumptions are not true for server software and programs that allocate one big chunk and manage memory on their own, like JVM.
- deleted 3y ago[deleted]
- Mathnerd314 3y agoRed Hat's recommendations for swap: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/managing_storage_devices/getting-started-with-swap_managing-storage-devices#recommended-system-swap-space_getting-started-with-swap https://access.redhat.com/documentation/en-us/red_hat_enterp... I would say the relevant info is "at least 4GB" on any system with >8GB of RAM.
- tayo42 3y agoDo people still have a use for swap in modern server workloads? With everything running in containers on multi tenant machines it seems like it would be a nightmare to over subscribe ram available. Every misbehaving process will get killed by the oom killer when it hits its cgroup limit. Makes sense for personal scenarios.
- al_borland 3y agoI just wrote some server provisioning automation for a datacenter. I originally didn’t include special handling for swap, as I didn’t think anyone would be using it, but I got some user requests. So now if they add swap, I set it up accordingly. It wasn’t common, and I don’t know if their app strictly required it or they were leaning on old habits, but some are still doing it.
- pjdesno 3y agoOne thing missing from this discussion is the cost of DRAM. It hit $4/GB in 2011, and hasn’t changed all that much since then. In contrast if you go back another 13 years, memory cost $1/MB in 1998. 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.
- kjkjadksj 3y agoRam still gets cheaper. 16gb ddr4 is $30, ddr5 is $45. The only company with expensive ram is and has always been Apple.
- LegionMammal978 3y agoAt least in my experience, 99% of the time that I run out of physical RAM, it's because of some runaway process that's gobbling up as much as it can for no real gain, or otherwise attempting to perform a computation that could only complete with an astronomical amount of memory. 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?
- gymbeaux 3y agoOn one hand, it’s inefficient to have more RAM than you’ll ever use. So many people buy MacBooks with the largest RAM capacity and while that worked well in the days of 16GB being the max, I’m not sure 64GB of RAM is going to come in handy even 5 years from now. RAM doubles because “marketing”, but RAM requirements for programs hasn’t really increased, certainly not proportionally. 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.
- mcc1ane 3y agohttps://chrisdown.name/2018/01/02/in-defence-of-swap.html https://chrisdown.name/2018/01/02/in-defence-of-swap.html
- daft_pink 3y agoIf it’s a underrammed mac we do.