r/AskProgramming • • 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!

76 Upvotes

40 comments sorted by

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.

7

u/Dobby_1235 Jan 20 '22

Why do they use new engines for each tab?

39

u/KingofGamesYami Jan 20 '22

(1) isolating information for security and (2) so when a website does something immensely stupid and crashes the engine, only that tab gets killed, rather than the entire browser.

65

u/djnattyp Jan 20 '22

So the porn site you're looking at in one tab can't grab your info from the banking site you have open in another.

5

u/nixfreakz Jan 21 '22

Lol, most porn sites have higher security then banks do. They are always on the cutting edge of things.

5

u/coldoil Jan 20 '22

Sandboxing.

2

u/playaspec Jan 20 '22

Is there a way to force it to cache exclusively to disk?

5

u/KingofGamesYami Jan 20 '22

Not that I am aware of, aside from having another program use all your RAM (which I have done before).

1

u/ThatWontCutIt Jan 21 '22

Why not... put not in use tabs in hard drive and make a buffer in RAM connected to tabs in hard drive?

3

u/KingofGamesYami Jan 21 '22

Because hard drive access is slow as fuck compared to RAM.

Also, chromium browsers do automatically this for inactive tabs. You can see the status and manage the behavior manually at chrome://discards in Google Chrome.

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.

It seems insane to me that a simple app showing mainly text uses multiple gigs.

A web browser is one of the most complex applications on your machine.

8

u/pe1uca Jan 20 '22

I was thinking that, it might be true that it handles mostly text, but that's the issue, it needs to parse that text and the render whatever that text means.
Also currently a lot of pages are not just text, I mean, reddit has a lot of images, now imagine sites like YouTube, Netflix, Spotify, etc that have streaming.
And some sites use a lot of animations that have engines of themselves (don't quote me on this, I think I read it some where).

Still as others have pointed out, the amount of resources web developer use is just unbelievable. Most don't have a sense of optimization.
And I don't blame them individually, is how the web development has evolved that makes and allows them to be like this.

3

u/[deleted] Jan 21 '22

As a web developer, this is so true it hurts. This is one of the reasons I find it kind of counterintuitive for web devs to have beefy machines... If your simple web app is struggling with resources on a power laptop, your average end user is going to have a bad time.

3

u/KingofGamesYami Jan 21 '22

To be fair, my laptop is running a SQL Server, a cloud emulator, three backend applications, two IDEs, and the web app. End user will be running... the web app and probably excel.

1

u/MyPythonDontWantNone Jan 21 '22

To be fair, the end user is probably running 23 web apps in different tabs.

1

u/KingofGamesYami Jan 21 '22

...which, at least for my application, would still take less system resources than the SQL server alone.

2

u/YMK1234 Jan 21 '22

I call BS on that theory tbh. Look at Opera back when they had their own engine. I could hold a good 50 pages open on my 256MB machine, all of which happened in a true MDI (so in theory you could display all those pages side by side in the browser window), and no delays between tab switches.

6

u/Alainx277 Jan 21 '22

I imagine back then websites were a lot smaller and simpler? You can't really compare that to the standard nowadays.

1

u/YMK1234 Jan 21 '22

Nah, memory hogging of Chrome was a major problem way back then, too. Sure it has gotten worse since then, but it's not the main issue.

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.

22

u/[deleted] Jan 20 '22

And in addition to that most JS developers don't know about the concept of "limited resources".

16

u/mrwizard420 Jan 20 '22

Hoooly shit, I am just breaking into Node.js and Flutter after spending years in C and Python... The amount of content that goes into a JavaScript web app is absolutely insane. I went to go move a sample project from one folder to another, and there's literally thousands of JS files just from basic packages.

13

u/[deleted] Jan 20 '22

Yes, I know exactly what you're talking about. Having a C++ background myself I was shocked to see how relentless Node.JS is using up disk space and RAM. I have no idea how this became an accepted thing but I am sure that back then I learned programming in a way more resource efficient manner.

10

u/KingofGamesYami Jan 20 '22

laughs in Qt installation size

4

u/Thaufas Jan 21 '22

<Laughs 3x louder in anaconda>

4

u/[deleted] Jan 21 '22

giggles in binary executable size

1

u/[deleted] Jan 21 '22 edited Jan 03 '26

unwritten paint rain plate rustic retire judicious groovy different support

This post was mass deleted and anonymized with Redact

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.

2

u/Treyzania Jan 21 '22

Not sure why you're being downvoted, you're right.

1

u/maxximillian Jan 21 '22

Thats simply not true, who cares how much money someone has when they have credit at insane interests rates. `there's a shit load of money to be made off poor people. There was a huge housing bubble thanks to getting people loans for houses that couldnt afford houses.

-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

1

u/Alainx277 Jan 21 '22

People aren't getting your joke... sad.

-1

u/Thaufas Jan 21 '22

Probably just a malloc call somewhere.

malloc()...Now that's a name I've not heard in a long time, a very long time....LOL!

First, nobody is coding in C for the web, unless they aspire to Hellraiser levels of self-loathing and torment. Admittedly, I once wrote a simple CGI based web server in C . However, that was a very specialized project because of a constraint we had with a particular piece of laboratory robotics instrumentation.

Also, that was over 20 years ago, and even then, there were probably better ways to solve the problem. However, I was very proficient with C back then, and I knew I could produce the code we needed very quickly.

Second, C++ was already growing enormously in popularity by the late 1990s, so by the early 2000s, anyone using C++ was (or should have been) using operator new and delete (or their array counterparts when declaring arrays) instead of malloc(), calloc(), and realloc().

Third, nowadays, modern C++ developers aren't using old fashioned naked pointers.

I don't truly know the answer why web browsers consume such vast amounts of memory. However, the explanations that other people here are giving around caching and the complexity of a JS engine, such as Chrome's V8, really resonate with me.

3

u/Alainx277 Jan 21 '22

It's a joke. Also, I wonder what those smart pointers use to allocate their memory...

1

u/Thaufas Jan 21 '22

It's a joke. Also, I wonder what those smart pointers use to allocate their memory...

Well, if you really want to go to absurd levels, those smart pointers aren't using malloc(), calloc(), or realloc(). So, what do you think operator new and delete are using to allocate memory?

0

u/[deleted] Jan 21 '22

[deleted] — view deleted comment

1

u/Thaufas Jan 21 '22

LOL! OK, then carry on!