Interview Question

What is a memory leak in .NET?

In .NET, unwanted reachability can retain managed objects indefinitely.

💡 Concept ✅ Quick Revision 🔷 C#

Answer

A managed memory leak occurs when objects remain reachable even though the application no longer needs them. • The garbage collector cannot reclaim reachable objects. • Long-lived event subscriptions, static collections, caches, and captured closures are common causes. • Unmanaged resources can also leak when disposable objects are not cleaned up.

💡 C# Example

publisher.Changed += subscriber.Handle; // Later, unsubscribe when the subscriber should be collectible. publisher.Changed -= subscriber.Handle;

⚡ Quick Revision

In .NET, unwanted reachability can retain managed objects indefinitely.