Live data from Hacker News

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

retrocomputing.stackexchange.com

71–78 of 78 posts

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

#71

Earlier quoted context omitted.

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 obse…

All LRUs, file-backend and anonymous are handled by the same code. There are some conditionals here and there - executable pages are promoted to active LRU on the first access, locked pages (if I remember correctly) too, but all pages go through the same cycle. See this Linux Plumbers conference presentation https://www.youtube.com/watch?v=0bnWQF7WQP0 with the following slides https://d3s.mff.cuni.cz/files/teaching/nswi161/2023_24/08_li...

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

#73
post #30
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'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…

The point is to have so much RAM that you don't need to offload anything.

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

#74
post #64

Earlier quoted context omitted.

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

Chrome and Slack. :-(

Oh god... My company uses Teams and it is one of the main reasons why I installed so much RAM on my workstation. Part of me wishes that the recent increase in RAM prices will force companies to reduce the ridiculous memory footprint of their software.

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

#75
post #20

Earlier quoted context omitted.

On systems with 32/64/128 GB of ram, I'll typically have a 1GB or 2GB swap. Just so that the system can page out here and there to run optimally. Depending on the system, swap is typically either empty or just has a couple hundred MB kicking around.

On what OS are you using these settings? I found that Windows will refuse to allocate more virtual memory when the commit charge hit the max RAM size even if there is plenty of physical memory left to use. I have 64 GiB of RAM and programs would start to crash at only 25 GiB of physical memory usage in some workloads because of high commit charge. I had to re-enable a 64 GiB SWAP file again just to be able to actuall…

> On what OS are you using these settings?

Linux. I also don't typically run any individual workload that would consume all system ram. Not loading a giant scientific model into memory all at once for fast processing or anything like that, so mileage will certainly vary based on requirements.

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

#76

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…

The OP here. I think this is the correct answer. I'm aware of the strict swap reservation, even mentioned it in the question, but I didn't realize that it doubles memory on fork even with copy-on-write, so I assumed that it implies a minimum of 1x, not 2x. This is specific to BSD and SunOS, but with them being popular in the 80s, the 2x rule became widespread.

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

#77
post #51

Earlier quoted context omitted.

You guys are getting Linux to hibernate!?

I managed to get mine to wake up from sleep, haven't recovered enough to attempt hibernation yet.

on my thinkpad with fedora hibernate works fine. i use it frequently. i even use it when for an unknown reason my usb-c headphones stop working. i don't know why. but after hibernate they work again. somehow waking up from hibernation fixes the problem

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

#78
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 ran with a setup like this for a bit, but I experienced far worse thrashing (and far more sudden onset) than I did with swap enabled. You need to take some extra steps to get a quick and graceful failure on RAM exhaustion.
Post reply on HN