Live data from Hacker News

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

retrocomputing.stackexchange.com

61–70 of 78 posts

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

#61
post #28
post #16

Earlier quoted context omitted.

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, wit…

That only helps if you don't have much free RAM. If you've got more free RAM than you need cache (including disk cache), swap only slows things down. With RAM prices these days, getting enough RAM is not worth it to avoid swap. IME on a desktop with 128GiB of RAM & Zswap I've never hit the backing store but have gone over 64GiB a few times. I wouldn't want to have pay to rebuild my desktop these days, 128GiB of ECC RAM was pricey enough in 2023!

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

#62

Earlier quoted context omitted.

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.

You guys are getting Linux to hibernate!?

[deleted]

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

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

Luckily we're not in the spinning HDDs thrashing a working set in and out of 128 MB of primary memory days anymore. We have laptops that ship with SSDs that read/write at 6 GB/s.

I was experimenting with some graphics algorithm and had a memory leak where it would leak the uncompressed 12 MP image with every iteration. I was browsing the web when waiting for it to process when I wondered why it was taking so long. That's when I noticed it was using 80+ GB of swap just holding onto all those dead frames. It finished and meanwhile it had no noticeable performance impact on whatever else I was doing.

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

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

I did similar with my 32GB laptop, but it was fairly flaky for ~4 years and I just recently put 48GB of swap on and it's been so much better. It's using over 20GB of the swap. The are cases in Linux where running without swap results in situations very similar to swapping too much.

What kind of workload do you run that consumes 20 GB of swap???

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

#65

Earlier quoted context omitted.

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.

It is doing exactly what swap is doing. That it's swap with a different policy doesn't make it not-swap. Also, that separate policy shouldn't even exist. For LRU/active-list/inactive-list purposes, why does it matter whether a page is anonymous or file-backed? If you need it, you need it, and if you don't, you don't. No reason for anonymous and file-backed memory to be separate sub-sub-systems under vm.

Anonymous memory and files have different access patterns. Files are frequently read sequentially and only once, so there is no need to keep them in memory. When files are read, cached memory pages are put into inactive file LRU first and promoted to active LRU only on the second access.

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

#66
post #3

The OP clearly states that he wants to know the earliest origin of the rule , and the only answers he gets are people giving their own opinions on how much swap space you should have. Too bad because it's an interesting question that I would also like to know the answer to.

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'm the OP. I got myself into collecting falsehoods people believe about Linux swap and OOM[1]. There is an entry about this 2x rule in this collection, with my answer on how to select swap size.

My question on Retrocomputing.StackExchange is my attempt to add some historical background to this entry.

[1]:https://alexeydemidov.com/2025/05/15/falsehoods-people-and-L...

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

#67

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…

TBF, I think overcommit was and remains an ugliness in how we manage memory. I wish we'd solved the fork commit-charge-spike issue by encouraging vfork (and later, posix_spawn) more heavily, not by making the OS lie about the availability of memory. The ship's long sailed though, so even I run with overcommit enabled and only grumble about what might have been.

Overcommit allows much better memory utilization. People disable overcommit all the time and get surprised when their programs start failing mallocs while there are still tons of discardable page cache in the system.

https://unix.stackexchange.com/q/797835/1027

https://unix.stackexchange.com/q/797841/1027

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

#68

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…

The key parameter for swap size is "how memory-hungry things you want to run", which isn't easy to measure, but paying for installed RAM is a somewhat usable proxy metric for that. If you were happy with 8gb, it's some evidence that your apps don't need much memory (and swap), but if you needed to pay for an upgrade to 32gb, that's some evidence that you're the kind of user who needs much more swap than those with 8gb of RAM.

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

#69
post #64

Earlier quoted context omitted.

I did similar with my 32GB laptop, but it was fairly flaky for ~4 years and I just recently put 48GB of swap on and it's been so much better. It's using over 20GB of the swap. The are cases in Linux where running without swap results in situations very similar to swapping too much.

What kind of workload do you run that consumes 20 GB of swap???

Chrome and Slack. :-(

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

#70

Earlier quoted context omitted.

It is doing exactly what swap is doing. That it's swap with a different policy doesn't make it not-swap. Also, that separate policy shouldn't even exist. For LRU/active-list/inactive-list purposes, why does it matter whether a page is anonymous or file-backed? If you need it, you need it, and if you don't, you don't. No reason for anonymous and file-backed memory to be separate sub-sub-systems under vm.

Anonymous memory and files have different access patterns. Files are frequently read sequentially and only once, so there is no need to keep them in memory. When files are read, cached memory pages are put into inactive file LRU first and promoted to active LRU only on the second access.

It is possible to unify things behind a single mechanism, yet apply different policies to different instances of this mechanism depending on circumstances and heuristics. We do not need almost entirely disjoint paging systems in the Linux kernel to notice that some kinds of memory have access patterns different from other kinds of memory. Instead of guessing based on whether someone used MAP_ANONYMOUS, we should observe what a program is actually doing.
Post reply on HN