01 / 05

How does Go's garbage collector work, and what knobs do you have to tune it?

Difficulty: 6/10
GC algorithm, tuning parameters, memory profiling

Go uses a concurrent tri-color mark-and-sweep GC that runs mostly alongside the program. GOGC controls heap growth trigger and GOMEMLIMIT (Go 1.19+) sets a soft memory ceiling.

GC algorithm overview
  1. 1

    Tri-color mark and sweep: objects are white (not visited), grey (reachable but children not scanned), or black (fully scanned)

  2. 2

    Concurrent: GC mark phase runs alongside the program — very short stop-the-world pauses (<1ms)

  3. 3

    Write barrier: ensures correctness when the mutator (your code) modifies pointers during GC

  4. 4

    Sweep phase reclaims white objects after mark completes

  5. 5

    GC is triggered when heap size doubles since last GC (controlled by GOGC)

GC tuning
Practical tuning strategies
  1. 1

    Profile first with pprof heap profiles before tuning GC parameters

  2. 2

    GOGC=off + GOMEMLIMIT: disable time-based GC, rely on memory limit for triggering — good for batch jobs

  3. 3

    Reduce allocations using sync.Pool for frequently allocated objects

  4. 4

    Avoid pointer-heavy data structures — GC must trace every pointer

  5. 5

    Use GODEBUG=gctrace=1 to see GC pause times and heap sizes in logs

Scenario Questions

0-2 years experience

  1. 1You added an in‑memory cache to a Go microservice and notice the process memory keeps growing even after the cache is cleared. How would you use Go's GC tools to investigate and fix the issue?
  2. 2If you set GOGC=50 in a small command‑line tool, what effect does that have on its memory usage and pause behavior compared to the default setting?

2-5 years experience

  1. 1During a load test, your Go service started experiencing occasional 200 ms GC pauses. Walk me through how you would identify the root cause and which GC knobs you would adjust.
  2. 2You need to reduce latency spikes in a high‑throughput API written in Go. Explain how you would balance GOGC, GODEBUG=gctrace, and runtime/pprof to tune the collector.

5-8 years experience

  1. 1Our platform runs dozens of Go services on shared VMs with limited RAM. Design a strategy for setting GC parameters across services to maximize overall throughput while keeping tail latency low.
  2. 2Explain how the concurrent mark‑sweep collector works under the hood and discuss the implications of increasing GOMAXPROCS on GC pause times in a CPU‑bound workload.

8+ years experience

  1. 1Our organization is standardizing on Go for all backend services. Propose a company‑wide policy for GC tuning, including monitoring, default GOGC values, and guidelines for when teams should deviate.
  2. 2Describe how you would design a self‑healing system that automatically adjusts Go GC knobs in response to observed latency and memory pressure across a fleet of services.

Follow-up Questions

  • What trade‑offs does the concurrent collector introduce for latency‑sensitive workloads?
  • How do you decide which GC knob to adjust when you see high pause times?
  • Can you share an incident where GC behavior caused a production issue and how you resolved it?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.