What are strong and weak references?

๐Ÿ’ก Concept: Strong and Weak References

Strong references keep objects alive and prevent garbage collection, whereas weak references allow objects to be collected if no strong references exist.

๐Ÿ“˜ Quick Intro

Strong references are default references in C#. Weak references enable conditional object lifetime, useful for caching and memory optimization.

๐Ÿง  Analogy

Think of a strong reference as a firm grip on an object, ensuring it stays, while a weak reference is a loose grip that lets go when needed.

๐Ÿ”ง Technical Explanation

  • ๐Ÿ›ก๏ธ Strong references prevent garbage collector from collecting the object.
  • ๐Ÿ‘ป Weak references do not prevent collection and can be checked for object availability.
  • ๐Ÿ”„ Useful for caches, where you want to keep objects if memory allows.
  • โš™๏ธ Implemented in C# via WeakReference class.
  • ๐Ÿ” Accessing a weak reference requires checking if target is still alive.

๐ŸŽฏ Use Cases

  • โœ… Implementing memory-sensitive caches.
  • โœ… Avoiding memory leaks due to lingering references.
  • โœ… Enabling objects to be collected when not strongly needed.
  • โœ… Improving application memory efficiency.

๐Ÿ’ป Code Example


var strongRef = new Object(); // Strong reference

var weakRef = new WeakReference(strongRef); // Weak reference to the same object

strongRef = null; // Remove strong reference

if (weakRef.IsAlive) {
    var obj = weakRef.Target;
    // Use obj if not null
}

โ“ Interview Q&A

Q1: What is a strong reference?
A: A reference that keeps an object alive.

Q2: What is a weak reference?
A: A reference that allows GC to collect the object.

Q3: When are weak references useful?
A: For memory-sensitive caches.

Q4: How to create a weak reference?
A: Using the WeakReference class.

Q5: Can you access a weak reference safely?
A: Yes, by checking if it is alive first.

Q6: Do weak references prevent memory leaks?
A: They help by not prolonging object lifetime.

Q7: Are strong references default?
A: Yes, all references are strong unless weak reference is used.

Q8: Can weak references cause problems?
A: Yes, if not checked before use.

Q9: What happens if weak reference target is collected?
A: Target becomes null.

Q10: Are weak references supported in all .NET versions?
A: Yes, from early versions onwards.

๐Ÿ“ MCQs

Q1. What does a strong reference do?

  • Allows GC
  • Keeps object alive
  • Deletes object
  • Copies object

Q2. What is a weak reference?

  • Prevents GC
  • Allows GC collection
  • Throws exceptions
  • Creates leaks

Q3. When to use weak references?

  • Always
  • Memory-sensitive caching
  • Never
  • Debugging

Q4. How to create a weak reference?

  • StrongReference
  • WeakReference
  • GarbageCollector
  • ObjectReference

Q5. Can weak reference be null?

  • No
  • Yes if collected
  • Never
  • Sometimes

Q6. Are strong references default?

  • No
  • Yes
  • Sometimes
  • Only in unsafe code

Q7. Do weak references prevent leaks?

  • Cause leaks
  • Help prevent leaks
  • No effect
  • Increase memory

Q8. What if weak reference target is collected?

  • Throws error
  • Target is null
  • GC stops
  • Program crashes

Q9. Are weak references in all .NET?

  • No
  • Yes
  • Only .NET Core
  • Only .NET Framework

Q10. Can weak references cause problems?

  • No
  • Yes if unchecked
  • Always
  • Rarely

๐Ÿ’ก Bonus Insight

Using weak references thoughtfully can optimize memory usage and avoid unintended object retention.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: