News for hackers. Stuff that matters.
Re:Linux Memory Overcommit (2007) by nabla9 on Sunday August 18, 2024 @06:39PM (#41286128)
As then as now, you can manage it.

vm.overcommit_memory == 0 heuristic overcommit

vm.overcommit_memory == 1 (full overcommit) allows allocating more memory than there is ram + swap.

vm.overcommit_memory == 2 never overcommit. There must be enough physical ram or virtual swap to allocate the memory.

Re:Linux Memory Overcommit (2007) by layer8 on Sunday August 18, 2024 @06:54PM (#41286210)
Can this be set for a specific process (or process group/tree), or only globally?
Re:Linux Memory Overcommit (2007) by nabla9 on Sunday August 18, 2024 @07:13PM (#41286310)
Globally

For a process you can use setrlimit() (ulimit command)

Re:Linux Memory Overcommit (2007) by silon42 on Monday August 19, 2024 @04:57AM (#41289201)
I'd want the OOM killer to kill processes with more over-commit first.
Re:Linux Memory Overcommit (2007) by jart on Monday August 19, 2024 @05:33AM (#41289389)
Why? There are many innocent applications that consume lots of virtual memory with no intention of using it. Sometimes sparse algorithms call for that sort of thing. It can be a nice crutch when you're in a pinch for time. A good example of a tool that does this is TSAN. It gobbles up virtual address space, which is practically free, thanks to the magic of modern MMUs. That's too powerful and good a thing to need to give up completely.
Re:Linux Memory Overcommit (2007) by kaba0 on Monday August 19, 2024 @06:08AM (#41289579)
Another example would be the JVM’s new low-latency GC, ZGC, that does some smart addressing tricks to store some information in pointer addresses themselves, effectively tripling the actual memory usage when naively looked at through virt memory.
Re:Linux Memory Overcommit (2007) by layer8 on Monday August 19, 2024 @07:31AM (#41290086)
Such applications could handle this explicitly, e.g. with mmap and mprotect. In principle there is no reason for such use-cases to rely on implicit overcommit behavior. Which also isn't present on Windows.
Re:Linux Memory Overcommit (2007) by jart on Monday August 19, 2024 @08:16AM (#41290369)
That's because Windows forces you to use special userspace libraries that account for memory and let you create "reservations" for large regions, without actually populating entries in the MMU (i.e. PML4t). Linux doesn't have that. You create reservations for large memory regions by allocating virtual memory, which of course doesn't become real (resident) until you use it. That's overcommit. But since there's no userspace libraries like WIN32 forced upon you on Linux, there's really no way for the kernel to know your intent when it comes to memory, because the abstractions for memory on Linux are so simple. Windows has a ridiculous byzantine system, which some people think is better. But people are really missing the point when they try to make an apples-to-apples comparison between the two systems of memory management. It's not a question of (a) overcommit good, vs. (b) overcommit bad. Anyone who thinks either is a fool. Linux is simply a system that requires a smart system administrator who understands your use cases and can tune sysctl accordingly when you want to push beyond the normal boundaries.
Re:Linux Memory Overcommit (2007) by layer8 on Monday August 19, 2024 @08:43AM (#41290551)
It seems to me that the prot parameter and/or MAP_NORESERVE would indicate the intent.
Re:Linux Memory Overcommit (2007) by kaba0 on Monday August 19, 2024 @07:54AM (#41290221)
It might actually be using that, I’m not intimately familiar with the topic.
Re:Linux Memory Overcommit (2007) by layer8 on Monday August 19, 2024 @07:57AM (#41290235)
Yes, I would expect that as well.

Implicit overcommit is really only necessary for fork and general backwards compatibility.

Re:Linux Memory Overcommit (2007) by ackfoobar on Friday August 23, 2024 @02:34PM (#41331708)
BTW the newer Generational ZGC is not using multi-mapped memory any more.
Re:Linux Memory Overcommit (2007) by kevingadd on Sunday August 18, 2024 @07:21PM (#41286368)
I like to run my development VM with overcommit_memory == 2, but I recently learned that the way it actually fails is pretty confusing. It's not an orderly 'abort: malloc failed' or process termination like I would've expected. Instead, you get weird intermittent failures that don't look like OOMs and your system appears to have plenty of free memory at the time.

Still good for flushing out scenarios where i.e. your build process needs 64GB of memory for approximately 500ms and then drops back down to reasonable levels, due to parallelism

Re:Linux Memory Overcommit (2007) by dataflow on Sunday August 18, 2024 @09:30PM (#41287028)
That sounds wild. How else can it possibly fail than by failing malloc? Where do the intermittent failures occur, if not in the return value? Could it be some part of your code isn't checking the return value for NULL?
Re:Linux Memory Overcommit (2007) by usefulcat on Sunday August 18, 2024 @10:51PM (#41287376)
IIUC, the way overcommit works on Linux is the memory returned by malloc() is not actually mapped to physical memory until it is accessed. So malloc may succeed and then at some point later you may get a segfault or similar when something actually tries to access that memory.
Re:Linux Memory Overcommit (2007) by dataflow on Sunday August 18, 2024 @11:05PM (#41287438)
> IIUC, the way overcommit works on Linux

Doesn't vm.overcommit_memory == 2 disable overcommit? That's the situation we're talking about...

Re:Linux Memory Overcommit (2007) by usefulcat on Sunday August 18, 2024 @11:22PM (#41287523)
You're correct; for some reason I didn't interpret the question in that context. I was thinking about when vm.overcommit_memmory is 0 or 1.
Re:Linux Memory Overcommit (2007) by cyberax on Sunday August 18, 2024 @11:04PM (#41287433)
malloc() will not return unmapped memory for small allocations. Your process might get killed if malloc() fails to get more backing pages.
Re:Linux Memory Overcommit (2007) by dent9876543 on Monday August 19, 2024 @07:02AM (#41289878)
Do you mean that my process might get killed [by the OOM killer] if /another process/ fails to get more backing pages?

Or are you suggesting that there is some other failure path that results in my process getting killed if my own malloc() fails to get pages? (That seems wrong. Should simply return NULL in that case, no?)

Re:Linux Memory Overcommit (2007) by cyberax on Monday August 19, 2024 @12:15PM (#41292299)
> Do you mean that my process might get killed [by the OOM killer] if /another process/ fails to get more backing pages?

It can be your process or some other process, depending on the OOM killer weights and heuristics.

> Or are you suggesting that there is some other failure path that results in my process getting killed if my own malloc() fails to get pages? (That seems wrong. Should simply return NULL in that case, no?)

No, malloc() in Linux will never return NULL for small allocations. What happens is that glibc will try to get more pages for its heap by calling mmap(). The call to mmap() will result in the OOM killer waking up and freeing some memory.

It then can kill some other process, and your process mmap() will succeed, or it can kill your process, and in this case mmap() never returns.

Re:Linux Memory Overcommit (2007) by cyberax on Sunday August 18, 2024 @11:03PM (#41287428)
> That sounds wild. How else can it possibly fail than by failing malloc?

By killing your process and/or by failing fork()s.

In general, malloc() in Linux will never fail for small allocations.

Re:Linux Memory Overcommit (2007) by fulafel on Monday August 19, 2024 @03:20AM (#41288602)
With overcommit disabled, fork may fail, but your other claims are backwards: malloc can fail, and processes aren't killed.
Re:Linux Memory Overcommit (2007) by cyberax on Monday August 19, 2024 @12:23PM (#41292374)
I suggest trying that :) You'd be surprised.

glibc allocator, like pretty much any other allocator, maps pages in advance without touching them. It's quite likely that you'll get OOM killed when you first try to touch them, rather than during mmap() for new pages.

Re:Linux Memory Overcommit (2007) by fulafel on Monday August 19, 2024 @10:39PM (#41296271)
Assuming you've also set overcommit_ratio to a conservative value, this would mean that the kernel overcommit=2 setting is not working, since mmap-ing writable pages counts as allocation from overcommit POV. It's a kernel sysctl, so I wouldn't expect it to change glibc's behaviour. There don't seem to be any open bugs about this in the kernel bugzilla. But do tell if you have found a bug in this area.

(This is documented in https://www.kernel.org/doc/html/latest/mm/overcommit-account... )

Re:Linux Memory Overcommit (2007) by ploxiln on Monday August 19, 2024 @04:58AM (#41289207)
> malloc() in Linux will never fail for small allocations

That's in kernel-space, not user-space processes. If you have overcommit disabled, it will be user-space processes making syscalls mmap() or sbrk() to get more heap memory (in which small allocations reside), that fails with an error return code, I assume. But I guess it could also fail to auto-grow the main thread stack, and presumably get a segfault. If the kernel needs to allocate more memory for a fork(), that syscall can fail. But if the kernel needs to allocate memory for some other internal purpose, probably back to the OOM killer, I guess ... hopefully this would be very rare?

Re:Linux Memory Overcommit (2007) by cyberax on Monday August 19, 2024 @12:21PM (#41292353)
> If you have overcommit disabled, it will be user-space processes making syscalls mmap() or sbrk() to get more heap memory (in which small allocations reside), that fails with an error return code, I assume.

Nope. No overcommit simply means that the OOM killer will be invoked earlier. The errors likely won't happen on the synchronous path due to the way glibc() maps the pages.

Long ago, I tried to write an OOM-resilient container library, and I wanted to test it. It worked as expected on Windows, but on Linux I was not able to get NULLs from malloc(). No amount of MLOCKALL and other tricks helped.

Re:Linux Memory Overcommit (2007) by ploxiln on Monday August 19, 2024 @02:54PM (#41293659)
Wow, that's disappointing. It seems there's really a lot of reasons to just accept overcommit on linux. (Browers, js engines used in various components now, databases that fork, many existing libraries not expecting allocations to fail ... and I guess they really don't after all?!)
Re:Linux Memory Overcommit (2007) by wmf on Monday August 19, 2024 @07:53PM (#41295523)
I think to fix this stuff you'd have to time travel back to 1980 or so. There's just so much software dependent on the broken behavior and a culture of not fixing things.
Re:Linux Memory Overcommit (2007) by kevingadd on Tuesday August 20, 2024 @04:55PM (#41303954)
From what I saw when running into this regularly, it seemed to be code that wasn't checking malloc return values for NULL, or code that didn't have well-defined behavior when out of memory. So I'd get weird error messages from random libraries being consumed by the applications I was using during execution, or individual operations in a larger task would fail but it would keep on chugging along.
Re:Linux Memory Overcommit (2007) by rcxdude on Monday August 19, 2024 @03:19AM (#41288599)
Do you run with swap in that case? I would suspect two possible reasons: firstly most software on linux is not run in that configuration and is thus unprepared for malloc failure: I would not be surprised if the return is not checked for many malloc() calls. Secondly, running without swap will cause bad performance (like, whole system freezing for a long time) failure modes as linux will evict cached code pages from RAM instead of rarely accessed data under memory pressure.
Re:Linux Memory Overcommit (2007) by taskforcegemini on Monday August 19, 2024 @03:44AM (#41288771)
>Secondly, running without swap will cause bad performance (like, whole system freezing for a long time)

in my experience it is exactly the opposite. unless you swap onto ssds, swapping will grind the whole system to a halt, while without swap the oom-reaper will kill the culprit and you can immediately work on the system to check/fix things. result also depends on your applications of course.

Re:Linux Memory Overcommit (2007) by wmf on Monday August 19, 2024 @07:56PM (#41295536)
The point is you need to have swap enabled but not use it. If you use swap it's bad and if you have swap disabled it's also bad. There "should" be no difference but that's not the case.
Re:Linux Memory Overcommit (2007) by lloeki on Monday August 19, 2024 @05:06AM (#41289245)
man 3 malloc:

       The malloc(), calloc(), realloc(), and reallocarray() functions
       return a pointer to the allocated memory, which is suitably
       aligned for any type that fits into the requested size or less.
       On error, these functions return NULL and set errno.
Problem is that thanks to overcommit malloc sort of always succeeds, and then many (most?) code doesn't check that malloc successfully returned and proceeds to march on towards a later obscure failure, hence below:

> It's not an orderly 'abort: malloc failed' or process termination like I would've expected. Instead, you get weird intermittent failures that don't look like OOMs