If you have ever wandered into a programming forum, you have met this sentence: "want a fast app? Stay away from GC languages like Java, Go, C#, Python, or JavaScript, just use C, C++, or Rust." That belief has had a long life, and behind it sits one picture of how GC works that is worth checking.
There is some truth in it: GC really does eat resources, and that is not up for debate. But two things usually taken as certain about GC, that it runs every few seconds and that it always makes things slow, deserve a look at the mechanism itself. In the previous article we watched Discord and Cloudflare run into this limit by measuring first, not guessing. Now it is time to peek at that limit from the inside. 🙂
Key takeaways
Modern Garbage Collectors trigger themselves through an allocation threshold, not a timer, and for most applications that arrangement works in your favor.\n- GC pauses in modern runtimes like Go and ZGC are already sub-millisecond and do not grow with heap size, so the "GC makes things slow" charge only holds in systems with microsecond deadlines.\n- The memory cost of GC is real but movable: a high GOGC saves CPU at the price of RAM, while a GOMEMLIMIT that is too tight ends in thrashing.\n- What turns GC into a problem is not its speed but the inability to schedule its pauses, so as long as your deadlines are loose the suspect is the wrong one.\n- The Instagram case shows some GC problems can be settled through configuration, and even that only makes sense when processes are short-lived.
Does the GC run on a timer?
The easiest sketch for imagining how GC works goes roughly like this:
while true:
sleep(5) # wait 5 seconds
cari_objek_yang_udah_nggak_dipakai()
But the real shape is nothing like that. If there really were a timer in there, your server idle at midnight would still be busy scanning memory every five seconds: CPU burning, cloud bill rising, and all of it cleaning up emptiness. Fortunately, modern GC is not that dumb. 😅
What almost every modern runtime uses now is an allocation threshold, not a clock. The GC only
wakes up when the memory your application allocates touches a certain limit. In Go, that limit is set
through the GOGC environment variable, whose default is 100.
The number 100 means this: the GC lets the heap grow to roughly twice the live memory. For example, if after a cycle the live memory left is 10 MB, the GC runs again when the heap touches 20 MB. As long as your application stays under that number, the GC sleeps soundly. Makes sense, right?
You can turn the 100 yourself. Lower it to 50 and the target becomes around 15 MB, so the GC runs more often but saves RAM. Raise it to 200 and the target becomes around 30 MB, so the GC runs less often but wastes more RAM. The full formula sits in the official Go GC guide if you want to compute it yourself, but the gist is just that.
So the answer is no. The GC is not driven by a timer.
But some of it really is time-based
Here the question gets more interesting: some runtimes do have a time-based trigger. Go is one of them. It forces the GC to run at least every 2 minutes, regardless of how slowly the heap grows. This is not forum gossip; it exists as a comment in the Go source code: if 2 minutes pass with no collection, one cycle is forced.
Why is that needed? Because the GOGC threshold is only useful if your application allocates often. If
the live heap is large but allocation is slow, exactly like Read States at Discord, that threshold
never gets touched, and this 2-minute timer is what eventually takes over. Every time it runs, the GC
has to scan the whole LRU cache holding millions of users. From that, Discord's
blog concluded why their graph had
spikes arriving neatly every 2 minutes or so, and why the effort to tune GC percent failed
completely.
Go is not the only one. V8 in Chrome has idle-time
GC: the cleanup work is handed to an idle task
scheduled by the browser, not to a fixed schedule. HotSpot even offers the
G1PeriodicGCInterval option, which triggers a periodic GC cycle while
the application is idle, usually to return memory to the operating system.
One honest note before moving on: the Discord case happened in 2020, when Go was still on the 1.8–1.10
line, and how its GC works has changed a lot since. What has not changed: as long as your heap is large
and full of pointers, scanning it remains work you have to pay for. That shift is what pushed Go 1.19
to add GOMEMLIMIT, so you can set an absolute memory limit, not just a ratio.
So if you have been picturing the GC as having a regular schedule, your guess was not wild. Only the proportion differs: the time-based triggers are a safety net or a side job, not the main engine.
What does the GC actually do inside?
The GC is like a cleaner who does not walk the halls every hour: it only moves when someone calls it. And almost every modern GC belongs to the tracing family, with the same basic pattern: mark, then sweep. To make it concrete, let's take the two phases one at a time.
How does the GC know which objects are still alive?
The GC does not read memory from address zero blindly. It starts from GC roots: local variables on the stack, global variables, and CPU registers. From there it walks along pointers like following a tree. Reachable objects get marked alive. Once the walk is done, the heap is swept and everything unmarked goes back to the free list, ready for reuse without asking the operating system for new allocation.
One detail that often gets missed: Go's GC is non-moving. Objects are not shifted around in memory. Some other languages move their objects (compacting) to pack memory tighter, at the price of updating all pointers. Java and V8 take a middle road with a generational collector, which bets that young objects die sooner, so not every object needs checking as often.
Tracing vs counting references
There are two big families here. Tracing is used by Go, Java, and V8. Reference counting is used by Python and Swift, in a different way: each object keeps a count of its references, and once that number hits zero, the memory is freed right away. Python even uses both at once, because refcount alone cannot handle objects that point at each other in a cycle.
That split matters to remember, because from it the policies for tuning the GC differ: from GOGC and
GOMEMLIMIT in Go, through the choice of collector from G1 to ZGC on the JVM, to the option of
turning off part of the GC in Python.
So where does the performance cost show up?
If the algorithm only walks pointers, what are we really paying for? These three things:
- Short pauses at phase transitions. In Go, the application only truly stops (stop-the-world) briefly at the transition between mark and sweep, and the length is deliberately kept from growing with heap size. On the JVM, ZGC has been a production feature since JDK 15 with sub-millisecond pauses, even on heaps of absurd size.
- The share of CPU the GC pulls. The mark phase takes around 25% CPU, which means your application threads are fighting over the processor. The most annoying part: if your allocation is fast, your own goroutines get dragged in to help the GC (GC assist).
- Memory overhead.
GOGC=100means you set aside room for roughly twice the live memory. RaiseGOGCand the GC runs rarely and the CPU breathes, but RAM gets wasted. SetGOMEMLIMITtoo tight and you get a new problem called thrashing: the GC runs constantly, and the application can slow down to half speed.
There is also a side effect that is harder to measure: GC metadata fills the CPU cache, so the data your business logic needs gets pushed out to RAM with far higher latency. This is not a number that appears on any documentation page, but it makes sense, and Instagram itself cited the rise in cache hit ratio as one reason they turned off GC in Python.
When does the GC actually become a problem?
Here the decision can be defended, and the answer depends on how tight your deadlines are.
- Ordinary web APIs and microservices. Not a problem. A half-millisecond pause in a request whose network latency is 30–50 ms will not be felt by anyone, and in exchange you get a language free of one of the most expensive classes of bug. Microsoft itself says around 70% of the CVEs they fix each year are memory safety issues, and that is in C and C++ code.
- Games chasing 60 FPS or more. Just imagine: at 60 FPS one frame gets only 16.6 ms, and at 144 FPS what is left is 6.9 ms. A GC pause of just 10 ms is enough to make the frame stutter.
- Kernels, drivers, and hard real-time systems. What matters here is not average speed but determinism. A system that may only be late by microseconds cannot hand its schedule to a GC.
- High-frequency trading. What gets traded here is microseconds. A pause distribution you cannot predict costs as much as the pause itself.
See the pattern: what makes the GC a problem is not "GC is slow", but "GC cannot be scheduled". As long as your application's deadlines are loose, the charge collapses on its own.
So is the GC a problem or not?
For most of the software you build, the answer is no. The GC trades a little RAM and a little CPU for the disappearance of one of the most expensive classes of bug ever to exist in the C world, and it was Microsoft that said so, not a Go fan.
Instagram could even turn off the cyclic collector in its Python and immediately run 10% more efficiently without switching languages. But that only makes sense because its processes are short-lived and restarted often.
So what you need is not to avoid GC, but to know when it starts struggling. If your latency graph has neat spikes every few minutes, the suspect might be GC, and you can even guess from the spike's duration. But prove it with a profiler first, not from a feeling.
Happy dissecting your latency graphs, and may the suspect never be the wrong one. 👋