What Garbage Collection Actually Costs
What Garbage Collection 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.
- 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.
- thomashabets2
> In Rust you pay for it by arranging your program in a way the compiler can verify.
I disagree with this. The sentence implies that this work is done in order to make the compiler happy, where my experience is that it forces the programmer to actually get it right.
I had an "aha moment" when I was frustrated at failing to express my intent to the compiler, and suddenly realised that the reason I couldn't "just say the magic words" was that my object ownership design was inherently flawed. I had to make large changes not to make the compiler happy, but to actually have a coherent design.
So no, it's not about what "the compiler can verify". That's like saying "my lawyer won't let me do this". No, your lawyer is your employee, not your boss. They're just saying that if you do this, then you may go to prison. It's not the same thing.
("unsafe" is the Rust way to go "thank you, legal department, but I'm making a business decision to take this risk. Your concern has been noted")
- shivanshuag
Agreed, for most real world softwares, the cost of GC is irrelevant. But there are still some programs like databases or game engines where the cost can start adding up. That's when you measure and optimize.