Live data from Hacker News

In defence of swap: common misconceptions

chrisdown.name

131–140 of 151 posts

Re: In defence of swap: common misconceptions

#131
post #33

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.

Windows has vastly different policies for RAM allocation and commit than Linux. Windows basically does not overcommit while Linux systems not only does it by default but quite depend on it for various loads to work properly. In consequence, the userspace has a tendency to handle RAM differently, but there is no magic: if programs are trying to allocate twice the amount of RAM and then only use half of it, Windows wit…

Windows also has two syscalls - one to reserve memory and one to commit memory. You can reserve+commit at once, but you can also just reserve a chunk of virtual memory that you commit at a later time. Accessing pages that aren't committed is a segfault. So you can say "I might need up to X contiguous bytes of virtual memory" and then commit as you go. IIRC, Windows will let you over-reserve, but not over-commit.

Edit: sorry, not two syscals, it's an option to the malloc-equivalent - VirtualAlloc

Re: In defence of swap: common misconceptions

#132
post #112
post #65

Earlier quoted context omitted.

The issue is that the current OOM killer doesn't support this usage at all. To extend the analogy: what do you do if grandma comes and fills your house with stuff? You need space to work, so you go and drop it off at the self storage place, but what if she just keeps filling your house up? The OOM killer will do absolutely nothing until both your house and the whole self storage place are totally full. By that point,…

One problem with relying on the OOM killer in general is that the OOM killer is only invoked in moments of extreme starvation of memory. We really have no ability currently in Linux (or basically any operating system using overcommit) to determine when we're truly "out of memory", so the main metric used is our success or failure to reclaim enough pages to meet new demands. As for the analogy -- there are metrics you…

> One problem with relying on the OOM killer in general is that the OOM killer is only invoked in moments of extreme starvation of memory. We really have no ability currently in Linux (or basically any operating system using overcommit) to determine when we're truly "out of memory", so the main metric used is our success or failure to reclaim enough pages to meet new demands.

The problem with relying on swap instead of the OOM killer is that, instead of the OOM killer, the user gets invoked in moments of extreme starvation of memory and the whole machine gets rebooted. The OOM killer is far gentler; it only kills processes until the extreme starvation is resolved.

Re: In defence of swap: common misconceptions

#133
post #128

Earlier quoted context omitted.

> So, in other words, if you have enough memory for your workload that you won't run out, there's no benefit to having swap space (i.e. you've wasted money on memory you don't need). No, that's the opposite. If you have enough memory for your workload that you won't run out, swapping lets you use more memory for disk cache (instead of keeping unaccessed anonymous pages in real ram). Unless by "won't run out" you mean…

> If you have enough memory for your workload that you won't run out, swapping lets you use more memory for disk cache Except that it doesn't happen in practice, on my systems anyway. If you have plenty of memory, you can keep all your programs in it and as much as the system wants to cache and still not run out. The theory says that swap effectively buys you some memory to spend on more important things (than what t…

Are you talking desktop workflows here? If this is a service, why are you paying for RAM that never gets used? Get a smaller instance.

Re: In defence of swap: common misconceptions

#134
post #130

Earlier quoted context omitted.

Sure, but why wouldn't you run some swap with your much bigger ram anyway? In the end the core idea is: sometimes you have anonymous memory that is accessed so rarely that you'd rather have an extra disk cache page. If you assume that the kernel is not paging out memory that you actually use when not under pressure, swap doesn't hurt you.

> Sure, but why wouldn't you run some swap with your much bigger ram anyway? If you don't need it, you don't need it. The other question is: how much swap exactly should I have? And why wouldn't I just add that much RAM instead? > In the end the core idea is: sometimes you have anonymous memory that is accessed so rarely that you'd rather have an extra disk cache page. That's the theory. In practice I always have mor…

>If you don't need it, you don't need it. The other question is: how much swap exactly should I have? And why wouldn't I just add that much RAM instead?

Because adding RAM costs money, adding swap space is a config setting.

It looks to me that your boxes are idle. Sure, if you're not doing any work, it doesn't matter...

>1) Bad assumption 2) it doesn't help you either, so why bother? Actually I might have a use for that disk space. In that case the swap just hurts.

1) Is it? Can you source that claim somehow? 2) It does help you, that's the point. It makes your disk reads faster 3) Your answer to needing memory is 'buy more ram', but you 'may have a use for that disk space'? Buy more disk.

Re: In defence of swap: common misconceptions

#135

Very few people on this thread read and understood the article. The point isn't working with data sets larger than RAM. The point is making better use of the RAM you do have by taking pages you'll almost never touch and spilling them to disk so that there's more room in RAM for pages you will touch. Banning swap is like making self-storage companies illegal and forcing everyone to hold all possessions in their homes.…

Feel free to explain it to me. " Under no/low memory contention [...] Without swap: We cannot swap out rarely-used anonymous memory, as it’s locked in memory. While this may not immediately present as a problem, on some workloads this may represent a non-trivial drop in performance due to stale, anonymous pages taking space away from more important use." Now imagine that I have no memory contention. In other words I'…

> If you sometimes run out of memory (or even get close), then you should have some swap. This seems fairly obvious to me. Relying on the OOM killer to "clean things up" is pretty dubious. But was there every any serious argument to do this? I've literally never heard of that before.

Why does that seem obvious to you? With swap, running low on memory is game over. Without swap, the OOM killer runs. You can call the OOM killer dubious, graceless, or any number of other things, but it gets the system responsive again without doing as much damage as the human intervention that's otherwise required.

Re: In defence of swap: common misconceptions

#136
post #128

Earlier quoted context omitted.

> If you have enough memory for your workload that you won't run out, swapping lets you use more memory for disk cache Except that it doesn't happen in practice, on my systems anyway. If you have plenty of memory, you can keep all your programs in it and as much as the system wants to cache and still not run out. The theory says that swap effectively buys you some memory to spend on more important things (than what t…

Are you talking desktop workflows here? If this is a service, why are you paying for RAM that never gets used? Get a smaller instance.

> Are you talking desktop workflows here?

Yes [I explicitly spelled out desktops & laptops] but it's been true on my servers too.

> If this is a service, why are you paying for RAM that never gets used? Get a smaller instance.

Not all providers are so flexible. I may want a server with more CPU & traffic and some additional disk space. Going there gets me more RAM too. It turns out these services are constrained by the real hardware. If I'm getting a box with enough CPU for my needs, well they are not going to a shop to buy that special box for me, they use what they have and what they have comes with plenty of RAM too.

Re: In defence of swap: common misconceptions

#137
post #130

Earlier quoted context omitted.

> Sure, but why wouldn't you run some swap with your much bigger ram anyway? If you don't need it, you don't need it. The other question is: how much swap exactly should I have? And why wouldn't I just add that much RAM instead? > In the end the core idea is: sometimes you have anonymous memory that is accessed so rarely that you'd rather have an extra disk cache page. That's the theory. In practice I always have mor…

>If you don't need it, you don't need it. The other question is: how much swap exactly should I have? And why wouldn't I just add that much RAM instead? Because adding RAM costs money, adding swap space is a config setting. It looks to me that your boxes are idle. Sure, if you're not doing any work, it doesn't matter... >1) Bad assumption 2) it doesn't help you either, so why bother? Actually I might have a use for t…

> 1) Is it? Can you source that claim somehow?

Only my anecode. I've had systems become annoyingly sluggish because the OS decided I no longer need something and paged it out, even tough I had plenty of RAM. Turns out I needed that something.

> 2) It does help you, that's the point. It makes your disk reads faster

I just gave my numbers. The systems are not caching nearly as much as I have ram. These numbers come from systems I use every day; they are not idle.

> 3) Your answer to needing memory is 'buy more ram', but you 'may have a use for that disk space'? Buy more disk.

Why are people so hell bent on telling me I should use swap that doesn't actually help me at all? Yes I buy as much as disk as I need, and I'm not putting unnecessary swapfiles or partitions on them. Yes I also buy as much RAM as I need.

The only real justification I see for swap here is that it's cheaper -- poor man's RAM. I call that emergency memory for when you can't have enough RAM. If I have enough memory, swap is completely pointless.

Re: In defence of swap: common misconceptions

#138
post #111

In my experience, a misbehaving linux system that'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`. Until this is fixed, I'll just keep running my systems with very small amounts of sw…

> In my experience, a misbehaving linux system that's out of RAM and has swap to spare will be unusably slow. Yeah, this is basically the main drawback of swap. I tried to address this somewhat in the article and the conclusion: > Swap can make a system slower to OOM kill, since it provides another, slower source of memory to thrash on in out of memory situations – the OOM killer is only used by the kernel as a last…

And a multigigabyte brick of a web browser.

Re: In defence of swap: common misconceptions

#139
post #116

In my experience, a misbehaving linux system that'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`. Until this is fixed, I'll just keep running my systems with very small amounts of sw…

The point being is, that a system doesn't have to misbehave to allocate more memory than the total RAM. And in those cases, there is a very good reason to have swap space, and swapping won't impact the performance of the system - rather the opposite.

Sure it does misbehave. The memory allocation failures should be handled properly and by that I mean not by crashing. Very few applications should require memory use beyond current free RAM. Especially not JVM, JavaScript VM, a web browser or even video player. Yet this silly heuristics in Linux lets it happen.

Re: In defence of swap: common misconceptions

#140
post #88

Earlier quoted context omitted.

I actually did this on a old laptop, setup 200mb of swap for 4gb of ram. And it caused huge problems for me, would run out of swap while having plenty of free memory and then go cripplingly slow.

Set the swappiness lower.

That only delays the inevitable. Try setting overcommit to 2 and ratio to 100, then note apps crashing.
Post reply on HN