News for hackers. Stuff that matters.
Re:The Linux kernel's inability to gracefully handle low mem... by JJMcJ on Tuesday August 06, 2019 @02:12AM (#20622612)
> vast amounts of memory for each tab

What underlies this? I am astounded to see 1GB of memory returned when I close a couple of tabs.

Chrome and Firefox both seem like this.

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?
Re:The Linux kernel's inability to gracefully handle low mem... by vaylian on Tuesday August 06, 2019 @02:27AM (#20622701)
To be honest: I do not know. But given how fat most websites are today, I am not that surprised that so much memory is needed. Yes, there is still a major leap from a couple of megabytes to a full gigabyte, but with so many DOM nodes and JS objects I can imagine that even a resource-conservative browser will have trouble keeping memory usage low.

Or are there any browsers where you observe significantly less memory usage on the same websites? (Ignoring limited browsers like Lynx of course)

Re:The Linux kernel's inability to gracefully handle low mem... by ygra on Tuesday August 06, 2019 @02:56AM (#20622824)
Most modern browsers cache a lot of data in memory as well to make things like navigating back snappy and avoiding a full page re-load. It's also necessary to cache most of the state of the previous page to allow retaining form fields if you accidentally navigated away (as some forms are only in the DOM, created by JS, and not part of the original HTML).

I believe to have read somewhere that at least Chrome listens to low-memory situations broadcast by the OS and will evict such caches. So while it uses a lot of memory as long as memory is available, it will also release much of it if necessary.

Re:The Linux kernel's inability to gracefully handle low mem... by PeCaN on Tuesday August 06, 2019 @03:39AM (#20623046)
This makes sense but is somewhat annoying since a web browser is not the only program running on my computer (but apparently wants to be) and eats up RAM that could be used for the OS file cache.

I wish there was some sort of allocation API specifically for allocating caches so that recently accessed files could kick out a web browser's cache of a not-so-recently accessed tab or vice versa.

Re:The Linux kernel's inability to gracefully handle low mem... by nitrogen on Wednesday August 07, 2019 @12:33PM (#20636294)
POSIX does have something sort of like this in madvise(), but I couldn't find a specific option for the semantics you described.
Re:The Linux kernel's inability to gracefully handle low mem... by bjoli on Tuesday August 06, 2019 @03:03AM (#20622856)
On a low memory computer I had configured FF to not show images unless I alt-clicked them. Together with not using JavaScript this meant using significantly less memory. I suspect browsers still have to have raw bitmaps in memory, which for the 2mb jpegs you are fed everywhere quickly adds up...
Re:The Linux kernel's inability to gracefully handle low mem... by chmod775 on Tuesday August 06, 2019 @03:29AM (#20622987)
They have to be in memory somewhere. They technically don't have to be kept in RAM if they are uploaded to VRAM though.

Assumining RGBA it's just over 8MB for a 1920x1080 image.

Re:The Linux kernel's inability to gracefully handle low mem... by gnode on Tuesday August 06, 2019 @07:29AM (#20624183)
Images could be kept compressed until they are painted. Most GPUs support texture compression, so don't need to keep a bitmap for compositing.
Re:The Linux kernel's inability to gracefully handle low mem... by chmod775 on Tuesday August 06, 2019 @08:26AM (#20624491)
Texture compression is inherently lossy, so it isn't an option.

It's also really dependend on your textures and what you want to do with them, you don't want browser to just go give it their best shot at compressing your company logo.

Re:The Linux kernel's inability to gracefully handle low mem... by iamnotacrook on Tuesday August 06, 2019 @04:09AM (#20623211)
You want your browser to be fast. Browsers are often the only thing running. You have a bunch of ram. Unused ram is pointless. Disks are large and fast and are used for swap. It's not clear why you're suprised, what the problem is, or why anyone would put any effort into optimising a browser for memory usage. The number of people who have tens or hundreds of tabs open in the real world isn't as large as it is on HN and other tech sites, and for many people who do just keeping the url around so the site can be reloaded is probably good enough.
Re:The Linux kernel's inability to gracefully handle low mem... by rat9988 on Tuesday August 06, 2019 @05:05AM (#20623514)
> Browsers are often the only thing running.

If I had to choose one program that is proportionally used the least alone, I would have voted browsers.

Re:The Linux kernel's inability to gracefully handle low mem... by plasticchris on Tuesday August 06, 2019 @02:43AM (#20622775)
Javascript? I use noscript and it's less than 200MB/tab, even with major news sites and what not open.
Re:The Linux kernel's inability to gracefully handle low mem... by agent008t on Tuesday August 06, 2019 @05:32AM (#20623660)
200MB/tab just to display some text with markdown and maybe some small images? Isn't that insane?
Re:The Linux kernel's inability to gracefully handle low mem... by ryandrake on Tuesday August 06, 2019 @02:57AM (#20622829)
Answer is too simple. Exactly what is taking up what memory? I'd love to see the annotated memory map of these processes. M Kb of javascript text. N MB of image data, O MB of this, P MB of that.

Unpopular opinion time:

My guess is most developers don't care and are not even looking at this anymore, either during development or after release. Nobody seems to even know how much memory their program allocates and how quickly it allocates that memory under various running conditions. I used to challenge my fellow developers: Stop in the debugger right now. About how much memory should the process be using? Nobody even seems to have an order-of-magnitude guess anymore. It's your program, dude! Shouldn't you know this?

You can ask any embedded software engineer exactly how much memory his/her program uses, what's the stack size, what's the heap size, what's statically and dynamically allocated. Sadly, this discipline is pretty much gone outside of that specialized area.

Re:The Linux kernel's inability to gracefully handle low mem... by CJefferson on Tuesday August 06, 2019 @03:12AM (#20622899)
For browsers, this problem is "once removed". Firefox and Chrome both go to heroic efforts to reduce memory usage. However, given the html and javascript which fill most websites, and users expecting responsiveness, it turns out to be very hard to use less memory.
Re:The Linux kernel's inability to gracefully handle low mem... by voltagex_ on Tuesday August 06, 2019 @03:03AM (#20622853)
Re:The Linux kernel's inability to gracefully handle low mem... by lanevorockz on Tuesday August 06, 2019 @03:40AM (#20623052)
Browsers are little OSes with encapsulated virtual machines. As every website tends to be a mix of web trackers, ads, server dependent functionality. The whole thing can be a big mess.
Re:The Linux kernel's inability to gracefully handle low mem... by tempguy9999 on Tuesday August 06, 2019 @05:31AM (#20623653)
If it helps any I have ~2500 tabs open in palemoon (well overdue for an afternoon of tab culling but anyway) and it's ~1.5GB. I never allow JS. So my guess is it's either JS directly, or perhaps JS pulling in extra resources when allowed to run.
Re:The Linux kernel's inability to gracefully handle low mem... by michaelmrose on Tuesday August 06, 2019 @02:04PM (#20627782)
I think a personal wiki with clickable links to 2500 sites would be more useful than a browser with 2500 tabs open. It could even be easily curated by more than one person.

That said, Firefox some strides starting in 55 insofar as handling very large number of tabs starting in version 55.

https://www.techradar.com/news/firefoxs-blazing-speed-with-h...

Unfortunately Palemoon is more or less firefox 38 still isn't it?

Re:The Linux kernel's inability to gracefully handle low mem... by tempguy9999 on Tuesday August 06, 2019 @03:28PM (#20628605)
Just tidying up after myself would fix most of that. I don't know what palemoon~firefox relationship is versionwise.

But my point was more that sans JS it really seems to use up far less memory. Honestly, try nuking js for 1/2 hour and see how it feels.

Re:The Linux kernel's inability to gracefully handle low mem... by michaelmrose on Tuesday August 06, 2019 @05:50PM (#20630039)
I get annoyed at all the sites that don't work without js.

My most common scenario with js is 10 tabs using 400Mb of ram to 30 tabs using less than 1gb with the former scenario being more common.

Re:The Linux kernel's inability to gracefully handle low mem... by _-___________-_ on Tuesday August 06, 2019 @04:10AM (#20623220)
1 GiB of virtual memory, I assume. IOW, not the same thing as 1/32 of your 32 GiB of RAM, for example.