Track Down a Memory Leak Caused by Event Subscriptions

Last updated: November 29, 2026

Quick Overview

A long-running desktop app's memory grows every time a user opens and closes a panel, even though closed panels are no longer referenced by the UI. Explain the likely cause with event handlers and implement a publisher that does not keep subscribers alive.

Microsoft
Software Engineering Fundamentals
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Onsite
Software Engineering Fundamentals
Medium

0

0

0 solved


A long-running desktop app's memory grows every time a user opens and closes a panel, even though closed panels are no longer referenced by the UI. Explain the likely cause with event handlers and implement a publisher that does not keep subscribers alive.

Event handler leaks are a classic in .NET and WPF applications like Visual Studio and Office add-ins: a long-lived publisher holds delegates that reference short-lived objects. Microsoft interviewers use this to check that you understand reachability in garbage-collected runtimes, how to confirm a leak with heap snapshots, and the trade-offs between unsubscribing and weak references.

What the Interviewer Expects
  • Explains that GC frees only unreachable objects, so a strong reference from a long-lived publisher keeps subscribers alive
  • Describes confirming the leak with heap snapshots and retention paths
  • Fixes the lifecycle by unsubscribing on close, and explains when that is not enough
  • Implements weak subscriptions correctly for bound methods
  • Prunes dead handlers to keep the publisher itself from growing
  • Mentions the weak event pattern in .NET and similar tools
Key Topics to Cover
Garbage collection
Reachability
Weak references
Observer pattern
Heap profiling
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • Why does a plain weakref to a bound method die immediately in Python?
  • What are the downsides of weak subscriptions (for example, handlers that silently stop firing)?
  • How would you find this leak in a production app you cannot attach a debugger to?
  • How do closures and lambdas capturing self create the same leak?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements

Panels subscribe to an app-wide theme_changed event. When a panel closes, it must be collectable even if it forgot to unsubscribe. Live subscribers must still receive events, in subscription order, ...

High-Level Design

In a garbage-collected runtime, an object is freed only when nothing reachable from a root references it. The theme service lives for the whole app, and its handler list holds bound methods (in .NET, ...


Submit Your Answer
Markdown supported