TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

In defence of swap: common misconceptions

150 pointsby c4urselfover 7 years ago

23 comments

cosarara97over 7 years ago
In my experience, a misbehaving linux system that&#x27;s out of RAM and has swap to spare will be unusably slow. The process of switching to a tty, logging in, and killing whatever the offending process is can easily take a good 15 minutes. Xorg will just freeze. Oh, and hopefully you know what process it is, else good luck running `top`.<p>Until this is fixed, I&#x27;ll just keep running my systems with very small amounts of swap (say, 512MB in a system with 16GB of RAM). I&#x27;d rather the OOM killer kick in than have to REISUB or hold down the power button.<p>Some benchmarks with regards to the performance claims would be nice.
评论 #16149847 未加载
评论 #16147469 未加载
评论 #16149662 未加载
评论 #16147065 未加载
评论 #16148652 未加载
评论 #16149895 未加载
quotemstrover 7 years ago
Very few people on this thread read and understood the article. The point isn&#x27;t working with data sets larger than RAM. The point is making better use of the RAM you do have by taking pages you&#x27;ll almost never touch and spilling them to disk so that there&#x27;s more room in RAM for pages you <i>will</i> touch.<p>Banning swap is like making self-storage companies illegal and forcing everyone to hold all possessions in their homes. Sure, you&#x27;d be able to get to grandma&#x27;s half broken kitschy dog coaster that you can&#x27;t bring yourself to throw away, but you&#x27;d also be harder to harder to fit and find your own stuff, the stuff you need all the time.<p>If you find yourself driving to and from the self storage place every day, you probably need a bigger home. But self storage is plenty useful even if you almost never visit it.
评论 #16147634 未加载
评论 #16148777 未加载
评论 #16148198 未加载
评论 #16149197 未加载
评论 #16149945 未加载
wahB4vaiover 7 years ago
I&#x27;m a big fan of determinism and service uniformity. Having that rarely used and response time critical function&#x2F;data&#x2F;whatever swapped out increases service time variation at best and complicates all worst case response time calculations at least.<p>I understand from the land of JIT compilers, garbage collectors, and oversubscribed everything that this is not much of a substantial concern as these features are already traded away.<p>The swap may be the best case in a bad situation. I would argue along the lines of don&#x27;t be in a bad situation...<p>I&#x27;m looking at you 8 of 16 GB used on cold boot Mac laptops... Looking at you with indignation and rancor Chrome.
评论 #16147997 未加载
perlgeekover 7 years ago
&gt; Under temporary spikes in memory usage &gt; With swap: We’re more resilient to temporary spikes, but in cases of severe memory starvation, the period from memory thrashing beginning to the OOM killer may be prolonged. We have more visibility into the instigators of memory pressure and can act on them more reasonably, and can perform a controlled intervention.<p>Somehow that doesn&#x27;t resonate with my experience. I tend to remember the cases where I can&#x27;t even SSH into the box, because the fork in sshd takes minutes, as does spawning the login shell.<p>I&#x27;d really like some way to have swap, but still loosen the OOM killer on the biggest memory hog when the system slows down to a crawl. I haven&#x27;t found that magic configuration yet.
评论 #16149091 未加载
评论 #16147027 未加载
评论 #16146918 未加载
评论 #16148680 未加载
avarover 7 years ago
Even for those who understand this well, it&#x27;s historically been really hard to coerce the Linux kernel into applying the right swap policies to your application.<p>As the author notes much of this has been improve by cgroups, and there&#x27;s always been big hammers like mlock(), even with those things it can be hard to prevent memory thrashing in extreme cases. I&#x27;ve seen swap disabled completely by people who understood how it worked as a last result because of that.<p>It&#x27;s always seemed to me that this was mainly a problem of the kernel configuration being too opaque. Why can&#x27;t you configure on a system-wide basis that you can use swap e.g. only for anonymous pages and for nothing else?<p>Similarly it would be nice to have a facility for the OOMkiller to call out to userspace (it would need dedicated reserved memory for this) to ask a configured user program &quot;what should I kill?&quot;. You might also want to do that when you have 10G left, not 0 bytes.
评论 #16146244 未加载
评论 #16148006 未加载
slaymaker1907over 7 years ago
I recently reenabled swap on my Windows machine due to frequent OOM, even with 16GB of RAM while playing Overwatch and browsing on Firefox. It seems like both of these programs allocate vast swaths of memory but then do not actually use that memory very heavily. After I turned swap back on, I did not notice any degradation in performance but my system stability skyrocketed.
评论 #16146580 未加载
black_puppydogover 7 years ago
Without wanting to impose my method or reasoning here, I run my dev machine without swap, and I&#x27;d rather have the same for the cluster machines I access.<p>This is for academic use only. I know how much RAM my machine has, and if I oom, it usually isn&#x27;t because I tried to squeeze in just a tiny bit too much data, but rather because I made some stupid mistake and keep allocating small chunks of memory very rapidly. On a system with even a moderate amount of swap, this makes everything grind to a halt, and it is usually much faster to just reboot the machine and deal with the problems later <i>in the unlikely event that rebooting actually causes problems</i>.
评论 #16149907 未加载
dborehamover 7 years ago
We&#x27;ve disabled swap (by not configuring a swap partition) on every server we&#x27;ve deployed since 2009. It&#x27;s a little irritating to have to manually remove the swap partition from various Linux&#x27; &quot;server&quot; default install options even today. Of course this means I&#x27;m still installing on bare metal that I own, so...dinosaur.
leocover 7 years ago
I&#x27;d be more convinced by the argument that swap shouldn&#x27;t be thought of as slow RAM if the author addressed the fact that it&#x27;s generally known as &#x27;virtual memory&#x27;—and it has been since at least System 370, so it&#x27;s not simply a later misconception: <a href="http:&#x2F;&#x2F;pages.cs.wisc.edu&#x2F;~stjones&#x2F;proj&#x2F;vm_reading&#x2F;ibmrd2505M.pdf" rel="nofollow">http:&#x2F;&#x2F;pages.cs.wisc.edu&#x2F;~stjones&#x2F;proj&#x2F;vm_reading&#x2F;ibmrd2505M...</a> . Instead the article just omits the term &#x27;virtual memory&#x27; completely, and pretty conspicuously.<p>I also think that a convincing case for swap would have to discuss the concepts of latency, interactivity, and (soft) real-time performance, things that largely weren&#x27;t to the fore in the salad days of the 370 family or the VAX. Virtual memory is the TCP of local storage.
评论 #16149179 未加载
nicklafover 7 years ago
I have an older Chromebook (c720), which is really quite memory starved (2GB RAM), and have experienced ChromeOS completely frying the SSD simply through prolonged tab-heavy swapping.<p>Now, I&#x27;ve replaced the SSD and installed a non-Google Linux distro, and would like to limit the amount of swapping Firefox can do.<p>I had been planning to simply use cgroups&#x27; memory features to limit the amount of memory consumed by Firefox processes, but if I am to understand the article (which I admit I didn&#x27;t read in full detail), I should also be able to tune swapping to limit the actual amount of swapping that takes place, avoiding a drastic uptick in SSD wear whenever open too many tabs.<p>That, and perhaps a Firefox extension that suspends background tabs in memory (which I&#x27;ve used before with a certain amount of effectiveness in the pre-WebExtension days).
评论 #16148980 未加载
评论 #16146143 未加载
评论 #16146101 未加载
CoconutPilotover 7 years ago
Swap was a great idea, but its time is gone. Swap doesn&#x27;t make sense anymore, hard drives have not scaled and kept up with the improvements in RAM.<p>In the Pentium 1 era EDO RAM maxed out at 256MB&#x2F;s and hard disk xfer was 10MB&#x2F;s. Common RAM size was 16MB.<p>In today&#x27;s era DDR4 maxes out at 32GB&#x2F;s and hard disk xfer is 500 MB&#x2F;s. Common RAM size is 16GB.<p>RAM xfer rate has grown is 320x. RAM capacity has grown 100x. Disk xfer rate has grown 50x.<p>Swap is no longer a useful tool.
评论 #16146501 未加载
评论 #16147626 未加载
评论 #16146829 未加载
评论 #16146914 未加载
评论 #16147026 未加载
whopaover 7 years ago
This is an informative and well written article, but seems incomplete in this day and age. In public cloud environments, network attached storage is far more prevalent, so the swap story may be different there (I honestly don&#x27;t know though). Since the author works at Facebook, he probably lacks experience in this regard.
评论 #16145971 未加载
评论 #16146499 未加载
ohaziover 7 years ago
Does hibernating via a swap file work reasonably well yet? I haven&#x27;t had a chance to try this out yet, but that&#x27;s the main reason I still have a swap partition on my laptop.
评论 #16147491 未加载
bhoustonover 7 years ago
On windows I have found it necessary to disable swap to keep myself efficient. Many times Ive had applications decide to allocate massive amounts of memory and then it leads to my system slowing down with tons of swap activity. In nearly all cases, I didn&#x27;t want my system to try its best to handle these massive memory requests but rather it should have just killed the offending application. Often in these failure scenarios the swap goes nuts and my computer becomes unresponsive that it takes a long time to even get to kill the bad actor.<p>Thus I disabled swap and I never had these unresponsive issues. I run with 32GB of ram so generally well behaved applications never run into memory issues.<p>Some applications that would cause issues would be too many VirtualBox instances that use more than available memory. A text editor that chocks trying to open a &gt;1GB text file (looking at you, the new JS-based editors.)
评论 #16145668 未加载
javituryover 7 years ago
For many years now I use ram compression instead of swap for desktops&#x2F;laptops. I particularly like zram but zswap is also great if you you are hitting hardware limits.<p>The difference with swap is that the computer doesn&#x27;t get unresponsive, it just slows down a bit. And Ram compression still buys some time before OOM killer hits.
ibizaover 7 years ago
Count me among the believers in running w&#x2F; swap. Here&#x27;s all it takes to provide Linux with a little swap space:<p><pre><code> fallocate -l 8G &#x2F;swapfile chmod 0600 &#x2F;swapfile mkswap &#x2F;swapfile swapon &#x2F;swapfile </code></pre> Add an entry in &#x2F;etc&#x2F;fstab &amp; you&#x27;re done. &quot;This little trick&quot; made all the difference on a compute cluster I managed, where each node contained 96G of RAM. It&#x27;s much more pleasant to monitor swap usage than the OOMKiller and related kernel parameters.
kartickvover 7 years ago
How long before desktop OSs manage memory like mobile ones, automatically shutting down background apps that aren&#x27;t being used, so that the system remains responsive no matter what?<p>Or, worst case, if two tasks that need 8GB each are running on a machine with 8GB memory, kill one, let the other finish, and restart the first one. Or, less ambitious, freeze one, swap it out to disk, let the other finish, and only then resume the frozen app.<p>Desktop OSs are so primitive at memory management, forcing the complexity onto the user.
评论 #16149447 未加载
viraptorover 7 years ago
I feel like the real metric is like to see from swap usage is: how much time did I spend waiting to swap-in a page, how much extra cache pages it gave me, and what&#x27;s the cache hit ratio. If the big purpose is to allow those extra few pages to be available, then either it&#x27;s worth doing or not - there should be an objective way to look at this. Unfortunately only the second and third part is easily available. The first... maybe via systemtap?
评论 #16149974 未加载
kstenerudover 7 years ago
So, in other words, if you have enough memory for your workload that you won&#x27;t run out, there&#x27;s no benefit to having swap space (i.e. you&#x27;ve wasted money on memory you don&#x27;t need).<p>But if you DO have swap space, there won&#x27;t be a performance hit (at least not under Linux) because it will only swap out some rarely used pages and then sit there doing nothing.<p>So, in the general case, it&#x27;s better to have it and not need it than need it and not have it.
评论 #16145954 未加载
评论 #16145958 未加载
Aardwolfover 7 years ago
In defence against swap on my personal computer:<p>-PCs have a lot of RAM now<p>-When you allocate that much memory it&#x27;s usually a bug in your own code like a size_t that overflowed. I never saw programs I would actually want to use try to allocate that much<p>-When using swap instead of ram, everything becomes so slow that you&#x27;re screwed anyway. The UI doesn&#x27;t even respond fast enough to kill whatever tries to use all that memory.<p>-How common is a situation where you need more memory than your ram size yet less than ram+swap size in a useful way? Usually if something needs a lot, it&#x27;s <i>really</i> lot (and as mentioned above not desirable)<p>-Added complexity of making extra partition<p>-Added complexity if you want to use full disk encryption<p>-I do the opposite of using disk as ram: I put &#x2F;tmp in a ramdisk of a few gigs<p>-Disks are slow and fast ssd&#x27;s are expensive so you would&#x27;t want to sacrifice their space (maybe if this changes some day...)
评论 #16146851 未加载
评论 #16147710 未加载
评论 #16149910 未加载
评论 #16146907 未加载
Neil44over 7 years ago
The system should be tuned so that under excessive pressure from requests it starts to turn requests away before running out of RAM. Having a small ammount of swap will let you get closer to the limit of RAM use without risking OOM killing something and getting the system into an undefined state. Also you can swap a fair bit of the system you don&#x27;t use and leave it there, giving you more RAM to use for processing requests.
loegover 7 years ago
All of this boils down to &quot;because buying more disk is cheaper than buying more RAM&quot; and &quot;avoid memory contention.&quot;<p>The author discusses the situation as if the quantity of RAM is fixed and swap can be added (or not). But that isn&#x27;t the only possibility — you can also add more RAM (it&#x27;s just expensive). For the same number of GB of RAM+swap vs just RAM, there is no reason to prefer the option with swap.
评论 #16145858 未加载
评论 #16146952 未加载
评论 #16148386 未加载
评论 #16146168 未加载
coding123over 7 years ago
My use case for swap: 10 dev environments on a cheap-ass AWS server. Where each environment is about 5 docker images.<p>They are slower, but to give every branch a fully usable test system is pretty awesome. No reason to pay through the butt for RAM for tier-1 dev environments. You can also have a premium dev environment for the develop branch on a different server.