Hacker News new | ask | show | jobs
by loeg 3083 days ago
All of this boils down to "because buying more disk is cheaper than buying more RAM" and "avoid memory contention."

The author discusses the situation as if the quantity of RAM is fixed and swap can be added (or not). But that isn't the only possibility — you can also add more RAM (it'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.

4 comments

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 more than enough RAM for all the cached pages the system wants to cache. On my laptop right now (booted today), I have 500MB of cached pages and 5.8 gigabytes of free memory. On my server (booted 499 days ago) I have 700MB of cached pages and 6 gigabytes of free memory.

If I were running out of memory [be it for cache or applications], I'd prefer to have more RAM than add swap. Yes, I keep calling it emergency memory.

> If you assume that the kernel is not paging out memory that you actually use when not under pressure, swap doesn't hurt you.

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.

>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.

> 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.

> you can also add more RAM (it's just expensive)

Or just impossible

Many laptops still have remarkably low maximum-RAM limits. The ones I have here ( Dell SME & corporate types ) are 4GB and 8GB. I live in constant fear of the solid-blue disk-access lamp.

And when I boot-up after a swap-thrash I am scolded for an unclean shutdown :(

You have not given an actual example of "impossible." A computer that can hold more RAM than 4-8 GB is possible to obtain, at some cost. You may object to the cost ("expensive"), but it is not impossible.
Well, considering that current Intel laptop chipsets support only 16g of RAM, this is pretty much a hard limit. Yes, you can switch to desktop hardware, but that is limited to usually heavier devices which run shorter on battery. So there are some very practical limits on increasing the RAM.
Agreed, in most situations.

One counter example:

If your processes in sum tend to A) access many disk locations, at large total disk space B) hold a lot of underused data in ram

This isn't totally impossible. Maybe an Ethereum node with a script doing a bunch of data reads, running side by side with hundreds of Chrome tabs, few of which are regularly accessed. (Totally hypothetical, of course...)

Swapping some rarely used ram out so the OS can buffer disk into ram seems like a reasonable approach (although maybe even more reasonable is: close some tabs).

Your point still stands that more ram is strictly as good or better performance in this scenario, but you might be able to get an equivalent performance boost much more cheaply with some swap space for the underused ram. Also, upgrading past 32 GB ram starts to veer from expensive to impossible on a laptop.

Author assumes that you actually have some disk space to spare, which is not always the case.

I wonder if instead of using 4GB RAM, I'd setup 3GB RAM + 1GB swap space in RAM disk would result in much wiser OOM killer decisions (or stability).

Showing superiority of such configuration would probably convince all swap sceptics

In that case, you should use zram or zswap, it's what Chromebooks do to avoid swapping to their limited-performance flash storage, despite only 2GB or 4GB RAM.