Hacker News new | ask | show | jobs
by kstenerud 3072 days ago
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).

But if you DO have swap space, there won'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.

So, in the general case, it's better to have it and not need it than need it and not have it.

2 comments

> 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 "never have to throw away a disk cache page", which seems very unrealistic.

> 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 the system chooses to page out). So does buying more memory.

> Unless by "won't run out" you mean "never have to throw away a disk cache page", which seems very unrealistic.

I have an instance of top running on my desktops & laptops all the time. I never see cache using up all of the memory.

                  total        used        free      shared  buff/cache   available
    Mem:           7848        1523        5827          53         497        6033
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.

If you, however, want to get back to work as quickly as possible if one of your calculations erroneously uses too much memory, having swap will prolong the system-is-frozen period before the OOM killer solves the problem.
> the OOM killer "solves" the problem.

I fixed this for you. At least there are still some OSes that don't consider this a "solution."