r/AskProgramming • u/superfunawesomedude • Jan 20 '22
How do browsers use so much ram?
I am a game-dev and I find the amount of ram browsers use today mind boggling. They use more ram than many AAA games, which have a legit use-case as they need to hold gigabytes of uncompressed textures and audio in memory. It seems insane to me that a simple app showing mainly text uses multiple gigs.
I have heard that the culprit is javascript. But How? I dont understand how that could happen. In games programming the memory used by all the code + all data is negligible, 99% of the ram is used by art assets like textures and audio.
How can javascript scripts (which are doing a very simple job) use so much?
I did comp-sci and am a professional programmer, so please dont shy away from deep technical explanations, just bear in mind I know nothing about internetty things like browsers.
Thanks!
61
u/KingofGamesYami Jan 20 '22
Well, one part of it is sandboxing. Modern web browsers run a completely seperate engine for each tab. So all the memory needed to fire up a parsing engine for HTML and CSS, plus the JavaScript interpreter is allocated to each tab.
https://example.com, for example, takes 36 kilobytes of RAM on my machine.
Another part of this is Chromium's aggressive caching and optimization. If you have free RAM, chrome will take it to cache things that, while not currently being used, may be used soon. Want your browser to use a lot less RAM? Have a different process using it all.
34
u/khedoros Jan 20 '22
A lot of it is caching things that might be needed. Information about page rendering that it uses to load quickly when you're jumping back and forward, for example.
A web browser is one of the most complex applications on your machine.
14
u/Felicia_Svilling Jan 20 '22
Most modern web pages are whole little js programs running in your browser, one in each tab. It adds up.
10
u/[deleted] Jan 20 '22 edited Jan 02 '26
teeny sable cover sulky saw touch intelligent license simplistic bow
This post was mass deleted and anonymized with Redact
4
u/nuKsBe Jan 21 '22 edited Jan 21 '22
Done both,
Slight additional notes (but unrelated to ram mostly):
HTML, JS or even CSS interpreter are much more advance than we might think of. For instance even a CSS engine holds just in time compilation mechanisms nowadays.
Regarding JS code complexity, I think people got used to be overly dependant on libraries for a good reason : lack of compatibility between browser, starting with the advent of jquery which solved a lot of issues. nowadays there is transitive dependencies in every direction so it is exponential (ie 10 external dependencies might means 100~1000). I guess the same will happens after some times in C++ now that there are decent package managers (though it's at least 10 years late). It's much harder to do a memory leak in JS than in C++ when following conventions (also simply because JS is nowadays mainly used with functional code rather than OOP), and everything is held by reference by default, and optimisation is done at frameworks (regarding DOM) level rather than in the application most of the time, so even "bad developers" don't do shit (well there are exception, if you want to do 60fps interactive animations for instance that's a speciality by itself, or at least it used to be), and as you said, web assets are light.
If you want a good resource regarding RAM issues in JS, check this: https://www.html5rocks.com/en/tutorials/memory/effectivemanagement/ This explains gmail's issues in the early days.
2
u/skellious Jan 21 '22
it's effectively an entire VM or multiple VMs running in your machine.
2
u/[deleted] Jan 21 '22
Inefficient developers (relying on heavy dependencies for each small thing), heavy tracking, single page applications, aesthetics (custom fonts), compatibility (loading assets for every browser that's different). Plus, rendering is also quite lazy, as people rely on frameworks that generate a lot of DOM, with a lot of custom effects. On top of it all, there's all the media that isn't always optimized for its way of consumption.
Add to that the fact that tabs are isolated in memory, and each tab is a clone of the memory in another tab if you open the same website.
7
u/knoam Jan 20 '22
The web is funded by advertising. Advertisers only care about people with money. People with money have relatively new, powerful hardware. There's no money in making websites lean and efficient. There's money in loading it up with big splashy images and loads of JavaScript ad tech to snoop on users and figure out exactly who they are.
-1
u/ZecroniWybaut Jan 21 '22
Most self-taught developers go into web development and have no idea of certain techniques to reduce usage of resources.
0
u/[deleted] Jan 21 '22
[deleted] — view deleted comment