-1

Given

public class A
{
 public B First { get; set; }
}       
public class B
{
 public C Second { get; set;}
}
public class C 
{ 
 public D Third { get; set; }  
}

And somewhere in the class you have this

var testClass = new A();
//All the properties have values in it and the class D has a property value that it is holding in to the memory
//testClass.B = new B();
//etc..

What happens if you did testClass = null? What does testClass's reference now to the heap? And what about D that is holding to a value that it can't collect?

EDIT: Just to clarify, given D has an event that hasn't been unsubscribed and has 10,000 handlers. What happens to testClass = null?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
123 456 789 0
  • 10,565
  • 4
  • 43
  • 72

1 Answers1

3

When you execute testClass = null, then the object remains on the heap. But there is no longer any reference to it in your code. It is eligible for garbage collection.

Any objects which are fields of A will no longer be referenced anywhere either. They will also be eligible for garbage collection.. and so on.

(As it happens, in your specific code, the fields First, Second and Third are never assigned. They are still null, so they don't really make any difference to the discussion).

Note that this collection probably won't happen straight away - setting testClass = null won't trigger a collection in itself.

If D references unmanaged resources, then it'll still be eligible for garbage collection when you set testClass = null. But unless D implements a Finalizer (which explicitly cleans up the unmanaged resources), then it will leak.

If D is an event with lots of subscriptions, then D will still be eligible for GC, even if you never explictly unsubscribe. See this answer here.

Following on, then if the subscriptions themselves are objects which are no longer referenced anywhere else, then they too will be eligible for collection.

Community
  • 1
  • 1
Baldrick
  • 11,712
  • 2
  • 31
  • 35
  • What if one of its properties is referencing to a value that it can't collect? Like a class that it references and inside it has unmanaged code? And I ommitted the values but put a comment there that all properties have been assigned to something and D is referencing to something that has unmanaged code in it that it can't collect. – 123 456 789 0 Dec 13 '13 at 02:13
  • 1
    @LeoLorenzoLuis - that would be a memory leak unless the class with the unmanaged reference implements a finalizer. Good reference: http://www.codeproject.com/Articles/319826/IDisposable-Finalizer-and-SuppressFinalize-in-Csha – Robert Levy Dec 13 '13 at 02:14
  • @RobertLevy Updated my question, yup that's my question what happens if i do testData = null and there is a memory leak? – 123 456 789 0 Dec 13 '13 at 02:17
  • @LeoLorenzoLuis: If you have unmanaged resources referenced by a class, you'd do well to implement the standard Dispose-Finalize pattern. See here: http://msdn.microsoft.com/en-us/library/vstudio/b1yfkh5e(v=vs.100).aspx – Baldrick Dec 13 '13 at 02:19
  • @LeoLorenzoLuis: See updated answer to take account of unmanaged resources held by D – Baldrick Dec 13 '13 at 02:23
  • @Baldrick What happens if it is an event that you forgot to unsubscribe and it has like 10,000 handlers in it. Will they ever be collected? – 123 456 789 0 Dec 13 '13 at 02:23
  • If D is an event with lots of subscriptions, then D will still be eligible for GC, even if you never explictly unsubscribe. See this answer here: http://stackoverflow.com/questions/298261/do-event-handlers-stop-garbage-collection-from-occuring – Baldrick Dec 13 '13 at 02:28