Technical Senior Level

How does Python manage memory? Explain reference counting, the garbage collector, and how you would diagnose a memory leak.

Quick Tip

Show diagnostic experience: "I use tracemalloc to snapshot memory at two points and compare. For cycle leaks, gc.set_debug(gc.DEBUG_SAVEALL) captures uncollectable objects. objgraph visualises reference chains."

What good answers include

Python uses reference counting as primary memory management — objects are freed when refcount reaches zero. The cyclic garbage collector handles reference cycles (objects that reference each other). Memory leaks in Python: circular references with __del__, caching without bounds, global collections that grow, C extension leaks. Diagnostic tools: tracemalloc (stdlib), objgraph, memory_profiler, gc.get_referrers(). Strong candidates discuss __slots__ for memory reduction and weakref for breaking cycles.

What interviewers are looking for

Senior Python question. Candidates who think Python cannot have memory leaks are wrong. Those who can systematically diagnose memory growth using profiling tools demonstrate production experience.

← All Python questions