How fast are malloc_size and malloc_usable_size in C?

When programming in C, we allocate memory dynamically using the malloc function, by passing how many bytes we wish to allocate. If successful, the function returns a pointer to the newly allocated memory. Later we can free the allocated memory with the free function.

In general, unless you are keeping track of it yourself, you cannot recover the number of bytes you wanted to allocate. That’s because the malloc function can allocate more memory than you request. For example, if you call malloc(1), you should not assume that only one byte was allocated.

However, you can ask the system to report the number of allocated bytes. It will always be at least as much as you requested but could be more (possibly a lot more). Under macOS, you call malloc_size and under Linux you call malloc_usable_size.

I have never seen these functions actually used in the wild. But they could possibly be useful. For example, if you allocate memory for a string buffer, why would you painfully keep track of how much memory you wanted when you can simply request the potentially more useful allocated number of allocated bytes?

At a minimum, these functions could help with debugging: you could easily check dynamically that you have enough memory allocated throughout the life of your problem.

One reason to avoid these functions is that they are not standard. This means that if your code relies on them, it is possible that you could have difficulties porting your code to a new platform in the future.

Another good reason to track the number of bytes yourself is performance.

So how fast are they?

macOS Linux
Time (CPU cycles) ~50 ~10

On Linux, malloc_usable_size is fast enough for almost every purpose, except maybe for your most performance-critical code. On macOS, malloc_size should be used sparingly.

This matches a performance pattern I have observed: the standard C libraries under Linux have very fast memory allocation compared to Apple platforms.

My source code is available.

Credit: This blog post was inspired by Slava Pestov.

Published by

Daniel Lemire

A computer science professor at the University of Quebec (TELUQ).

2 thoughts on “How fast are malloc_size and malloc_usable_size in C?”

  1. malloc_size can be useful for cases where you’re allocating a buffer which is dynamically changing size over time, and you want to just use the entire buffer you get rather than leaving a bunch slack. For instance, think of std::vector storage, you would prefer that both the STL and underlying malloc library don’t each have a bunch of unused padding in there. Better would be to use something like malloc_good_size(), since the library can usually answer that question directly without any heap references, but that’s also non-standard.

    Apple’s implementation stores the length in a side bitmap which covers a large chunk of memory. Some other implementations store the info in a prefix of the allocated space, which will generally be very fast since it’s likely to share cache lines with the allocated data.

  2. If the issue with the search algorithm efficiency is in the Kernel, I really hope it’s in the Darwin piece of it, not the BSD piece

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see