News for hackers. Stuff that matters.
Re:The Linux kernel's inability to gracefully handle low mem... by Animats on Tuesday August 06, 2019 @12:36AM (#20622171)
Ah, yes, that bug.

Few programs can handle a fail return from "malloc", and Linux perhaps tries too hard to avoid forcing one. Most programs just aren't very good at getting a "no" to "give me more memory" Browsers should be better at this, since they started using vast amounts of memory for each tab.

I used to hit a worse bug on servers. If you did lots of MySQL activity, so that many blocks of open files were in memory, and then started creating processes, you'd often hit a situation where the Linux kernel needed a page of memory but couldn't evict a file block due to some lock being set. Crash. That was years ago; I hope it's been fixed by now.

Re:The Linux kernel's inability to gracefully handle low mem... by fluffything on Tuesday August 06, 2019 @06:14AM (#20623846)
> Browsers should be better at this,

Browsers are quite good at this actually. Major web browsers run on Windows (and even 32-bit windows!), where there is no overcommit, so malloc can return "no" any time, which happens quite often when you are limited to 4Gb of memory per process.

The only apps that suck at this are Linux-only apps that are never used anywhere else and just assume that all Linux systems have overcommit enabled.

Re:The Linux kernel's inability to gracefully handle low mem... by simias on Tuesday August 06, 2019 @06:50AM (#20624005)
>Most programs just aren't very good at getting a "no" to "give me more memory"

I suspect that overcommiting is one of the reasons for this though. Many programmers in the Linux world have integrated that "malloc can't fail" and the only error handling they bother doing is calling abort() if malloc fails.

Of course the fact that C doesn't provide any sane way to implement error handling probably doesn't help.

Re:The Linux kernel's inability to gracefully handle low mem... by needs on Tuesday August 06, 2019 @07:05AM (#20624071)
Handling malloc() failure is almost never done for short lived programs. For instance git used to fail as soon as an error popped of (whether be it malloc() or open(), etc...). It just is much simpler and convenient to do so.

While C has no special error handling mechanism in place, error handling can still be done reasonably. IMO, the big reason for why malloc() errors are rarely handled is because it is quite hard to come with a viable fallback strategy.

Re:The Linux kernel's inability to gracefully handle low mem... by simias on Tuesday August 06, 2019 @07:55AM (#20624311)
>Handling malloc() failure is almost never done for short lived programs.

True, and that makes sense for something like git. But in my experience many long-lived programs don't bother to handle ENOMEM gracefully either.

But I guess I'm veering off-topic here, I'm mostly fine with applications crashing of their own volition when they don't have enough memory. I agree with you that in many cases there's no clear recovery path for an application that's out of RAM. It's the OOM-killer I have a problem with.

>While C has no special error handling mechanism in place, error handling can still be done reasonably.

I very much disagree with that. There are a few factors that make error handling in C a pain:

- No RAII, so you have to explicitly handle cleanup at every point you may have to early-return an error (goto fail etc...).

- No convenient way to return multiple values from a function. That means that in general functions signal errors returning some special value like 0 or -1 (even that is very much nonstandard, often even within the same library).

Oh you want to be able to signal several error conditions? Uh, maybe use several negative codes then? Oh you need those to return actual results? Well maybe set errno then? Don't forget to read `man errno` though, because it's easy to get it wrong. Oh you had a printf in DEBUG builds in there that overwrote errno before you could test it? Oops. Don't do that!

What's that, your function returns a pointer and not an integer? Ah, mmh, well maybe return NULL in case of error? You want to return several error codes? Well maybe you can just cast the integer code into a pointer and return that, then use macros do figure out which is which. It's terribly ugly? Well the kernel does it so... It can't be that bad right? Oh and what about errno? Remember that?

What's that, NULL is a valid return value for your function? Uh, that's annoying. Maybe use an output parameter then? Oh, or maybe some token value like 0xffffffff, that probably won't ever happen in practice right? After all that's what mmap does.

So no I wouldn't consider C error handling reasonable in any way shape or form. "Non-existent" is more accurate. You can always work around it but it always gets in the way.

I try to always implement comprehensive error checking in my programs. I do a significant amount of kernel/bare metal work, so it's really important. It's not rare that I end up with functions that contain more error-handling-related code than actual functional code.

Re:The Linux kernel's inability to gracefully handle low mem... by Crinus on Tuesday August 06, 2019 @08:28AM (#20624500)
You are making things sound way more complicated than they need to be, the situation is actually very simple: if you need to return multiple error codes, use a return value for the error code and give back things via an output parameter, otherwise just use a sentinel value for error (0, -1 or NULL depending on context, they aren't totally random you know, 0 and nonzero are used for false/true, -1 is used when you expect some index and NULL when you expect some object). When in doubt just use an error return code everywhere (e.g. what many Microsoft APIs - even some C++ ones - do with HRESULT).
Re:The Linux kernel's inability to gracefully handle low mem... by simias on Tuesday August 06, 2019 @09:44AM (#20625072)
If it's not that complicated please explain why OpenSSL, the linux kernel, Curl a multitude of very popular C libraries don't do what you describe. Clearly it's complicated enough that even talented C coders try to cut some corners when given the chance.

C error handling ergonomics are non-existent which means that everybody bakes ad-hoc library-specific conventions that are extremely error-prone.

You could argue that they're doing it wrong and you might have a point but if almost everybody gets it wrong maybe it's fair to blame the language itself a little bit.

Re:The Linux kernel's inability to gracefully handle low mem... by Crinus on Tuesday August 06, 2019 @10:38AM (#20625538)
I already gave an example of APIs that do this - pretty much all COM APIs use HRESULT. I do not know why not everyone does this as i'm not everyone and as such i cannot tell what sort of considerations (if any) were going on. At best i can make some guesses.

BTW curl does seem to do what i wrote above, for example `curl_easy_init` returns a `CURL` object on success or NULL if there was an error [1] and `curl_easy_perform` returns a `CURLcode` value [2] that looks like it is used across the API to indicate errors.

[1] https://curl.haxx.se/libcurl/c/curl_easy_init.html

[2] https://curl.haxx.se/libcurl/c/curl_easy_perform.html

Re:The Linux kernel's inability to gracefully handle low mem... by robert_foss on Tuesday August 06, 2019 @10:03AM (#20625235)
The kernel very much returns sentinel values, if something more complicated has to be transmitted error codes are commonly used. I see nothing wrong with it.
Re:The Linux kernel's inability to gracefully handle low mem... by simias on Tuesday August 06, 2019 @03:45PM (#20628782)
I'm not arguing that the kernel devs are doing it wrong. I'm only pointing out that, in my opinion, the way C deals with error handling (that is, by not doing anything at all) is far from reasonable and the cause of many bugs. It's terrible ergonomics.

If you have a kernel function returning a pointer and you think that you're supposed to check for NULL when it actually returns a ERR_PTR in case of errors you will not only fail to do the check but on top of that end up with a garbage pointer somewhere in your program. If you have a MMU and you try to de-reference the pointer you'll have a violent crash, which at least shouldn't be too hard to debug. If you feed the pointer to some hardware module or if you're working on an MMU-less system then Good Luck; Have Fun.

C doesn't have your back here. It doesn't let you signal how a function reports errors, it doesn't even let you tag nullable pointers.

Re:The Linux kernel's inability to gracefully handle low mem... by bjourne on Tuesday August 06, 2019 @01:41PM (#20627537)
Often you need to return error objects. Consider a function for parsing something. You want to return not only the error code, but also the line and column number of the parse error, and a description of it. So you need two output parameters; one for the result and one for the error. Your declaration becomes something like this:

    bool parse(inp_type *a, out_type **b, out_error **c);
where the return value false indicates an error. In C++, you'd just have written something like:

    out_type parse(const inp_type& a);
and thrown an exception on error.
Re:The Linux kernel's inability to gracefully handle low mem... by Crinus on Wednesday August 07, 2019 @06:05AM (#20633297)
In C you can return a struct, however a better approach is to use a context object which also contains error information, like:

    ctx_t* ctx = ctx_new();
    if (!ctx) ... fail ...
    if (!ctx_parse(ctx, code)) {
        show_error_message(ctx_erline(ctx), ctx_ercol(ctx));
        ... more fail ...
        ctx_free(ctx); /* often done in a goto'd section to avoid missing frees*/
    }
This also allows you to extend the APIs functionality, error information, etc in the future while remaining backwards compatible.
Re:The Linux kernel's inability to gracefully handle low mem... by machinecoffee on Thursday August 08, 2019 @06:53AM (#20643338)
Which is great, except that ctx_new() requires a malloc, which then can fail, and now you can't even explain why the thing failed, as you have no context info.

You also have to worry about all of the ctx objects you've created along the way, to free them up as you recover from the low memory error.

Re:The Linux kernel's inability to gracefully handle low mem... by alyandon on Wednesday August 07, 2019 @10:38AM (#20635067)
That is very similar to the way I handled errors back in my C days.
Re:The Linux kernel's inability to gracefully handle low mem... by bjourne on Thursday August 08, 2019 @03:35AM (#20642572)
Yep, you're absolutely right. But don't tell me that is simple! :)
Re:The Linux kernel's inability to gracefully handle low mem... by la_barba on Tuesday August 06, 2019 @12:16PM (#20626564)
> No RAII, so you have to explicitly handle cleanup at every point you may have to early-return an error (goto fail etc...).

I think RAII can be useful, but I've never found any use for it in systems level code that I write. Most of the time I'm dealing with resources that were allocated inside a systems library or an external component which just gives me a handle to the resource. I think this is a common enough scenario in systems code that I don't think its just me.

e.g.

    1. X = CreateResource()
    2. Y = TransformResource(X)
    3. ProcessNewResource(Y)
    4. Z = TransformResource(Y)
    5. etc. etc.
And so as you transform that resource, you will have multiple ways to unwind the resource depending on where the failure occurs. Even if you wrap X in some RAII container, you don't know what your destructor is going to look like.

Another con to RAII, especially when paired with shared-ownership smart pointers, is you lose predictability over your resource deallocs. You never know when the last pointer is going go out of scope, and if its a 'heavy' resource with a complicated unwind, you're going to get a CPU spike at an indeterminate time. I deal primarily with industrial automation code and I much prefer to have a smooth/even CPU graph. I think this issue is more relevant to systems code which is the context of this thread.

Re:The Linux kernel's inability to gracefully handle low mem... by abjKT26nO8 on Tuesday August 06, 2019 @07:04AM (#20624065)
I've just checked and it appears that c++'s std::vector::resize may throw std::bad_alloc when malloc fails, but rust's Vec::resize's interface doesn't leave any room to report any errors, so I guess that it will panic...? That's sad.
Re:The Linux kernel's inability to gracefully handle low mem... by steveklabnik on Tuesday August 06, 2019 @08:47AM (#20624612)
The standard library assumes infallible allocation, yeah. We have plans to add fallible stuff eventually, but we’re still working on our allocator APIs.
Re:The Linux kernel's inability to gracefully handle low mem... by hurrrrrrrr on Tuesday August 06, 2019 @09:29AM (#20624938)
Rust in general will panic on memory allocation failure. There was some discussion about oom handling a while back but I don't know the current state.
Re:The Linux kernel's inability to gracefully handle low mem... by Animats on Tuesday August 06, 2019 @06:04PM (#20630140)
Now you regret not putting in exceptions.
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.
Re:The Linux kernel's inability to gracefully handle low mem... by zwaps on Tuesday August 06, 2019 @06:54AM (#20624020)
I think that's not that bug at all. When memory runs out, the entire system stalls, including the UI, but nothing crashes. If these issues are frequent, the system is basically frozen.

I have this in Matlab on Linux. Matlab can actually deal with worker processes being killed, but my machine just locks up. Therefore, we have to run these specific simulations under Windows, where this doesn't occur.

Re:The Linux kernel's inability to gracefully handle low mem... by HugThem on Tuesday August 06, 2019 @01:23AM (#20622392)
I witnessed MySQL bringing linux servers down two.

In my case it happens like this:

I have a long running PHP process that constantly fires away mostly SELECT but also a bunch of INSERT and UPDATE statements and also some DELETEs.

Since the DB and the key files do not fit into memory, its all disk bound work.

All tables are MyISAM.

Like clockwork, this stalls the virtual machine once per day.

All I can do is to hard power down the VM and restart it. Afterwards the table data is corrupted beyond repair.

Not sure it is related to memory though. Because the memory usage of PHP and MySQL seem to be constant. Most RAM seems to be used by Linux for caches.

Re:The Linux kernel's inability to gracefully handle low mem... by kokey on Tuesday August 06, 2019 @04:00AM (#20623175)
The most common cause here is something causing a situation where some queries hang or takes a long time to complete, while also locking access to something, while new queries keep coming in. This builds up quickly.

A good way to catch this would be to have something log the list of running queries every couple of seconds. Look at this log after the crash and you'll hopefully be able to identify which are the long running processes, and which are the regular queries that builds up.

To fix it would be a combination of making the queries that cause the locking to be less like that, also perhaps putting in a limit on how many queries can build up and also implement a way for the regular queries that build up to time out or fail quicker or more gracefully.

Re:The Linux kernel's inability to gracefully handle low mem... by HugThem on Tuesday August 06, 2019 @04:44AM (#20623408)
I fire the queries sequentially. So there is never more then one query running.
Re:The Linux kernel's inability to gracefully handle low mem... by kilburn on Tuesday August 06, 2019 @07:14AM (#20624114)
The "once per day" part is suspicious. Maybe a scheduled backup is what's running the other query ;)
Re:The Linux kernel's inability to gracefully handle low mem... by HugThem on Tuesday August 06, 2019 @07:22AM (#20624155)
I hade the same suspicion. Especially since the provider indeed runs a nightly backup of the VMs. But even after turning that off, the VM stalled the next night again.
Re:The Linux kernel's inability to gracefully handle low mem... by z3t4 on Tuesday August 06, 2019 @02:21AM (#20622664)
Try doing some rate limiting in order to not cause the dead lock. Should probably also disable write cache. And if it still doesn't work switch to a bare metal machine. And give it a lot of swap and up the swappiness. Swapping is a much better alternative then crashing. VPS providers doesn't like swap because it will tear their SSD disks, so the swap and swappiness is probably preset too low.
Re:The Linux kernel's inability to gracefully handle low mem... by HugThem on Tuesday August 06, 2019 @04:46AM (#20623418)
I tried sleeping 0.1s every 5s or so. It did not help. Still crashed.

I don't think swapping would even occur since neither mysql nor php grow their memory usage over time.

It's not an SSD. It's good old rusty HD.

Re:The Linux kernel's inability to gracefully handle low mem... by jmiserez on Tuesday August 06, 2019 @02:49AM (#20622794)
>Afterwards the table data is corrupted beyond repair.

That should not happen with a DB even if you turn off the power. Are you sure the hardware is good?

Re:The Linux kernel's inability to gracefully handle low mem... by smueller1234 on Tuesday August 06, 2019 @02:53AM (#20622813)
GP ist using the MyISAM storage engine. It's not crash safe. This is sad but expected behavior.

Don't use MyISAM!

Re:The Linux kernel's inability to gracefully handle low mem... by ygra on Tuesday August 06, 2019 @03:01AM (#20622847)
Is there any reason to ever use it (or: Why does it still exist)? In-memory databases for caches or other things that are not critical? I have to admit, I was astounded when I first got the error message from MySQL that a table was corrupted and that I should run REPAIR TABLE. That sounded like very weird behavior for a database.
Re:The Linux kernel's inability to gracefully handle low mem... by smueller1234 on Tuesday August 06, 2019 @09:45AM (#20625081)
Any remaining reasonable use cases would be sufficiently corner-casey that that the first order approximation is "if you want it to behave like a database, no, you do not want MyISAM".

This being said, at least some years ago, a use case I saw that held SOME water then was generating MyISAM tables offline, importing them as-is into a running MySQL (or taking an instance offline and bringing it back up) and then serving from it read-only. At least at the time, this provided better RO performance than InnoDB. I wouldn't be surprised if that was still true. Please don't do that at home!

Also, I think until the previous-to-most-recent release, some internal tables were still MyISAM, causing MySQL overall to have some very rare cases of not being crash safe. Again, I think that's since been resolved in 5.8(?).

Re:The Linux kernel's inability to gracefully handle low mem... by HugThem on Tuesday August 06, 2019 @04:48AM (#20623428)

    Is there any reason to ever use it
It is faster, uses less disk space and has a more logical filesystem layout.
Re:The Linux kernel's inability to gracefully handle low mem... by amaccuish on Tuesday August 06, 2019 @06:13AM (#20623844)
What do you mean by more logical? innodb_file_per_table has existed for a while now.
Re:The Linux kernel's inability to gracefully handle low mem... by HugThem on Tuesday August 06, 2019 @06:45AM (#20623983)
A flag with that name exists, yes. But it does not seperate table data into one file per table. It will still put stuff related to the tables into the central ibdata1 file.

Google "ibdata1 one file per table" to see all the pain it causes.

Re:The Linux kernel's inability to gracefully handle low mem... by amaccuish on Tuesday August 06, 2019 @09:36AM (#20625009)
False.

> But it does not seperate table data into one file per table

That's because if you didn't have it set when creating the database, it won't move data to the new fs layout when you set the setting on, without an OPTIMIZE. If you had it on from the beginning, table data is per file. I literally just did an ls on my /va/lib/mysql and there's a folder per database, in which there are 2 files per table (.frm and .ibd).

When innodb_file_per_table is on, and the database has been OPTIMIZEd, only the following is stored in ibdata1 [0]:

- data dictionary aka metadata of InnoDB tables

- change buffer

- doublewrite buffer

- undo logs

[0] https://www.percona.com/blog/2013/08/20/why-is-the-ibdata1-f...

Re:The Linux kernel's inability to gracefully handle low mem... by HugThem on Tuesday August 06, 2019 @12:46PM (#20626899)

   only the following is stored in ibdata1
You say "only", I say "clusterfuck".

Just look at the very page you linked to. It's a totally confusing concept that befuddles users and causes questions "we often receive", starts "panic", can "unfortunately" not easily be analyzed and you might need to "kill threads" and initiate "rollbacks" to fix the problems it brings.

MyISAM got that right. One dir per database.

Re:The Linux kernel's inability to gracefully handle low mem... by amaccuish on Tuesday August 06, 2019 @02:44PM (#20628182)
Ok but I don't get why you're so obsessed with the fs layout anyway. You should mostly treat it as a black box. And the point of ibdata1 is safety, which as you stated higher up is a serious problem with MyISAM. Even if it's not oom situations, you'll end up stuck sooner or later. You have been warned.
Re:The Linux kernel's inability to gracefully handle low mem... by HugThem on Tuesday August 06, 2019 @03:22PM (#20628565)

    You should mostly treat it as a black box
Again, check the very link you posted. People do that. Until shit hits the fan. And then they have to take that black box apart. Which is not easy.
Re:The Linux kernel's inability to gracefully handle low mem... by PeCaN on Tuesday August 06, 2019 @03:33AM (#20623017)
welcome to MySQL
Re:The Linux kernel's inability to gracefully handle low mem... by ensiferum on Tuesday August 06, 2019 @03:14AM (#20622909)
In general, the out of memory condition doesn't always come from the Linux kernel however but from the underlying memory allocator which typically is the memory allocator in the CRT in libc. Just because some process's memory allocator returned NULL or threw bad_alloc doesn't mean the system as whole is running out of memory.

When the kernel is running out of memory it will just start the OOM killer which will kill a process with low "nice" value.

Re:The Linux kernel's inability to gracefully handle low mem... by joosters on Tuesday August 06, 2019 @03:25AM (#20622969)
Actually, I'd say that if malloc (or equivalent) returns NULL then the system really is out of memory. Every general-purpose memory allocator is going to contact the OS to ask for more memory if it doesn't have anything free in its own buffers.

But... it's still no good saying 'make your program behave nicely when malloc fails' - even if your own code is perfect, what are the chances that every library you use does the same thing? And even then, Linux by default will optimistically over-allocate memory (and rightfully so!) - with the result that you'll never catch every out of memory condition.

IMO, 'out of memory' is not a property that each single process should try to manage, rather it should be the OS or some other process with a global oversight that monitors memory usage and takes measures when memory gets tight.

Re:The Linux kernel's inability to gracefully handle low mem... by ensiferum on Tuesday August 06, 2019 @03:54AM (#20623142)
You're right the memory allocator ultimately gets the memory it manages from the OS but as a programmer you're looking at it from the abstraction that its API provides and assuming any particular condition that would cause a NULL to be returned or bad_alloc to be thrown may or may not be correct.

The other point is that there's a distinction between kernele's view of OOM condition and some memory managers's OOM condition. Consider you run two processes, both allocate X gigs of memory and both succeed. However once you start running and committing the memory you'll get a kernel OOM condition and one process is killed. This is the overcomitting you mentioned.

Personally I don't see why people make such a big fuzz about dealing with memory allocation failures. Memory is just a resource same as any other OS resource, socket, mutex, pipe whatever. Normally in a well designed application you throw on these conditions and unwind your stack and ultimately report the error to the user or print it to the log and perhaps try again later. Just because it's "memory" should not make it special IMHO.

Re:The Linux kernel's inability to gracefully handle low mem... by joosters on Tuesday August 06, 2019 @05:57AM (#20623766)
It's the transparency of memory allocation that makes it so difficult to deal with failures. Even 'trivial' library functions could allocate memory, hell even calling a tiny dumb function might cause the stack to require a new page of memory, leading to failure. Just checking that all malloc calls check for NULL isn't even half of it.

Exception handlers won't save you either. Unless you consciously consider every memory allocation failure, your exception handlers will be too high level and result in your program either aborting by itself or becoming unusable. Did you pre-allocate enough resources to pop up an 'out of memory' error window? Good luck failing gracefully.

Memory allocation is special.

Re:The Linux kernel's inability to gracefully handle low mem... by ensiferum on Tuesday August 06, 2019 @06:13AM (#20623841)
Yes, it's an imperfect world and you can't control what happens in a library but the attitude "it doesn't matter if my code is messed up or not, some library will still do the wrong thing" doesn't help. All you can do is make your code work properly and that's what you (and everyone else) should do.

Again it's imperfect world saying that "it won't work because x, y, z will happen" is not the right attitude and is bad attitude. Most of your code should treat it as just a resource allocation failure and in a sane program that is indicated by propagating an exception up the stack. Now you might be right that the program might fail when it'd be the time to display a message box to the user or whatever. But somewhere in the middle layers of the code you don't have that context, you don't know that it will fail. Therefore that part of your code really should be (exception) neutral just like in any resource allocation case.

Re:The Linux kernel's inability to gracefully handle low mem... by cies on Tuesday August 06, 2019 @10:49AM (#20625632)
> Memory allocation is special.

I think the Zig lang treats it as special, therefore making you write code that handles the case that a malloc fails explicitly.

Re:The Linux kernel's inability to gracefully handle low mem... by bzbarsky on Tuesday August 06, 2019 @09:49AM (#20625133)
> I'd say that if malloc (or equivalent) returns NULL then the system really is out of memory.

That's very much not true when 32-bit processes are involved. You can easily be out of (non-fragmented) address space in a 32-bit process (whether it's all resident or not) while the overall system is nowhere close to being out of memory.

Even in a 64-bit process you can exhaust the address space without being out of memory if you try hard enough; you just have to try much harder.

That said, even on Linux allocators will return NULL when they're just out of address space; there's no overcommit going on there.

Re:The Linux kernel's inability to gracefully handle low mem... by joosters on Tuesday August 06, 2019 @11:40AM (#20626135)
> That said, even on Linux allocators will return NULL when they're just out of address space; there's no overcommit going on there.

Try calling fork() in that process then. By rights, the new process should inherit its own copy of all the address space of the old process, and is free to overwrite it with whatever it wants. Linux (by default) won't stop fork() from failing on a process with N GB of RAM and total memory(+swap) < 2N GB, yet there simply isn't the memory around for both processes. There's your overcommit.

Re:The Linux kernel's inability to gracefully handle low mem... by LgWoodenBadger on Tuesday August 06, 2019 @10:07AM (#20625274)
Would it be possible for the kernel to suspend the process in scenarios where malloc would fail instead of returning a failure? Either until enough becomes available for it to succeed, or until something tells the kernel to renew/revive/resume the process and try the malloc again?
Re:The Linux kernel's inability to gracefully handle low mem... by rtkwe on Tuesday August 06, 2019 @10:12AM (#20625302)
It could but if that process is using most of a systems memory that will lock up forever because while the process is frozen it won't release any of it's memory.
Re:The Linux kernel's inability to gracefully handle low mem... by ailideex on Tuesday August 06, 2019 @12:44AM (#20622206)
You provide limited information but it is not clear the scenario you explain is a bug. If too much memory is locked into resident memory with mlock then this sounds like the expected and correct behavior.
Re:The Linux kernel's inability to gracefully handle low mem... by hobbes78 on Tuesday August 06, 2019 @10:41AM (#20625564)
Then I prefer the unexpected and incorrect behaviour of Windows, which freezes the offending application and continues to be responsive, allowing me to kill it if I wish to do so...
Re:The Linux kernel's inability to gracefully handle low mem... by notacoward on Tuesday August 06, 2019 @10:40AM (#20625555)
> Few programs can handle a fail return from "malloc"

Fewer than should, that's for sure, but hardly a trivial number. A lot of old-school C programs are very careful about this, and would handle such a failure passably well. Unfortunately, just about every other language tends to achieve greater "expressiveness" by making it harder to check for allocation failure. How many constructors were invoked by this line of code? By this simple manipulation of a list, map, or other collection type? How many hidden memory allocations did those involve? I'm not saying such expressiveness is a bad thing, but it does make memory-correctness more difficult and so most programmers won't even try.

As the world moves more and more toward "higher level" languages, returning an error from malloc becomes a less and less viable strategy. Might as well just terminate immediately, since "most frequent requester is most likely to die" is better than 99% of the OOM-killer configurations I've ever seen.