What is a memory leak?
π‘ Concept: Memory Leak
A memory leak happens when your program keeps holding references to objects that it no longer needs. Since the garbage collector only frees memory for unreferenced objects, those βstuckβ references prevent cleanup and gradually eat up memory.
π Quick Intro
In C#, memory leaks donβt usually happen because of raw pointer misuse (like in C++). Instead, they occur when objects stay alive unintentionally. Left unchecked, this leads to higher memory usage, slower performance, and eventually app crashes.
π§ Analogy
Imagine never throwing away old files from your desk. Even if you donβt need them, they keep piling up until your desk is unusable. Thatβs what a memory leak is β holding onto things you shouldβve let go.
π§ Technical Breakdown
- ποΈ The garbage collector canβt free memory if objects are still referenced.
-
π Common causes:
- Forgetting to
-=
unsubscribe from event handlers - Static fields keeping objects alive
- Unreleased unmanaged resources (file handles, DB connections)
- Forgetting to
- π Symptoms: memory keeps growing, performance slows down, app eventually crashes.
- π οΈ Detection: profiling tools like
dotMemory
,PerfView
, or Visual Studio diagnostics. - β
Prevention: explicitly release references, implement
IDisposable
, and always unsubscribe from events.
π― Use Cases
- β Diagnosing performance issues in long-running apps.
- β Ensuring proper disposal of event subscriptions.
- β Preventing out-of-memory exceptions.
- β Writing memory-efficient C# code.
- β Keeping long-running apps (like web servers) stable over time
- β Writing clean, memory-efficient C# code
- β
Avoiding production outages from
OutOfMemoryException
- β Debugging performance issues that are hard to trace
π» Code Example
// Event subscription causing memory leak if not unsubscribed
class Publisher {
public event EventHandler RaiseEvent;
}
class Subscriber {
public void Subscribe(Publisher pub) {
pub.RaiseEvent += HandleEvent;
}
private void HandleEvent(object sender, EventArgs e) { /*...*/ }
public void Unsubscribe(Publisher pub) {
pub.RaiseEvent -= HandleEvent;
}
}

β Interview Q&A
Q1: What is a memory leak?
A: Unreleased memory due to persistent references.
Q2: What causes memory leaks in C#?
A: Event handlers, static references, unmanaged resources.
Q3: How to detect memory leaks?
A: Using profiling and diagnostic tools.
Q4: How to prevent memory leaks?
A: Properly unsubscribe events and dispose objects.
Q5: Can garbage collection fix leaks?
A: No, GC only collects unreferenced objects.
Q6: What is event subscription leak?
A: Forgetting to unsubscribe keeps objects alive.
Q7: Is static reference a cause?
A: Yes, it keeps objects rooted.
Q8: Can unmanaged code cause leaks?
A: Yes, if not managed correctly.
Q9: What happens if memory leaks?
A: Performance degradation and crashes.
Q10: How to avoid leaks?
A: Follow best practices for resource management.
π MCQs
Q1. What is a memory leak?
- Released memory
- Unreleased memory
- Memory allocation
- Garbage collection
Q2. What causes memory leaks?
- Loops
- Event handlers and static references
- Threads
- Exceptions
Q3. How to detect memory leaks?
- Debug logs
- Profiling tools
- Unit tests
- Code review
Q4. How to prevent memory leaks?
- Ignore
- Unsubscribe events
- Use static
- Restart app
Q5. Can GC fix leaks?
- Yes
- No
- Sometimes
- Never
Q6. What is event subscription leak?
- Unsubscribing
- Not unsubscribing
- Garbage collecting
- Allocating
Q7. Is static reference a leak cause?
- No
- Yes
- Sometimes
- No idea
Q8. Can unmanaged code cause leaks?
- No
- Yes
- Sometimes
- No idea
Q9. What happens if memory leaks?
- Better performance
- Performance issues
- No effect
- Crash
Q10. How to avoid leaks?
- Ignore
- Proper resource management
- Use more memory
- Restart system
π‘ Bonus Insight
In C#, leaks often hide in plain sight β especially with events and statics. Reviewing code with a βlifecycle mindsetβ (who creates this, who cleans it up?) is the simplest way to avoid them. Remember: the garbage collector isnβt magic β it only collects what you let go of.
Being aware of memory leaks and proactively managing event subscriptions and references is key to robust C# applications.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!