Find and Fix a Memory Leak From Forgotten Event Listeners
Last updated: November 29, 2026
Quick Overview
A long-running server's memory grows steadily until it is restarted. Heap analysis shows thousands of request handler objects retained by a global event bus. Explain how you would debug this and fix the bus so it stops leaking.
0
0
0 solved
A long-running server's memory grows steadily until it is restarted. Heap analysis shows thousands of request handler objects retained by a global event bus. Explain how you would debug this and fix the bus so it stops leaking.
Memory leaks in long-lived processes are a classic debugging conversation at Google, where servers run for weeks. Interviewers check a methodical approach (measure, snapshot, compare, find retainers) and a fix that addresses ownership rather than restarting more often.
What the Interviewer Expects
- Describes measuring growth and taking heap snapshots
- Finds the retaining path from a GC root
- Identifies listener registration without unregistration
- Fixes with weak references or explicit lifetimes
- Adds a regression test or metric
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- Why does a bound method need WeakMethod rather than weakref.ref?
- When are weak references the wrong fix?
- How would you catch this kind of leak in CI?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
- Diagnose: confirm growth is real and unbounded, not cache warm-up.
- Locate: find which objects accumulate and what keeps them alive.
- Fix: subscribers that nobody else references should be collect...
High-Level Design
Debugging steps: plot RSS and heap size over a day to confirm linear growth. Take two heap snapshots an hour apart (tracemalloc in Python, or a heap profiler) and diff by type; RequestHandler coun...