Skip to content
Preptima

Top 11 Garbage collection Interview Questions

Browse 11 real-world technical and behavioral interview questions about Garbage collection. Review scenarios, edge cases, and architectural best practices.

You put @functools.lru_cache on a method to speed it up, and the service's memory keeps growing. Why, and which fix do you choose?

The cache lives on the class-level function and its keys include self, so every instance the method touches stays strongly referenced and is never collected; with maxsize=None or functools.cache that memory leak is unbounded. Cache per instance with cached_property or an instance dict, or cache plain values.

mediumconcept

Your GC log shows a stop-the-world pause far longer than the collection work inside it. Where did the rest of the time go?

Most likely into reaching the safepoint. Every application thread has to arrive at a poll point before the operation can begin, and one late thread stalls all of them, so the pause is time-to-safepoint plus the actual work. Safepoint logging separates the two.

hardconcept

How do you tell whether a value escapes to the heap, and how would you find the allocations that are costing you?

Escape analysis moves a value to the heap when the compiler cannot prove its lifetime ends with the frame that created it, and go build -gcflags=-m prints every decision. Which of those allocations matter is a measurement question, answered with a benchmark, an alloc_space profile and benchstat.

hardconcept

How does the .NET garbage collector decide what to collect, and how would you make a hot path allocate less?

The GC segregates the heap into generations on the assumption that most objects die young, collecting gen0 often and gen2 rarely, with large objects on a separate heap that is not compacted by default. Allocating less means removing boxing and using Span<T> over pooled or stack memory.

hardconcept

A service dies with OutOfMemoryError in production. Walk me through diagnosing it.

Read the message text first, because it names which region ran out. Then use the GC log to see whether the live set after each full collection is trending upward, which means a leak, or flat and close to the ceiling, which means the heap is simply too small. A heap dump tells you what is retaining the memory.

hardscenario

Your p99 latency jumped tenfold after a deploy while p50 is unchanged. Diagnose it out loud.

A flat p50 rules out a uniform slowdown, so the tail belongs either to a subset of requests or to a queueing effect. Partition the latency histogram by instance, endpoint and tenant before naming a cause, then confirm with traces, GC logs and pool-wait metrics.

hardscenario

A hot path allocates heavily and garbage collection is showing up in your profile. What does Span give you that a substring does not?

Span is a stack-only view over memory you already have, so slicing it costs nothing while Substring allocates a new string and copies. It removes allocations rather than making them faster, which is the right lever in a runtime where the cost is collection pressure rather than the allocation itself.

hardconcept

Memory keeps climbing in a Go service under load. How do you find out why, and what can you actually tune?

Take a heap profile and compare two taken minutes apart, because the shape of the growth tells you whether it is a leak, a cache with no bound, or simply the collector letting the heap grow. The two real knobs are GOGC and GOMEMLIMIT, and reducing allocation matters more in Go than in a generational runtime.

hardscenario

How do you optimise Java Virtual Machine (JVM) garbage collection to eliminate tail latency spikes in a game server?

Analyse JVM tuning strategies to prevent Stop-The-World (STW) pauses and ensure predictable microsecond response times for game server logic. It also connects garbage collection to the point an interviewer is testing.

hardscenario

What happens when you append to a slice, and when do two slices stop sharing memory?

Go slice append either extends the existing backing array or allocates a new one when capacity is exhausted. Bugs appear when two slices share the same array and one append mutates data the other still observes.

mediumconcept

A long-running Python service keeps climbing in memory and never comes back down. How do you work out where the memory went?

CPython frees an object the instant its reference count reaches zero, so steady growth is usually something still holding a reference rather than a leak. The cycle collector reclaims only reference cycles. Measure with tracemalloc first, then use __slots__ or generators to cut per-object and peak cost.

hardconcept