Why do browsers sometimes drain a lot of RAM until rebooted?

488 views

Why do browsers sometimes drain a lot of RAM until rebooted?

In: 5662

18 Answers

Anonymous 0 Comments

This is a bit too complicated for ELI5, so I’ll have to gloss over a lot of details.

There are multiple layers of memory management, at least two. There’s the virtual memory system that manages the entire computer and a heap allocator that manages each process.

The reason why you need this division is that creating a memory object needs to be *really* fast. Switching CPU protection modes is too slow; you should make memory-management decisions within each application without calling the operating system.

Long-lived applications hold on to some of their free memory so that they don’t need to immediately turn around and ask for it back. **This means a system monitor cannot show you that free memory.** The OS doesn’t know about it!

The system-level layer doesn’t ask for memory back. **It takes what it needs,** if and when it needs it. It measures how long it has been since memory was accessed, identifies “cold” pages that probably won’t be accessed again, backs them up to swap if needed, and unmaps those pages without the application even knowing.

The result of this multilayer model is that if you have only one long-running application that can find uses for memory, that application *should* end up filling your physical memory. On a scale of 1 to fine this is usually fine. **You can’t judge the memory usage of a browser unless you have something else competing for memory.** That’s when you can meaningfully look at statistics like refaults and pressure stall.

If there’s actually too much memory in use, performance falls apart as the system spends more and more time juggling data and less executing instructions. The solution then is to crash tasks – and without that threat, applications probably shouldn’t proactively return free memory to the OS.

[Firefox has the ability to detect when the system is in trouble and will back off](https://hacks.mozilla.org/2021/10/tab-unloading-in-firefox-93/). Android tells apps to design a response for “free memory or crash” threats. Server Linux can use Facebook’s new-ish oomd – if a server starts to swap heavily then it should log for statistical analysis, crash, and fix itself.

You are viewing 1 out of 18 answers, click here to view all answers.