What Garbage Collection Actually Costs

What Garbage Collection Costs

What Garbage Collection Actually Costs

Garbage collection isn't free: the real cost is proportional to the number of live pointers, not memory used. This article breaks down the two memory-management paradigms—runtime-managed GC and manual/compiler-managed—and explains why pointer-heavy code pays more during collection. It offers a practical three-phase measurement approach (CPU share, allocation profile, live object analysis) and a checklist to decide when GC optimization matters, warning that premature optimization is usually a mistake.

Collecting a 4 GB graph of a million small objects pointing at each other is orders of magnitude more expensive than a single 4 GB buffer.
  1. kazinator

    > Think of a service that keeps a large cache in memory, or an index built out of millions of small objects that all point at each other. Every one of those pointers has to be followed on every cycle, for as long as the process is up.

    That's a strange thing to assert, having acknowledged the existence of generational GC.

  2. gwbas1c

    Careful, this statement is misleading:

    > The leaks that come from forgetting to free something go away entirely.

    Not quite: In manually-managed and referenced counted languages with destructors, releasing resources, (open file handle, open socket, open connection to a database, ect,) happens when objects are cleaned up.

    In a (tracing) garbage collected language, releasing resources is a very manual process. You might not have a memory leak, but leaking file handles or similar resources is a real problem with real consequence.

  3. kev009

    Even things built directly on underlying malloc and free typically have some form of "garbage collection" in the malloc implementation for efficiency and performance (geometric sizing, thread caching, etc).

    It's best to think about lifetimes and lifecycles where possible. Immutability where sensible and things like pool allocation are examples of this.

    GC languages can result in quite pessimistic code because they encourage people to NOT think about what is going on. But people have also built functional HFT engines on things like the JVM by thinking about lifetimes and lifecycles.

  4. 220hertz

    I used to write a lot of Javascript-like Extendscript scripts back when I was using InDesign a lot. The DOM's global object $ had a method to directly invoke the garbage collector. It made a difference certainly, but it was difficult to tell to what extent because InDesign itself gradually leaks memory and becomes more bloated the longer you use it in a single session.

  5. miladyincontrol

    What does GC cost?

    For Caddy with an incredibly synthetic http only benchmark it costs about 2ms of latency and somewhat less throughput.

    Worth it in an incredibly artificial benchmark? Perhaps. However when it comes to real world usage the cost is a significantly smaller piece of the pie.

More from this day

2026-08-13