Live data from Hacker News

Origin of the rule that swap size should be 2x of the physical memory

retrocomputing.stackexchange.com

21–30 of 78 posts

Re: Origin of the rule that swap size should be 2x of the physical memory

#21

Curious, How much swap have you personally allocated on your personal setup?

Why was this downvoted? I’m generally curious what current recommendations for swap are too! Edit: oh and I don’t have an actual personal system with swap configuration on it anymore to give my own answer anymore either.

same, completely fair thing to ask.

people are too negative these days :|

Re: Origin of the rule that swap size should be 2x of the physical memory

#22

Early BSD VM pre-allocated swap backing for every anonymous page — you couldn't allocate virtual memory without a swap slot reserved for it, even if the page was never paged out. When a process forks, the child needed swap reservations for the parent's entire address space (before exec replaces it). A large process forking temporarily needs double its swap allocation. If your working set is roughly equal to physical…

Today's swap also is not preallocated by the user. It is entirely handled by the OS itself. If it needs swap space to hibernate it will go ahead and allocate it itself.

Re: Origin of the rule that swap size should be 2x of the physical memory

#23

None of the answers are satisfying to me, tbh. I install more RAM so I can swap less . If I have 8 GB, then the 2x rule means I should have a 16 GB swap file, giving me 24 GB of total memory to work with. If I then stumble upon a good deal on RAM and upgrade to 32 GB, then if I never had memory problems with 24 GB, then I should be able to completely disable paging and not have a problem. But instead, the advice woul…

It's not meant for that kind of comparison. It's a variant of Simpson's paradox. Any individual system with a fixed set of tasks needs less swap when it gets more RAM. But when you look at the aggregate of systems, the systems that have more tasks to run get more RAM to run them, and systems with fewer tasks get less RAM. And since more tasks need more swap, everything scales together (though often not linearly).

Re: Origin of the rule that swap size should be 2x of the physical memory

#24
post #16

Earlier quoted context omitted.

Nope. Those are not the only answers I am seeing. I’m still curious though. 2x was nice because nobody really questioned it. Now that we have there doesn’t seem to be one “answer”. This is a fun/interesting question that comes up every now and then here and elsewhere :-) I suspect someone smarter than me about system tuning will have a much smarter and nuanced answer than “just use 2x”

I thought the modern advice was you don't need it at all. No more spinning disks, so the there's no speed gain using the inner-most ring, and modern OSes manage memory in more advanced, and dynamic ways. That's what I choose to believe anyway, I don't need anymore hard choices when setting up Linux :)

The main downside to not having swap is that Linux may start discarding clean file backed pages under memory pressure, when if you had swap available it could go after anonymous pages that are actually cold.

On a related note, your program code is very likely (mostly) clean file backed pages.

Of course, in the modern era of SSDs this isn't as big of a problem, but in the late days of running serious systems with OS/programs on spinning rust I regularly saw full blown collapse this way, like processes getting stuck for tens of seconds as every process on the system was contending on a single disk pagefaulting as they execute code.

Re: Origin of the rule that swap size should be 2x of the physical memory

#25

Curious, How much swap have you personally allocated on your personal setup?

Windows: I set min size to whatever is necessary to make RAM+swap add up to ~2 GBytes per CPU thread, to avoid problems with parallel Visual Studio builds. (See, e.g., https://devblogs.microsoft.com/cppblog/precompiled-header-pc...) Performance is typically fine with ~0.75+ GBytes RAM per job, but if the swapfile isn't preconfigured then Windows can seemingly end up sometimes refusing to grow it fast enough. Safest to configure it first

macOS: never found a reason not to just let it do whatever it does. There's a hard limit of ~100 GBytes swap anyway, for some reason, so, either you'll never run out, or macOS is not for you

Linux: I've always gone for 1x physical RAM, though with modern RAM sizes I don't really know why any more

Re: Origin of the rule that swap size should be 2x of the physical memory

#26
in 1997 people talking about it on the Slackware Usenet group:

    >Question: Why do you need 500MB of swap space? You would be better of
    >spending your money on more RAM than wasting it on so much swap space,
    >considering that it would most likely never be used anyways.

    I work with systems that have between 256MB and 1GB of RAM and
    between 4GB and 16GB available for Linux. My experience with other
    operating systems is that swap should be 2X to 3X RAM

    ...

    The info that I have read about Linux is that the 2x for swap space is
    only for those running less than 16mb of ram. Your swap space could be
    equal to your ram

    ...

    I know there are broken OSes out there where it's recomended to
    have 2x RAM swapspace, but Linux is not broken in that way.
    With Linux you should have  -  swapspace,
    and depending on your needs that might range from 0 to infinity
    MBs of swap.

    ...

    THIS IS CRAZY!!!! YOU DON'T KNOW WHAT THE F--K YOU'RE TALKING ABOUT.
It goes downhill from there..

https://groups.google.com/g/alt.os.linux.slackware/c/hWy0h_S...

Re: Origin of the rule that swap size should be 2x of the physical memory

#27
post #5

Earlier quoted context omitted.

Zero. My office workstation has 48 GB of RAM, my home computer has 64 (I went a bit overboard). I have very bad memories of swap thrashing and the computer becoming totally unresponsive until I forced a reset; if I manage to fill up so much RAM, I very much prefer the offending process to die instead of killing the whole computer.

It's funny how people think they're disabling swapping just because they don't have a swap file. Where do you think mmap()-ed file pages go? Your machine can still reclaim resident file-backed pages (either by discarding them if they're clear or writing them to their backing file if dirty) and reload them later. That's.... swap. Instead of achieving responsiveness by disabling swap entirely (which is silly, because e…

mmap is not swap. It's using the same virtual memory mechanisms to load/dump pages to disk. The policy for when to read and write those pages is completely different.

Re: Origin of the rule that swap size should be 2x of the physical memory

#28
post #16

Earlier quoted context omitted.

Nope. Those are not the only answers I am seeing. I’m still curious though. 2x was nice because nobody really questioned it. Now that we have there doesn’t seem to be one “answer”. This is a fun/interesting question that comes up every now and then here and elsewhere :-) I suspect someone smarter than me about system tuning will have a much smarter and nuanced answer than “just use 2x”

I thought the modern advice was you don't need it at all. No more spinning disks, so the there's no speed gain using the inner-most ring, and modern OSes manage memory in more advanced, and dynamic ways. That's what I choose to believe anyway, I don't need anymore hard choices when setting up Linux :)

I don't think that's correct. Having swap still allows you to page out rarely-used pages from RAM, and letting that RAM be used for things that positively impact performance, like caching actually used filesystem objects. Pages that are backed by disk (e.g. files) don't need that, but anonymous memory that e.g. has only been touched once and then never even read afterwards should have a place to go as well. Also, without swap space you have to write out file backed pages, instead of including anonymous memory in that choice.

For that reason, I always set up swap space.

Nowadays, some systems also have compression in the virtual memory layer, i.e. rarely used pages get compressed in RAM to use up less space there, without necessarily being paged out (= written to swap). Note that I don't know much about modern virtual memory and how exactly compression interacts with paging out.

Re: Origin of the rule that swap size should be 2x of the physical memory

#29

Early BSD VM pre-allocated swap backing for every anonymous page — you couldn't allocate virtual memory without a swap slot reserved for it, even if the page was never paged out. When a process forks, the child needed swap reservations for the parent's entire address space (before exec replaces it). A large process forking temporarily needs double its swap allocation. If your working set is roughly equal to physical…

Today's swap also is not preallocated by the user. It is entirely handled by the OS itself. If it needs swap space to hibernate it will go ahead and allocate it itself.

It does? Last I checked Linux doesn't do dynamic swap sizes, and while Windows has dynamic swap sizes it has a separate big non-dynamic file for hibernation. I have no idea what MacOS does.

Re: Origin of the rule that swap size should be 2x of the physical memory

#30
post #5

Curious, How much swap have you personally allocated on your personal setup?

Zero. My office workstation has 48 GB of RAM, my home computer has 64 (I went a bit overboard). I have very bad memories of swap thrashing and the computer becoming totally unresponsive until I forced a reset; if I manage to fill up so much RAM, I very much prefer the offending process to die instead of killing the whole computer.

I'm not an expert, but aren't you just reducing the choice of what pages can be offloaded from RAM? Without swap space, only file-backed pages can be written out to reclaim RAM for other uses (including caching). With swap space, rarely used anonymous memory can be written out as well.

Swap space is not just for overcommitting memory (in fact, I suspect nowadays it rarely ever is), but also for improving performance by maximizing efficient usage of RAM.

With 48GB, you're probably fine, but run a few VMs or large programs, and you're backing your kernel into a corner in terms of making RAM available for efficient caching.

Post reply on HN