Visualizing coroutine allocation costs in memory.

Every Coroutine Frame Is a Heap Allocation Until It Is Not

I spent three years in high-frequency trading where a single unexpected heap allocation wasn’t just a performance hit—it was a post-mortem waiting to happen. I remember staring at a profiler during a midnight debugging session, watching the latency spikes dance in sync with our new asynchronous logic. Everyone in the tutorials tells you that C++20 coroutines are a “zero-cost abstraction,” but they’re lying by omission. If you aren’t accounting for the hidden coroutine allocation costs incurred every time a coroutine suspends and resumes, you aren’t writing high-performance code; you’re just praying the allocator doesn’t choke during a market spike.

I’m not here to sell you on the magic of syntax sugar or the elegance of the language spec. My goal is to pull back the curtain on what the compiler is actually doing with your coroutine frames. I will show you exactly how to profile these allocations, how to use custom allocators to keep your data on the stack, and how to ensure your predictable latency doesn’t vanish the moment your code hits a complex state machine.

Table of Contents

Heap vs Stack Allocation the Cost of Being Wrong

Heap vs Stack Allocation the Cost of Being Wrong

The default assumption in most C++ tutorials is that coroutines are “magic.” They handle their own state, they suspend, they resume—and they do it all without you lifting a finger. But that magic is backed by a heavy, often invisible, mechanism: the coroutine frame. When a coroutine is invoked, the compiler needs a place to store all those local variables and the suspension state. If the compiler can’t prove the lifetime of that state, it defaults to a heap allocation.

This is where the heap vs stack coroutine allocation debate stops being academic and starts being a performance killer. If you’re writing high-frequency code, a hidden `operator new` call inside your hot path is a disaster. You aren’t just paying for the allocation itself; you’re paying for the cache misses and the potential for heap fragmentation. While modern compiler coroutine elision techniques like HALO (Heap Allocation Elision Optimization) are getting better at moving these frames to the stack, you cannot rely on them. If your compiler’s optimizer misses a single hint, your “efficient” asynchronous logic just became a bottleneck.

The Memory Footprint of Coroutine Frames You Ignored

The Memory Footprint of Coroutine Frames You Ignored.

Most people treat coroutines like magic functions that just “pause,” but the reality is much more physical. When a coroutine suspends, it isn’t just sitting there; it has a physical presence in memory. This is the coroutine frame, a structured block that holds every local variable, every parameter, and the entire instruction pointer state required to resume execution. If you’ve written a coroutine that captures a large `std::array` or a heavy object by value, you aren’t just adding a few bytes to the stack—you are bloating the size of every single frame.

The real danger lies in the cumulative effect. In a high-throughput system, the memory footprint of coroutine frames can quickly spiral out of control, leading to cache misses that turn your “efficient” asynchronous code into a memory-bandwidth nightmare. While modern compilers are getting better at compiler coroutine elision techniques—essentially trying to prove that a coroutine doesn’t actually need to live on the heap—you can’t rely on the optimizer to save you from poor design. If your frame size is massive, your CPU will spend more time chasing pointers through the heap than actually executing your logic.

Five Ways to Stop Bleeding Latency in Your Coroutines

  • Stop treating `operator new` like a free lunch. If you haven’t implemented a custom `operator new` within your promise type to pull from a pre-allocated memory pool, you aren’t writing high-performance code; you’re just writing a slow way to trigger the system allocator.
  • Profile your frame sizes. I’ve seen developers write massive coroutine state machines only to realize their “small” async task is actually dragging a 2KB frame across the heap every time it suspends. Keep your local variables lean; every byte added to the coroutine state is a byte that increases your cache miss probability.
  • Embrace `std::allocator_traits` for custom allocation logic. If you want control, you have to be explicit. Don’t let the compiler decide how your coroutine frame is laid out; tell it exactly which arena it should live in.
  • Watch the lifetime of your captures. Passing large objects by value into a lambda that starts a coroutine is a silent killer. You aren’t just copying data; you’re bloating the coroutine frame and ensuring that every suspension point is more expensive than it needs to be.
  • Use `std::coroutine_handle::empty()` checks before you start assuming your allocation succeeded. In high-pressure, low-latency environments, a failed allocation isn’t just a crash—it’s a non-deterministic timing spike that will ruin your tail latency long before the OS actually kills your process.

The Bottom Line

Stop treating coroutines like lightweight state machines; unless you’ve implemented a custom allocator, every `co_await` is a potential trip to the heap that will wreck your cache locality.

The compiler doesn’t owe you performance; it follows the spec, and the spec allows for heavy-handed allocation patterns that will turn your latency-sensitive loop into a memory management nightmare.

If you aren’t profiling your coroutine frame sizes, you aren’t writing systems code—you’re just guessing, and in production, guessing is how you end up with an OOM killer on your hands.

The Bottom Line

At the end of the day, coroutines aren’t magic; they are just state machines with a heavier bill than you might expect. If you treat every `co_await` as a zero-cost abstraction, you’re ignoring the reality of how those frames are being laid out in memory. Between the potential for unexpected heap allocations and the cache pressure caused by bloated frame sizes, the “performance” of your asynchronous code is entirely dependent on your ability to control the lifecycle of that state. You can’t just sprinkle `co_await` throughout a latency-sensitive hot path and expect the compiler to clean up your architectural mess. You have to account for the footprint before you commit to the pattern.

C++ gives you the tools to build incredibly efficient, non-blocking systems, but it won’t hold your hand when your telemetry shows a sudden spike in allocator contention. The language expects you to understand the underlying mechanics—to know exactly where your data lives and how long it stays there. Stop treating the standard library like a black box and start looking at the actual machine behavior. When you stop fighting the language and start working with its specific, often punishing, rules, that’s when you actually start writing code that performs as intended in the real world.

About Ruaridh Kensington-Oyelaran

C++ rewards people who know what the compiler is allowed to do. I write about the rules that bite, the ones nobody mentions until you have already shipped the bug.

More From Author

Code review for C++ specifics: ownership first.

Review Ownership First, Style Last