Live data from Hacker News

Malloc Never Fails (2012)

scvalex.net

21–30 of 165 posts

Re: Malloc Never Fails (2012)

#21

Earlier quoted context omitted.

Raspberry Pis almost always run 32 bit OSes, so that's a common place to encounter 32-bit.

I had always thought they'd be running a 64-bit OS, given that the new hardware is 64-bit. TIL that Raspbian is still 32-bit!

It seems Broadcom never released the required binary blobs in 64-bit format. People love to hate on Intel, but the ARM SoC world is quite terrible about binary blobs.

Re: Malloc Never Fails (2012)

#22

malloc fails with ulimit

Very relevant, because containers are often under what basically is ulimit. Not that you can do much when malloc fails...

If you take a look at the Linux kernel, a hugely complex and sophisticated piece of software, you'll find that it gracefully handles allocation failures.

Re: Malloc Never Fails (2012)

#23

Malloc allocates virtual memory. The original article had to issue a correction at the end: "I was wrong about why malloc finally failed! @GodmarBack observes, in the comments, that x64 systems only have an address space of 48 bits, which comes out to about 131000 GB. So, on my machine at least, the malloc finally failed because of address space exhaustion."

That's incorrect, 2^48 bits = 262144GB actually.

But x86-64 divides the 48-bit address space into two halves, only one of which is available in the user mode.

Re: Malloc Never Fails (2012)

#24

Malloc allocates virtual memory. The original article had to issue a correction at the end: "I was wrong about why malloc finally failed! @GodmarBack observes, in the comments, that x64 systems only have an address space of 48 bits, which comes out to about 131000 GB. So, on my machine at least, the malloc finally failed because of address space exhaustion."

That's incorrect, 2^48 bits = 262144GB actually.

They forgot to mention the kernel gets the top half of the address space, leaving 2^47 for the process. And then it mostly adds up.

Re: Malloc Never Fails (2012)

#25

malloc fails with ulimit

Very relevant, because containers are often under what basically is ulimit. Not that you can do much when malloc fails...

Depends; for example one video application I've written buffers as many incoming frames in memory as possible; if malloc fails when taking the buffer from 8000 to 9000 frames it's not a problem; just carry on running with the current buffer size.

Re: Malloc Never Fails (2012)

#26
post #16

Earlier quoted context omitted.

Very relevant, because containers are often under what basically is ulimit. Not that you can do much when malloc fails...

This is true, in my experience. If you keep your program fairly simple, implement it in plain C, avoid recursion, and keep these requirements in mind from the start, then it is possible to write a program that fails gracefully when it runs out of memory. Some old-school Unix daemons do this. But for most software it's impractical, so you might as well define your own xmalloc() that calls abort() if malloc() returns z…

An exception-safe C++ program can do the same through a std::bad_alloc exception.

If the out-of-memory was caused by something simple like accidentally loading 2G of data because of some odd data in an network request or a user selected file, and it rolls back to the start of the UI action or the incoming network request, the application may still work fine.

(I usually connected other out-of-resource conditions such as pipe/socket/open failures to std::bad_alloc too. They're pretty comparable in effect and give a bigger chance of actually exercising those exception paths)

Re: Malloc Never Fails (2012)

#28
I saw the title and thought "oh heck this isn't right at all"...

Set VM Overcommit to zero on an embedded system with no swap (a Raspberry Pi will do nicely).

Write a C program that malloc()'s all the RAM.

Watch malloc start to fail when you hit the RAM limit and the kernel has dumped all the I/O cache it can.

Re: Malloc Never Fails (2012)

#30

malloc fails with a negative argument

malloc takes a size_t, which is unsigned. A "negative" argument is just something that's absurdly large.

On 64 bit systems it would result in a call to malloc asking for between 9.2 and 18.4 Exabytes.
Post reply on HN