18 ms·
Do we really need swap on modern systems?
- sevensor 10y agoMy desktop at work has 16G of RAM. I didn't bother setting up swap, and I find the old guidance (2x RAM) pretty absurd at this point. I've had the OOM-killer render the system unresponsive a couple of times, but only because I'd written a program that was leaking memory and I was pushing it to misbehave. If you really want virtual memory on purpose, you can still set up a memory-mapped file for your big data structure.
- sddfd 10y agoI don't have swap either. On 8GB it is pretty annoying, because a program I often use frequently overcommits and the system hangs. Is there any way to tell the OOM killer which program to kill first?
- outworlder 10y agoFrom the article: Without swap, the system will call the OOM when the memory is exhausted. You can prioritize which processes get killed first in configuring oom_adj_score.
- dice 10y ago>Is there any way to tell the OOM killer which program to kill first? From TFA: >Without swap, the system will call the OOM when the memory is exhausted. You can prioritize which processes get killed first in configuring oom_adj_score. The linked solution document is only available to registered RH users, though, and the name is actually oom_score_adj and not oom_adj_score. `man 5 proc` has details, but tl;dr is set /proc/<pid>/oom_score_adj to -1000 to make a process OOM-killer-invincible.
- wyldfire 10y ago> Is there any way to tell the OOM killer which program to kill first? The fun OOM analogy [1] that comes up when people propose different OOM killer designs: > An aircraft company discovered that it was cheaper to fly its planes with less fuel on board. The planes would be lighter and use less fuel and money was saved. On rare occasions however the amount of fuel was insufficient, and the plane would crash. This problem was solved by the engineers of the company by the development of a special OOF (out-of-fuel) mechanism. In emergency cases a passenger was selected and thrown out of the plane. (When necessary, the procedure was repeated.) A large body of theory was developed and many publications were devoted to the problem of properly selecting the victim to be ejected. Should the victim be chosen at random? Or should one choose the heaviest person? Or the oldest? Should passengers pay in order not to be ejected, so that the victim would be the poorest on board? And if for example the heaviest person was chosen, should there be a special exception in case that was the pilot? Should first class passengers be exempted? Now that the OOF mechanism existed, it would be activated every now and then, and eject passengers even when there was no fuel shortage. The engineers are still studying precisely how this malfunction is caused. [1] https://lwn.net/Articles/104185/ https://lwn.net/Articles/104185/
- amyjess 10y agoUse earlyoom: https://github.com/rfjakob/earlyoom https://github.com/rfjakob/earlyoom By default, it'll start killing processes when free memory drops below 10%, though you can configure the threshold. I had the same problem for years, and then I started using earlyoom and I don't have to deal with it anymore.
- lisivka 10y agoUse cgroups or `ulimit -m`.
- jerf 10y agoPutting spinning-rust-backed swap on a 16G system is absurd. By the time such a system is into swap, it probably isn't trying to swap three or four megabytes, it's probably trying to swap three or four gigabytes, and that can literally take hours. Simply writing that much data to a hard drive can take a non-trivial amount of time, and swap doesn't generally just cleanly run out to the hard drive with nothing else interfering, it's a lot messier. Given the speeds of everything else involved, a 16GB RAM system trying to swap to a hard drive, even a good one to say nothing of those slow-writing SMR hard drives [1], is basically a system that has completely failed and it might as well just start OOM-killing things. A system backed by an SSD does degrade more nicely, though. The system visibly slows down but doesn't go to outright unresponsive like it does on a hard drive. You can make a case for letting that happen and having human intervention select the processes to kill, rather than letting the kernel do it. So, even though it still isn't really useful as an extension of RAM, it can still be useful in recovering from systems that you've run yourself out of memory on. Since putting an SSD in my systems I've actually gone back to running with some swap space. Though the fact I like hibernation sometimes is also a reason I run with swap in Linux on my laptop. [1]: Swap will almost certainly completely blow out the buffers on those things and you'll be stuck with the raw hardware write speeds pretty quickly.
- amyjess 10y ago> I've had the OOM-killer render the system unresponsive a couple of times Use earlyoom instead of relying on oom-killer. https://github.com/rfjakob/earlyoom https://github.com/rfjakob/earlyoom To quote from the description: > The oom-killer generally has a bad reputation among Linux users. This may be part of the reason Linux invokes it only when it has absolutely no other choice. It will swap out the desktop environment, drop the whole page cache and empty every buffer before it will ultimately kill a process. At least that's what I think what it will do. I have yet to be patient enough to wait for it. [...] > This made people wonder if the oom-killer could be configured to step in earlier: superuser.com , unix.stackexchange.com. > As it turns out, no, it can't. At least using the in-kernel oom killer. And earlyoom exists to provide a better alternative to oom-killer in userspace that's much more aggressive about maintaining responsivity.
- jlgaddis 10y agoMy new workstation has 128 GB of RAM. It also has 1 GB of swap (on NVMe) that, AFAICT, has never been touched. I use it as sort of a canary that something abnormal is happening if it starts being used.
- lmm 10y agoI've been reading these stories for ten years. About 8 years ago I started taking them seriously and stopped using swap. Turns out not having swap works much better. I'm amazed how slowly the consensus seems to be moving though.
- njharman 10y agoSystems are used for vastly different purposes. With different memory usages and expected operation. There can be no consensus because there is no one answer.
- tossaway1 10y ago> I've been reading these stories for ten years. About 8 years ago I started taking them seriously and stopped using swap. Not sure what you're referring to here. This story doesn't recommend eliminating swap...
- tyingq 10y ago"Systems without swap can make sense and are supported by Red Hat - just be sure the behaviour of such a system under memory pressure is what you want" So, it doesn't exclusively recommend it, but it concedes that there are use cases where it makes sense.
- problems 10y agoYeah. I've had issues with this on some systems. On Windows without swap when you hit a remotely low on RAM point, things start going really poorly for some reason - random latency. So with 16 GB of RAM even I can't disable swap on Windows without some really strange performance characteristics, I run SSDs so I really wanted it off and I just stuffed more RAM in my box - with 32 GB it isn't a problem. On Linux however, you can pretty much turn it off and everything will run smooth until you're actually out and then you lag badly briefly, Linux's oom-killer does its thing and all is good again within the span of a few seconds.
- leereeves 10y agoSwap seems like a nice safety valve. Preferable, I think, to suddenly shutting down an important program in use because it's OOM.
- Aardwolf 10y agoMust depend on use case, but I prefer program that is planning to use swap (usually one where I accidentally allocate a way too big buffer) to fail automatically, rather than having to try to use the now unresponsive system UI to kill it
- ordu 10y agoYou are right, it depends. While building firefox from sources system needs several gigs of RAM. At the same time normal functioning of my system does not need more that 4Gb. And a couple of years I used swap just for such big /usr/bin/ld processes. Now I have 8Gb of RAM and linking FF or LibreOffice is not an issue anymore.
- jandrese 10y agoI remember when it became impossible to build Firefox on our lab machines with 1GB of RAM and 2GB of swap. Even before that it took literally all day to build. I got another taste of that lately when I had to build Wireshark from source on a Raspberry Pi model B thanks to broken packages in the repo. At least it was the version with 512MB of memory and overclocked. For the most part Wireshark isn't that bad to build, certainly better than Firefox, but there are a couple of dissectors that have unreasonably large source files.
- uwu 10y agoit never works for me on windows it just slows my system down to a crawl, requiring me to force a reboot it probably depends on your hardware and if i disable the pagefile, windows update stops working and at 75% memory usage it starts panicing and closing programs
- 10y ago
- adultSwim 10y agoWhat about to support hibernation? Is that possible via swap file now?
- adultSwim 10y agoAnswer: yes
- kevinmgranger 10y agoIt depends upon what filesystem you're writing it to, but the answer is mostly yes.
- zumu 10y agoHow do you hibernate with no swap? Do you need a special hibernation partition to write to?
- gravypod 10y agoThe way I've done it is create a swap file and set it's swappiness to 0 so nothing actually gets paged into it. Hibernation forces the writes so it will get used on hibernate.
- phkahler 10y agoI never understood the rule of thumb where swap space was proportional to the amount of physical RAM. It seem to me it should be the size of your largest expected allocation (system wide) minus the amount of physical RAM or something like that. If you had a nicely configured system and took out half the RAM it doesn't make sense that you'd want less swap space.
- ordu 10y agoBut you choose amount of RAM dependant on maximum memory usage. Therefore swap space (being proportional to RAM) becomes dependant on largest expected allocation also. It wouldn't be wise to build system with 2Gb of RAM and 4Gb of swap space, when you need 6Gb of memory at peaks: such a system would be slooow. It may be not wise to buy 8Gb of RAM when 5Gb is the maximum that might be needed.
- Someone 10y agoA system that has way more swap than RAM will run out of 'performance is acceptable' way before it runs out of memory. That was different in the early days, but that was because people accepted worse performance (GC that stops the world for seconds can be better than no GC, even when running a GUI). Certainly nowadays, if you take out half the RAM, you will want to take out half the processes, too.
- AckSyn 10y agoThe rule made sense when your system had fewer than 128-256 MB of ram. These days it doesn't.
- cbhl 10y agoI think it made a lot more sense in the mid-90s, where a system would have 32 MB of RAM and people read the RAM requirements of the software they'd buy. So the size of your largest expected allocation was proportional to the size of RAM only because if you had more RAM you'd run more RAM-intensive software. Now, desktops can have 32 GB of RAM, but everyone just uses it to run Chrome.
- CGamesPlay 10y ago
- Dayshine 10y agoSo, the argument the article makes is: 1. Swap is slow 2. If using swap, your system starts to thrash 3. If thrashing, you can't close programs to free memory 4. If you can't close programs, you have to wait until the task is killed by the OS 5. If you have no swap (or very little), you don't have to wait. Except with an SSD, swap isn't slow enough to cause that issue. So really this article only seems to apply to servers, not desktops.
- mitchty 10y agoNot entirely true, I've had swap thrashing with ssd's on desktops too. Though it tends to mean you're boned, or going to be waiting a while while all i/o is dedicated to swapping for minutes at a time.
- jandrese 10y agoIt seems like the OS developer's opinion these days is that RAM is cheap, don't swap. In the old days people cared a lot about swap performance because RAM was so tight that you were virtually guaranteed to swap at some point. These days you get 16GB sticks in boxes of Crackerjacks so why would you ever swap? Of course the trend of making notebooks thinner by ditching the SODIMM slots and soldering insufficient amounts of memory to the mobo may reverse this.
- sirn 10y agoOne usage of swap in modern systems: hibernation. If you need to use hibernation, that means a swap must exists, either as a swapfile (pre-allocated, as uswsusp require a fixed offset on the disk to resume) or as a partition.
- gravypod 10y agoI wish we took the path of EROS [0] rather then "RAM and DISK are seperate". A lot of problems stem from that incompatable viewpoint of computing. Computer Science is about hiding complexity under lays of abstraction that continualy provide safer states and constraints on the things built on top of them. Our abstraction that RAM and DISK are seperate is not safer nor does it provide constraints that are simple to navigate. Thinking about this the other way, where DISK is all you need and memory is just a write-through cache, is much safer in my opinion and leads to some really cool application design. If RAM and DISK are the same, then writing a file system is just writing an in-memory tree. No need to pull data from the disk, just navigate the tree in your program's memory and pull the blob data out. Want to persist acorss reboots, protect against power outages, or save user settings? Just set a variable and it'll be there. The benifits are much better then the costs. [0] - https://web.archive.org/web/20031029002231/http://www.eros-os.org:80/ https://web.archive.org/web/20031029002231/http://www.eros-o...
- nobodyorother 10y agoYou might want to investigate Mumps: https://en.wikipedia.org/wiki/MUMPS https://en.wikipedia.org/wiki/MUMPS Setting data in memory is the same as setting data on disk, the only difference is the name of the variable: s X=1 ; store 1 in variable named X, in memory. s ^X=X ; store 1 in variable named X, on disk. s X=^X ; load disk to memory
- vidarh 10y agoThe challenge with this is that abstracting away disk in a way that isn't horribly leaky is incredibly hard as long as one lets us manipulate individual bits and the other requires us to write whole sectors. Note that EROS is not providing a write-through cache. It's providing a write-back cache using checkpointing coupled with a journalling capability and ability to explicitly sync data. So it's leaky: Your application needs to know that it needs to structure it's writes to memory so that they will make sense if the system comes back up with some of the data missing, and needs to know how to use the journalling functionality. It can't just act as if it's running in RAM forever.
- 10y ago
- amyjess 10y agoI haven't used swap in years, and more recently I've accompanied that by using earlyoom [0] to start killing processes when RAM usage rises above 90%. Both changes have made my computers much more usable. Systems should designed to fail fast when memory is low instead of slowing down. [0] https://github.com/rfjakob/earlyoom https://github.com/rfjakob/earlyoom
- onli 10y agoThat article takes a system with 2GB ram as example. For a modern system that is pretty unrealistic, even Laptops have more. My system has 12. I missed the mention of zram. Zram can create ramdisks, and compress them. It can create a compressed swapdisk in ram, basically making your ram last longer in case you really run out of memory. In my experience that is a good alternative to having a bit of swapspace as reserve, as the article recommends.
- derefr 10y agoWhat I've always been specifically confused about, is if there's any point in giving a VM a swap partition inside its virtual disk, rather than just giving it a lot of regular virtual memory (even overcommitting compared to the host's amount of memory) and then letting the host swap out some of that RAM to its swap partition. Personally, I've never given VMs swap. I'd rather have memory pressure trigger horizontal scaling (or perhaps vertical rescaling, for things like DBMS nodes) than let Individual VMs struggle along under overloaded+degraded conditions.
- 293984j29384 10y agoAh, this is a great idea. It'd also be easier to understand and see service degradation (ie. physical memory being used on the host) directly from something like vCenter instead of relying upon Solarwinds to tell me the host is out of memory.
- moftz 10y agoBut does the host actually know what is an appropriate thing to swap? It doesn't know what is contained in that chunk of memory it just swapped. Although ideally, you would just build the system to contain enough memory for each VM so they can each run at full capacity along with whatever else overhead it may need for your hypervisor. You wouldn't want the host swapping out anything related to your VMs because it's just going to kill any performance of the affected VM. Give each VM its own swap space and let the guest figure out what needs to be swapped.
- tedunangst 10y agoGenerally yes. In fact, this is why "balloon" drivers exist, to allow the host to create backpressure and make the guest swap. The guest knows more about which pages are interesting than the host. If you make the host do the swapping, it will pick silly things, like the guest's disk cache, to write to swap.
- scott_s 10y agoFor clarification to other readers, "Generally yes" was the reply to the originally posed question, which means the above comment actually disagrees with the suggested solution. (I had to read both a few times to get this straight.)
- benibela 10y agoSomething seems to be seriously wrong with the swap implementation on modern systems. 20 years ago on Windows 98 it just started swapping, but it was no big deal. If something became too slow to be usable, you could just press ctrl+alt+del and kill that swapped program and everything worked fine afterwards. On the other hand, my modern linux laptop, it starts swapping, and it swaps and swaps and you can do nothing, not even move the mouse, till 30 minutes later something crashes.
- rootbear 10y agoCould this be a reflection of the increasing gulf between RAM speed and HD speed? Even with NVMe drives, which one probably shouldn't be swapping to anyway, RAM is orders of magnitude faster.
- Retric 10y agoWith SSD's disk is not that slow.
- SapphireSun 10y agoSSDs are only ~4x faster than magnetic last I checked. If RAM is 100ns per access, and hd access is down from say, 1ms to 0.25ms, that's still a huge huge gap. 4x isn't even an order of magnitude. EDIT: see comment below for more accurate numbers.
- snowwindwaves 10y agoFrom the article: >A typical reference to RAM is in the area of 100ns, accessing data on a SSD 150μs (so 1500 times of the RAM) and accessing data on a rotating disk 10ms (so 100.000 times the RAM.
- SapphireSun 10y agoThank you for the correction. I should have read more carefully. Still, we're talking 3 orders of magnitude for SSD vs RAM.
- Beltiras 10y agoI've had 32 gigs of memory in my desktop rig for more than a decade. Needless to say, no swap. Some decent points about mitigation like memory priority in the article. Good read.
- braveo 10y agoI have 32G in one machine, and 16G in another. I recently moved over to the 16G machine to do my dev work in, and I run a few VM's in it. I've found myself wanting to upgrade it to 32G ram, but honestly that's about the only use case (besides production servers) where I would ever consider swap, and at that point I consider it a problem of not enough memory rather than swap being necessary.
- garrybelka 10y agoThe real issue is not an amount of swap but thrashing. E.g., several large processes sleeping in memory on desktop would be fine if only one or two used at the same time. OTOH, clustered nodes well tuned for a single task may not need a swap. In any case, it is a metric for thrashing that should be used to initiate culling.
- intoverflow2 10y agoAll the laptops at my workplace have the minimum storage (Apple...), it becomes frustrating when I open photoshop and almost my entire free space suddenly vanishes while my 16gb of ram isn't even 20% utilised
- TorKlingberg 10y agoDo current operating systems ever page out memory to swap when not necessary, in order to make room for more disk cache in RAM?
- saurik 10y agoIf OS's stopped doing this at some point, I'd be shocked: this is why having memory and swap is valuable.
- deathanatos 10y agoYou'll be shocked, then. Linux under the default settings won't swap unless there is pressure to do so. Windows will. It's a tradeoff: if you swap something out while not under pressure, that could be the thing you next need, resulting in it just getting swapped back in. Or, maybe not and the extra cache is useful (but if you're not under pressure, maybe letting go of older cache, instead of swapping something out, is a better trade: letting go of old cache doesn't require swapping out, since it's by-definition backed by disk.)
- pvdebbe 10y agoWindows' approach is painful if you run it on slow HDDs like I did 15 years ago before switching to linux, full time.
- PeCaN 10y agoWindows does this very aggressively. It's built in to the I/O subsystem, actually, and (IIRC—it's been a bit since I've messed with this) IOCP calls execute immediately (on the same thread, avoiding any context switches) when requested data is already cached.
- kalleboo 10y agomacOS does this aggressively. I'm currently at 8 GB used/16 GB, and still have 5 GB swapped out.
- scottlamb 10y agoI hate swap. My experience with it is that once a disk-backed machine (as opposed to SSD) has started swapping, it's essentially unusable until you manually force all anonymous pages to be paged in by turning off swap ("sudo swapoff -a" on Linux) or reboot. My hunch is that the OS is swapping stuff back in stupidly. Once memory is available, I'd like it to page everything back proactively, preferring stuff from swap and then from file-backed mmaps. But instead it seems to be purely reactive, each major page fault requiring a disk seek to page in what's needed with little if any readahead. Basically the whole VM space remains a minefield until you stumble over and detonate each mine in your normal operation. Much better to reboot and have a usable system again. On my Linux systems, I've turned off swap. On OS X...last I checked, I wasn't able to find a way to do this. I'd like to turn off swap entirely, or failing that, have some equivalent way to force all of swap to be paged in now so I don't have to reboot when I hit swap. Anyone know of a way?
- calpaterson 10y agoContrarian anecdote: I've recovered from swapping a few times on a desktop machine with an HDD (typical scenario: an ON clause was omitted from a join and postgres is doing a cartesian join between two tables). I didn't find things unable instantly, I was able to recover by SIGTERMing the relevant process and then running `swapoff --all` while maybe going for a tea break. YMMV.
- outworlder 10y ago> My experience with it is that once a disk-backed machine (as opposed to SSD) has started swapping, it's essentially unusable until you manually force all anonymous pages to be paged in by turning off swap ("sudo swapoff -a" on Linux) or reboot. That depends. If your workload exceeds the amount of available memory, you will start "thrashing" the disk and that can make a system un-responsive. If you happen to launch a large application, or start working with a big file, unused pages will be evicted to disk to make room and, after some slowdown, the system should become perfectly usable again. YMMV On OSX, I don't know a way, but I can't recall the last time I had to reboot due to RAM/swap issues, even when I was developing apps on a 4GB Macbook Air. I guess memory compression, which is enabled by default, helps here. Most OSX systems have very fast SSDs as well.
- kebolio 10y agoI've found you definitely need swap if you don't have 8GB of memory. I personally have nowhere near the amount of patience required to wait for the OOM killer.
- phil21 10y agoThis is by far my biggest pet peeve in the space. The "rule of thumb" that you need 2x RAM as swap. Even 10 years ago this "rule" was ancient and useless but it was always a constant challenge educating customers as to why, and that yes - we really did know better than your uncle Rob. Once a server hits swap, it's dead. There is no recovering it other than for exceptional cases. If you are swapping out, you've already lost the battle. I tend to configure servers with 512MB to 1GB swap simply so the kernel can swap out a couple hundred MB of pages it never uses - but that's really more to make people feel better than it really being useful at all.
- toast0 10y ago2x RAM as swap is clearly bad, but I like having around 512MB to 1GB (on systems of basically any size); when you do start using more ram than you have, it gives you some buffer (as long as you actually alert on it). If you have a small memory leak, you can recover; if you have a large memory leak, you're going to run out of swap pretty quick anyway.
- thatcks 10y agoRules of thumb involving more swap than RAM probably date from decades ago, when Unix virtual memory systems were sufficiently primitive that the total amount of virtual memory you could use was just your swap space, not swap space plus (most of) RAM. (The limitation came about because the simple way to handle swapping is to assign every potentially swappable page of virtual memory a swap address when you allocate it in the kernel. Then the kernel always knows that there's space for the page if it ever needs to swap it out and you're never faced with a situation where you need to swap out a page but there's no swap space left.)
- ChuckMcM 10y agoOne of the things we used at Blekko was that swap became a 'soft' indicator that something on the system had exceeded its foot print (our machines all had 96GB of RAM so it meant something had too much RAM) and OOM-killer messages in the log was grounds for taking the machine out and rebooting it and looking for a more serious problem (like sometimes things rebooted and had 32GB less RAM). That said, the article's recommendation was spot on in terms of making a conscious decision on how you want your system to behave when its coming close to running out of memory. Large swap space was originally the way you got those things that were too big to fit in memory to run, and now they are a way to essentially batch process very large data sets.
- dancek 10y ago> [Recommended amount of swap] depends on the desired behaviour of the system, but configuring an amount of 20% of the RAM as swap is usually a good idea. This sounds like good advice compared to the classic "2x RAM" guideline. Back in the HDD era when we already had around 8GB RAM I started wondering how long it would take to actually fill 16GB of swap in terms of raw write speed. On the other hand SSDs are fast enough that swap might actually make a low-memory system feel faster. My current Linux laptop has around the same amount of swap as RAM. Am I mistaken in thinking that suspend-to-disk saves RAM contents on the swap partition?
- tomxor 10y agoOver the past 5 years swapping has always been due to a memory leak.
- tomxor 10y ago(for me I mean), I should really learn how to disable it.
- mamcx 10y agoWhat about OSX? Is goood to disable swap?
- mwpmaybe 10y agoMy personal rules of thumb for Linux systems. YMMV. * If you need a low-latency server or workstation and all of your processes are killable (i.e. they can be easily/automatically restarted without data loss): disable swap. * If you need a low-latency server or workstation and some of your processes are not killable (e.g. databases): enable swap and set vm.swappiness to 0. * SSD-backed desktops and other servers and workstations: enable swap and set vm.swappiness to 1 (for NAND flash longevity). * Disk-backed desktops and other servers and workstations: accept the system/distro defaults, typically swap enabled with vm.swappiness set to 60. You can and likely should lower vm.swappiness to 10 or so if you have a ton of RAM relative to your workload. * If your server or workstation has a mix of killable and non-killable processes, use oom_score_adj to protect the non-killable processes. * Monitor systems for swap (page-out) activity.
- gregmac 10y agoFor the curious (I was): * vm.swappiness = 0 The kernel will swap only to avoid an out of memory condition, when free memory will be below vm.min_free_kbytes limit. * vm.swappiness = 1 Minimum amount of swapping without disabling it entirely. * vm.swappiness = 60 The default value. * vm.swappiness = 100 The kernel will swap aggressively. https://en.wikipedia.org/wiki/Swappiness https://en.wikipedia.org/wiki/Swappiness
- Jedd 10y ago> vm.swappiness = 0 The kernel will swap only to avoid an out of memory condition, when free memory will be below vm.min_free_kbytes limit. This is not the case. It used to be the case, but changed in kernel version 3.5-rc1 (2012 ish) There was a discussion about this on HN a few weeks ago: https://news.ycombinator.com/item?id=13511086 https://news.ycombinator.com/item?id=13511086 And there's a blog post on the percona website about how this rather bizarre change bit them: https://www.percona.com/blog/2014/04/28/oom-relation-vm-swappiness0-new-kernel/ https://www.percona.com/blog/2014/04/28/oom-relation-vm-swap... I call it bizarre because (as I wrote in that other HN thread) a) it changed the behaviour of lots of production systems in a surprising way, and b) if you want to ensure your processes never swap you already had the option to not have a swap file or partition.
- mixedbit 10y agoIf Linux has no swap, it doesn't quickly and efficiently kill processes when memory is exhausted. Instead it first removes executable code from RAM and reads it back from disk when needed. This is because without swap executable code is the only thing in RAM that is duplicated on disk and can be removed. This makes the system completely frozen and unusable.
- rcxdude 10y agoThis is my experience too. I used to run my desktop without swap, but found that the experience when running out of memory was even worse than with swap. Also there appears to be enough memory which isn't actually used frequently that it gives a bit more memory headroom (I will still manage to use up 32GB of RAM).
- xorcist 10y agoI tried to run systems without swap a few years ago. That wasn't a very good idea. Most applications are very generous in their memory usage (not to mention allocation, almost always insane), and normally those pages are swapped out never to be heard from again. So without swap performance suffers since less pages are available for cache. (And in virtual environments it gets even worse since the balloon driver isn't really that great.) I didn't have time to see it through and abandoned it. In light of that this recommendation from Red Hat is very interesting. Just a fifth of memory as swap is probably enough to get real world performance back, without getting completely stuck when something goes haywire. On large memory systems it should probably be even less.
- tscs37 10y agoPersonally, I have a 100GB swap partition on my system... why? because I'm a filthy tab hoarder. I just put them in the background, if i need them, sure it takes a couple seconds but I don't have to close 'em. /shrug
- jlgaddis 10y ago"The Great Suspender" (browser extension) is your friend.
- TheGrassyKnoll 10y agoMe too (typing this in my 277th tab) Using Firefox on Bunsen Linux (Debian Jessie derived) Its only using half of 4G of RAM, but there's 20 Mbytes in swap.
- I_am_neo 10y agoI once swore if I had at least 200 Mb of ram I would turn swap off. Decades and giga bytes of ram later that still hasn't happened
- kabdib 10y agoIt's not uncommon for us to buy rack machines with much more RAM than disk. The disk is almost uninteresting, except that we need a place for an OS to boot from, and some other legacy things. I suspect I would be fine with much of our datacenter being diskless (and put disk -- ahem, I mean storage -- where it is needed). Local disk is a headache more often than not.
- jedberg 10y agoMy feeling on swap is this: 1) If you're ok with one machine dropping out of your system, you don't need swap. 2) You should never build a system where losing a single machine is a problem. 3) Therefore, you should never need swap 4) Perhaps there is an exception for a desktop machine, since it's doesn't fit rule 2.
- perlgeek 10y agoDoesn't that risk cascading failures? A cluster of a few machines experiences a bunch of requests that trigger pathological memory usage. One machine OOMs, drops out. Now the rest of the cluster has to take more load, needs more memory, and increases the likelihood that the other machines also run out of memory.
- scottlamb 10y ago> A cluster of a few machines experiences a bunch of requests that trigger pathological memory usage. One machine OOMs, drops out. Now the rest of the cluster has to take more load, needs more memory, and increases the likelihood that the other machines also run out of memory. A performance cliff (as you'd inevitably see while swapping) also puts you at risk of cascading failure. It might actually be better to completely drop out if the restart time is reasonably low. This is similar to GC thrashing with Java servers: many people prefer to configure their servers to suicide when GC time is over some threshold rather than try to go on as long as possible. I'm one of those people. Better ways to avoid cascading failure are overprovisioning (RAM is pretty cheap for servers) and load shedding / graceful degradation at the application layer, coupled with care in client-side retry logic. (Avoiding accidental capacity caches, using exponential backoff on any retry.)
- galdosdi 10y agoTend to agree. A bit of a side ramble: Unfortunately, sometimes regarding rule 2, you already have a system where losing a single machine is a problem, and it will take time and resources to improve or replace it to the point where losing a single machine isn't a problem, so "in the meantime" you have to accept and support this. Also, sometimes "the meantime" is very long. :-( Also, by the time the system is improved to be more resilient, maybe you'll be working somewhere else or on something else, and, presto, you'll uncover some other horrible legacy system in your dependency chain that isn't resilient either. It seems as if at every organization that has had computers for long enough, there is an infinite supply of legacy systems. Point being unless you only work with brand new things that themselves only work with brand new things, you can't get out of getting decent at managing services that aren't properly "any single machine can disappear" resilient
- pmontra 10y agoMy Ubuntu development laptop has been running without swap since I bought it in early 2014. It's got 16 GB of RAM and sometimes it hits 11 GB of used memory. No problems whatsoever. If I'll start hitting the memory limit I'll buy another 16 GB. I've replaced the HDD with a SSD but I don't understand why I should use it as swap like in the old days of RAM scarcity.
- lazyant 10y agoJust a note for people playing with vm.swappiness ( = 0 for ex) , what it does it's different depending on distro and version (as in with one version = 0 meant "absolutely no swap" and another version it may mean "try not to swap"
- beezle 10y agoI'm not sure if it is still true on Win 10, but earlier versions started to perform terribly if you had no swap on the boot partition, regardless of how much core you had.
- sevensor 10y agoThis is contrary to my experience in Windows 7. As under Linux, I run Win7 without swap on modern hardware, and I've had no trouble there. Which versions of Windows have you had trouble with?
- beezle 10y agoXP and 7. Current rig had 32GB though I left the boot swap at 1.5GB when 8 was installed and likewise after the upgrade to 10. My understanding when researching this (long ago) was that Windows like to purpopsely put stuff on the swap even if there is free ram. Even now, with not quite 9GB "in use" 22GB "standby" and 1G "free" the paging file is at 1.5% use with peak about 3%. Granted, that is tiny on a fixed 1.5GB file but for some reason Windows feels the need to drop about 20-50Mb in the pagefile.
- vmarsy 10y agoon modern system with a lot of RAM I found myself doing the opposite of swap to speed up development : mount disk on RAM using tmpfs [1] and then change the ccache [2] directory to that ram disk. With that setup, I obviously don't want swap to kick-in :) It can make the compilation of C++ programs much faster. [1] http://ubuntublog.org/tutorials/how-to-create-ramdisk-linux.htm http://ubuntublog.org/tutorials/how-to-create-ramdisk-linux.... [2] https://linux.die.net/man/1/ccache https://linux.die.net/man/1/ccache
- pavanky 10y agoI have had /tmp as tmpfs for a couple of years since arch switched to this layout. I ended up having the opposite problem, probably because I never had enough RAM. I have only ever enabled swap when compiling a few projects with "-j8" would take up all the memory. Using fewer threads would usually end up being slower than more threads + swap.
- rkeene2 10y agoThe main issue I have with not using swap in modern Linux is that it will cause the kernel to be busy for hours at a time. What happens is, as the kernel runs low on RAM, it has to spend more time searching for smaller and smaller chunks of RAM to back the request, the smaller chunks are more numerous and the "kswapd" kernel thread is responsible for this activity. As the system approaches 0 RAM free kswapd will also try to release less important pages, which takes more CPU time. Ultimately you get to the point where allocations take a really long time, and there are lots of allocations.
- Animats 10y agoSwapping should have disappeared years ago. At best, it gives the effect of twice as much memory, in exchange for much slower speed. It was invented when memory cost a million dollars a megabyte. Costs have declined since then. How much does doubling the memory cost today? What seems to keep swap alive is that asking for more memory ("malloc") is a request that can't be refused. Very few application programs handle an out of memory condition well. Many modern languages don't handle it at all. Nor is it customary to check for a "memory tight" condition and have programs restrain themselves, perhaps by starting fewer tasks in parallel, opening fewer connections, keeping fewer browser tabs in memory, or something similar. I've used QNX, the real time OS, as a desktop system. It doesn't swap. This make for very consistent performance. Real-time programs are usually written to be aware of their memory limits. Most mobile devices don't swap. So, in that sense, swapping is on the way out.
- Gaelan 10y agoUntil Apple stops soldering on memory, swap will still be alive on the desktop.
- euyyn 10y agoPlus mobile apps do get, and usually handle, a low-memory notification from the OS.
- RubenSandwich 10y agoOn iOS too many low memory warning in a set amount of time, Apple won't tell developers how many in what time frame in order to prevent them from gaming the system, will result in your app getting killed.
- rascul 10y agoJust put your swapfile on a ramdisk.
- virmundi 10y agoI've disabled swap on MacOS. Works fine with just 16 GB. That's with Spring STS and at least one 2GB virtualbox for running ArangoDB. I do use Safari so it uses less memory than Chrome. I shut it off because, in my opinion, OSX is pretty shitty at memory management. It swaps for no good reason. I've had 6 GB free and still using 1.6 GB of swap. That shouldn't happen.
- kalleboo 10y agoIt swaps out unused memory to free up space for disk cache in order to improve performance. This is actually a good use of memory.
- z3t4 10y agoDisk space is cheap. Add a few gigs of swap, or make a swap file! If you just have a few gigs of HDD disk space left, buy another disk!! Having some swap is worth it. One neat thing that swap can be used for is to take sleeping processes, that might use a lot of memory, and put that memory on disk to free up memory for other active programs. How much swap do you need ? Square root of your RAM size.
- phire 10y agoLast time I tried running a linux system with zero swap, I ran into huge issues. It would never actually hit the OoM killer, instead it would just lock up while it still technically had a few hundred mb of memory free. From what I can tell, it was stuck in a loop evicting something from cache and then immediately pulling it back in from disk. Everything was technically still running, but the ui wasn't responsive enough for me to even kill a program. Simply adding 200mb of swap would change the behaviour enough that the OoM killer would eventually run.
- kevin_thibedeau 10y agoI haven't used swap in years on an 8GiB machine. I might hit the OOM killer once a year. It does its job and the system keeps running.
- sitkack 10y agoIf u want to exec from a process using a large fraction of the physical address space on the machine, you need swap to maintain a nice amount of virtual adddess space. Needing swap and using swap are different things. How swap interacts with the process and memory subsystem is poorly understood.
- rini17 10y agoI recommend using swap together with zswap, and increase swappiness. Zswap is available in mainline kernel. It keeps compressed "swapped-out" pages in memory (so they are accessible quickly on page fault) and only uncompressible pages go to disk. Usually most of memory is compressible and overhead is small, so it is suitable for many workloads. See https://wiki.archlinux.org/index.php/Zswap https://wiki.archlinux.org/index.php/Zswap .
- INTPenis 10y agoI love how the article essentially says that each situation is different and then says 20% of RAM is a good rule of thumb. In my experience, the most commonly used swap configuration is minimal, 500M perhaps. And vm.swappiness=1. I'd say it's more rare to find a system that actually needs swap than one that can do without it. I have yet to run into an application that for some reason needed swap around.
- crest 10y agoThese days the primary use of swap is to get the efficiency of overcommitting RAM without compromising reliability.
- vbezhenar 10y agoMany applications request a memory, do some writes and don't use it in typical scenarios. So this memory is effectively wasted. If there's swap, smart operating system will swap that memory and use physical memory for more important tasks, e.g. for disk caching. So using swap allows for more efficient memory usage. E.g. on small server with 21 day uptime I have 102/1024MB memory used and 41MB is in swap, which means that I have 5% memory almost for free.
- shmerl 10y agoIs it possible to hibernate without swap? That's one of the use cases when it's actually useful.
- smarterclayton 10y agoIn designing Kubernetes, for instance, we document and (mostly) enforce swap off for many of the reasons laid out here. Kubernetes takes over the management of host overcommitment, and being able to react correctly to OOM and near-OOM depends to some degree on having a clear understanding of the actual memory use on the system.
- dredmorbius 10y agoSwap isn't the problem. Swapping is. Question is: is there a way to identify who the culprit is and take appropriate vengeful action in the instance of swapping? Generally it's "foo large application", though there's also a very strong tendency for "foo large application" to be a critical system element -- either OS or application level.
- mayoff 10y agoIs iOS a modern system? Because iOS does not have swap. > Although OS X supports a backing store, iOS does not. In iPhone applications, read-only data that is already on the disk (such as code pages) is simply removed from memory and reloaded from disk as needed. Writable data is never removed from memory by the operating system. Instead, if the amount of free memory drops below a certain threshold, the system asks the running applications to free up memory voluntarily to make room for new data. Applications that fail to free up enough memory are terminated. https://developer.apple.com/library/content/documentation/Performance/Conceptual/ManagingMemory/Articles/AboutMemory.html https://developer.apple.com/library/content/documentation/Pe...
- htns 10y agoI have 16 GB of memory with swap off yet still sometimes get lag and freezes due to low memory. An aggressive OOM killer or a performance watchdog should be considered a mandatory feature. On desktop I'd much rather have my programs shut down than get any lag.
- treffer 10y agoI have a somehow different view on swap. The issue is not swap or swap utilisation, the problem is worst case latency. Even for a database an OOM is usually better than a latency hit that makes it unusable slow. As a simple example an app might start allocating and use memory in an infinite loop. How long will that take? How long will your system be unresponsive? If you have more swap than you can write in 30s you'll most likely do it wrong (your system can be unresponsive for 60+s). Another worst case would be allocating all memory, using it and then performing random reads throughout the memory space. Your swap to ram ratio defines how much misses and thus how much IO you are doing instead of direct memory access. This should stay way below your IO capacity. As a result I usually try to use a small swap partition and monitor for swap-ins, not swap usage or swap out. So that's my thinking around swap mainly due to the fact that I have seen too many servers causing issues due to swap related latency.
- st4rbuck 10y agoAre this rules also applicable for FreeBSD or any other BSD like systems?
- crest 10y agoFreeBSD doesn't overcommit the available memory (RAM + swap) by default. Don't use swapfiles in FreeBSD because the file system write paths of UFS and ZFS potentially allocate memory. Both geom_mirror (software RAID-1) and geom_eli (disk encryption) are fine and I would recommend using GELI to create a onetime keys for mirrored swap partitions at boot. An other good habit to get into is to limit the resources available to your services to some generous upper bound you expect them to require. The most flexible way to enforce those are restrictions in FreeBSD are hierarchical resource limits. Use them and monitor resource consumption. That way you get an early warning before a rouge process drives the system into massive swapping.
- zurn 10y agoSwap media is closing in on RAM speed at a fast clip. RAM latency has been stalled for 30 years and storage latencies are doing very well & improving due to flash. So from hardware POV, it should be a good time for swapping.
- deleted 10y ago[deleted]
- isaac_is_goat 10y agoCan you change swap settings on macOS, I haven't dug in deeply but couldn't find an option anywhere but I feel that the default settings is faaaaar too aggressive.
- patrickg_zill 10y agoNot knowing a huge amount of OS kernels theory, I can still point to Solaris 10 and it's handling of swap as being clearly superior. I had a 16gb ram server giving me problems... I logged in and it was swapping continuously with only 2mb, yes 2048kb, free. Yet my ssh session was not overly lagged. Under Linux, if there is heavy swapping, forget it, nothing will work well.