Urgent.News

What's breaking now, across thousands of outlets.

Tech

What is malloc doing, anyway?

The malloc function provides a simple interface for requesting blocks of memory. When you call malloc with a specific size, it returns a void pointer to a contiguous block of bytes that has been created for your use. Once you no longer need that memory, you call free with the same pointer, and the memory is freed up again.

Under the hood, malloc relies on the kernel through syscalls to actually allocate and free memory. However, making a syscall for every small allocation can be expensive. To mitigate this, malloc performs some bookkeeping in the background.

When you call malloc repeatedly in a loop, like a thousand times, malloc doesn't go through the kernel each time. Instead, it first asks the kernel for a larger block of memory using the brk syscall, which sets the program break to the requested address, effectively allocating the heap. Subsequent calls to malloc simply take memory from this existing block, without needing to go through brk again.

malloc also allocates a little extra memory beyond what you requested, to accommodate the bookkeeping metadata it needs to manage the allocations and deallocations. The memory returned by malloc is also aligned to a 16-byte boundary, which is suitable for any C data type.

When you free a chunk of memory, malloc puts it into a free list so that it can be reused for future allocations. It tries to merge adjacent free chunks back together to reduce fragmentation and make more efficient use of memory.

This behind-the-scenes bookkeeping makes malloc much more efficient than a naive allocator that simply bumps a pointer up by the requested size each time. malloc defers the expensive syscalls until necessary, and reuses memory whenever possible.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Also reported by 1 other outlet

Read the original at benmandrew.com →

More in Tech

Here's What Must Happen for SpaceX Stock to Rebound

SpaceX stock is down 22% since its IPO in June.

  • SpaceX stock down 22% from IPO price amid AI sector correction
  • AI market concerns over data center infrastructure scaling
  • SpaceX needs AI roadmap fulfillment to justify $1T valuation

More from Monday 9 November →