# malloc barely touches the kernel: 1,000 allocations, just two syscalls

Original title: What is malloc doing, anyway?

> Hacker News story 50424157 · language: en · submitted 2026-11-09
> Web version: https://zeli.app/story/50424157
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 255 · by [benmandrew](<https://news.ycombinator.com/user?id=benmandrew>) · 💬 64
[original](<https://benmandrew.com/articles/what-is-malloc-doing-anyway>) · [HN discussion](<https://news.ycombinator.com/item?id=50424157>)

## Summary

A hands-on walkthrough of what malloc actually does under glibc: it asks the kernel for memory via brk, grabs a 132 KiB heap chunk on the first call, and serves the next 999 allocations from that pool. The post then builds a toy allocator step by step—adding chunk headers, free lists, splitting and coalescing, demand paging, and finally trimming memory back to the kernel—while timing syscalls to show why malloc(16) runs 22.7 times faster than sbrk(16).

## Key quote

> Each 256 MiB written then costs 65,536 page faults, one per 4 KiB page, increasing the RSS.

## HN discussion (64 comments)

### [assumed_throwaw](<https://news.ycombinator.com/item?id=50425141>)

> Tangential, but the complexity and over-head of the malloc abstraction is precisely what made me very intrigued by "stack-only" languages, such a recently published Goose. There is something very elegant and exciting about the idea of (almost) completely getting rid of it. Just as modern programming languages have developed good ergonomics that replaces a garbage collector, the same could hopefully be done to replace heap-allocated memory.

### [K0IN](<https://news.ycombinator.com/item?id=50427045>)

> When getting into wasm when it was new and had clang support, I wrote my own libc and malloc, which was so a great learning all these low level functions and tricks (free lists for example), I also like that I can read wasm quite well, and tooling is good to just return a .wat and read it.

### [projectileboy](<https://news.ycombinator.com/item?id=50428699>)

> Good learning resource for this (and related) topics: https://cs61.seas.harvard.edu/site/2026/

### [bajard](<https://news.ycombinator.com/item?id=50428547>)

> Malloc() is sloooow. Even more if you also use free(). Consider allocating a single array and scratching there if possible.

### [wpmaven](<https://news.ycombinator.com/item?id=50425742>)

> I found this one very well explained time ago, for those looking for more details: https://sploitfun.wordpress.com/2015/02/10/understanding-gli...

---

## Cite

- Page: https://zeli.app/story/50424157
- Date: 2026-11-09
- Source: benmandrew.com (https://benmandrew.com/articles/what-is-malloc-doing-anyway)
- Hacker News: https://news.ycombinator.com/item?id=50424157

## Other languages

- zh: https://zeli.app/zh/story/50424157 · markdown: https://zeli.app/zh/story/50424157.md
- ja: https://zeli.app/ja/story/50424157 · markdown: https://zeli.app/ja/story/50424157.md
- ko: https://zeli.app/ko/story/50424157 · markdown: https://zeli.app/ko/story/50424157.md
- de: https://zeli.app/de/story/50424157 · markdown: https://zeli.app/de/story/50424157.md
- es: https://zeli.app/es/story/50424157 · markdown: https://zeli.app/es/story/50424157.md
- ru: https://zeli.app/ru/story/50424157 · markdown: https://zeli.app/ru/story/50424157.md
