What is a memory leak?
๐ก Concept: Memory Leak
A memory leak in C# occurs when the application holds references to objects that are no longer needed, preventing the garbage collector from reclaiming memory.
๐ Quick Intro
Memory leaks lead to increased memory consumption, reduced application performance, and potential crashes if unmanaged.
๐ง Analogy
Imagine holding onto old files in your desk forever, even if you no longer need them, cluttering the workspace.
๐ง Technical Explanation
- ๐๏ธ Objects still referenced are not collected by the garbage collector.
- ๐ Common causes: event handlers not detached, static references, circular references in unmanaged code.
- ๐ Leads to memory bloat and performance degradation.
- ๐ ๏ธ Use profiling tools to detect leaks.
- ๐ Best practice is to release references and 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.
๐ป 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
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!