News for hackers. Stuff that matters.
Re:The Linux kernel's inability to gracefully handle low mem... by jamienicol on Tuesday August 06, 2019 @04:47AM (#20623425)
It's spread across all parts of the browser, but speaking as a Firefox graphics engineer, we use quite a lot of memory. Painting web pages can be slow, so we try to cache as much as possible. When elements scroll separately, or can be animated, we need to cache them in separate buffers. If we get the heuristics wrong (and it's hard to get it right for every web page out there) this can be explosive. It's not helped by the fact that graphics drivers can frequently bring down the whole process when they run out of memory. It's a hard problem, but webrender will help as it needs to cache less.
Re:The Linux kernel's inability to gracefully handle low mem... by cosarara on Tuesday August 06, 2019 @09:25AM (#20624913)
Maybe the browser should try to discard some cached data when the system is out of memory. Then some things in the browser would be slower, but the operating system wouldn't hang.
Re:The Linux kernel's inability to gracefully handle low mem... by bzbarsky on Tuesday August 06, 2019 @09:36AM (#20625000)
The browser _does_ do that. The hard part is detecting "the system is out of memory". Some OSes notify you when that happens, and Firefox listens to those notifications and will flush caches. Some OSes will at least fail malloc and let you detect out-of-memory that way. Linux does neither, last I checked.

Disclaimer: I work on Firefox, but not the details of the OS "listen for memory pressure" integration.

Re:The Linux kernel's inability to gracefully handle low mem... by cosarara on Tuesday August 06, 2019 @09:47AM (#20625101)
Any userspace process can see how low the memory is though, firefox could do it itself. Still, if a notification system is already used in other OSes, a very easy solution would be to add such notification channel in userspace so that any process could ask firefox to free memory. Right now I am using earlyoom to save my system from freezing. It sometimes kills firefox, sometimes dbeaver, sometims VMs. But if it could tell firefox to chill for a bit and free memory, then I could avoid the massacre (at least sometimes).
Re:The Linux kernel's inability to gracefully handle low mem... by bzbarsky on Tuesday August 06, 2019 @10:39AM (#20625550)
> Any userspace process can see how low the memory is though

How, exactly?

Or put another way, how do you reliably tell apart "we are seriously thrashing" and "resident memory is getting close to the physical memory limits but there is plenty of totally cold stuff to swap out and it won't be a problem" from userspace? The kernel is the only thing that can make that determination somewhat reliably.

Re:The Linux kernel's inability to gracefully handle low mem... by cosarara on Tuesday August 06, 2019 @11:13AM (#20625864)
Maybe there's a better way than this, but the same way earlyoom decides it's time to kill processes (% RAM usage) firefox would decide it's time to free cache. While using the 100% of RAM might be the optimal state if you aren't going to use more than that, it's not safe.
Re:The Linux kernel's inability to gracefully handle low mem... by heavenlyblue on Tuesday August 06, 2019 @05:48AM (#20623727)
So let's say I'd like to write a memory-efficient web page, what should I avoid then?
Re:The Linux kernel's inability to gracefully handle low mem... by bzbarsky on Tuesday August 06, 2019 @09:44AM (#20625075)
Based on my experience as a Firefox developer investigating memory usage reports, the worst-performing "normal" web pages in terms of memory have:

1) Lots of script (megabytes). 2) Possibly loaded multiple times (e.g. in multiple iframes). 3) Possibly sticking objects into global arrays and never taking them out (aka a memory leak for the lifetime of the page). 4) Loading hundreds of very large images all at the same time. 5) Loading hundreds or thousands of iframes that all have nontrivial, if not huge, amounts of script. Social media "like" buttons often fall in this bucket.

There are obviously also pathological cases: if your HTML has 1 million elements in it (not a hypothetical; I've seen this happen), memory usage is going to be high, obviously. And arguably having a page with thousands or hundreds of thousands of JS functions is "pathological" too, but it's pretty normal nowadays...

Re:The Linux kernel's inability to gracefully handle low mem... by kevingadd on Tuesday August 06, 2019 @07:56AM (#20624318)
For video memory/tile memory usage, avoid anything fancy that's hard to rasterize, for starters: Think things like rounded borders, drop shadows, opacity, transparent background images, etc. The more complex it is to draw your page the more likely that it will end up being cached into temporary surfaces and composited and stuff. For a while for some absurd reason Twitter's main layout had a bunch of rectangles with opacities of like 0.95 or 0.99, so all the layers had to get cached into separate surfaces even though you could barely tell it was happening. Getting them to fix that made the site faster for basically every Firefox and Chrome user. They hadn't noticed.

For JS and DOM memory usage you can use the browser's built in profiler to get a pretty good estimate of where things are going and what you've done wrong.

Re:The Linux kernel's inability to gracefully handle low mem... by gnode on Tuesday August 06, 2019 @07:17AM (#20624126)
Rather than guess at what to avoid, you should make use of the memory profilers which Firefox and Chromium developer tools provide. Apparently Firefox's memory profiler is an add-on: https://developer.mozilla.org/en-US/docs/Mozilla/Performance...
Re:The Linux kernel's inability to gracefully handle low mem... by heavenlyblue on Tuesday August 06, 2019 @08:24AM (#20624473)
Yeah, but this is a pigeonholing principle.

I don’t want to spend my time developing something only to discover it doesn’t perform well due to some reason.

I would prefer to not use any of the performance killers in the first place.

Re:The Linux kernel's inability to gracefully handle low mem... by gnode on Tuesday August 06, 2019 @11:48AM (#20626240)
Firstly, avoid leaking memory (including objects like images and DOM nodes) in JavaScript. Leaking memory here means retaining a reference beyond the end of the object's use. The garbage collector only collects memory which is no longer referenced; it does not attempt to analyse when a reference is no longer used.

Secondly, avoid including unnecessary resources. Many web pages include many libraries which are then mostly unused. Some packaging tools can help eliminate such unused code.

A memory profiler helps in both cases: it detects leaks, and it measures the cost of resources, allowing you to make educated decisions about their inclusion.

Re:The Linux kernel's inability to gracefully handle low mem... by Crinus on Tuesday August 06, 2019 @05:52AM (#20623744)
HTML3+
Re:The Linux kernel's inability to gracefully handle low mem... by na85 on Tuesday August 06, 2019 @06:53AM (#20624017)
JavaScript
Re:The Linux kernel's inability to gracefully handle low mem... by heavenlyblue on Tuesday August 06, 2019 @06:55AM (#20624027)
Is that a joke, or are you essentially saying that if I used WebAssembly, then most of the memory usage would go down?
Re:The Linux kernel's inability to gracefully handle low mem... by superkuh on Tuesday August 06, 2019 @09:59AM (#20625214)
He's not wrong. If you disable JS by default tabs will take much, much less RAM. Sites that require JS aren't worth the time anyway. Unless you're literally writing an application there's no reason to require executing code to render in text and images. And there's absolutely no reason to not have a no-JS fallback. In fact, there should be a real HTML skeleton first upon which you write JS enhancements.

But these are all things you do if you want to make a webpage for people. If your main concern is corporate profit or saving institutional funds then SPAs and requiring JS for obfuscation makes sense in the anti-user way that corps trend to. It's just faster. Who cares if it's anti-user when profits are on the line?

Re:The Linux kernel's inability to gracefully handle low mem... by Narishma on Wednesday August 07, 2019 @04:02PM (#20638323)
Is there a setting to disable all of that caching? In other words, prioritize memory usage over performance?