How much memory does a call to ‘malloc’ allocates?
Daniel Lemire's blogIn C, we allocate memory on the heap using the malloc function. Other programming languages like C++ or zig often call on malloc underneath so it is important to understand how malloc works.
In theory, you could allocate just one byte like so:
char * buffer = (char*) malloc(1);
How much memory does this actually allocate?
On modern systems, the request allocates virtual memory which may or may not be actual (physical) memory. On many systems (e.g., Linux), the physical memory tends to be allocated lazily, as late as possible. Other systems such as Windows are more eager to allocate physical memory. It is also common and easy to provide your own memory allocators, so the behavior varies quite a bit.
But how much virtual memory does my call to malloc(1) typically?
There is likely some fixed overhead per allocation: you can expect 8 bytes of metadata per allocation although it could be less or more depending on the allocator. You cannot use this overhead in your own program: it is consumed by the system to keep track of the memory allocations.
If you asked for 1 bytes, you are probably getting a large chunk of usable memory: maybe between 16 bytes and 24 bytes. Indeed, most memory allocations are aligned (rounded up) and there is a minimum size that you may get. And, indeed, the C language has a function called realloc which can be used to extend a memory allocation, often for free because the memory is already available.
You can ask how much memory is available. Under Linux, you can use the malloc_usable_size while under FreeBSD and macOS, you ca use malloc_size. So I can write a small programs that inquires how much (virtual) memory was actually granted given a request. For one byte, my macOS laptop gives out 16 bytes while my x64 Linux server seemingly gives 24 bytes.
If I plot the memory actually granted versus the memory requested, you see a staircase where, on average, you get 8 extra bytes of memory. Thus you probably should avoid allocating on the heap tiny blocks of memory (i.e., smaller than 16 bytes). Furthermore, you may not want to optimize the allocation size down to a few bytes since it gets rounded up in any case. Finally, you should make use of realloc if you can as you can often extend a memory region, at least by a few bytes, for free.

Generated by RSStT. The copyright belongs to the original author.