General object destruction question:
-
Being an intermediate programmer, I'm having a hard time understanding the documentation that is out there regarding this topic. Let's say I have two objects, derived from ClassA and ClassB. The lifecycle of Class B is as follows: -ClassA instantiates an instance of ClassB, and places this object into, say, a hashtable. -ClassB does its thing for a while, but then becomes useless. -ClassA removes ClassB from the hashtable. Now, there is no reference to that object within ClassA. What happens to ClassB? Is it still floating out there in memory? Do I need to tell ClassA to actually destroy the ClassB object, or does the GC already know that ClassA no longer refers to ClassB, and therefore destroys it automatically? Thanks for your help.
-
Being an intermediate programmer, I'm having a hard time understanding the documentation that is out there regarding this topic. Let's say I have two objects, derived from ClassA and ClassB. The lifecycle of Class B is as follows: -ClassA instantiates an instance of ClassB, and places this object into, say, a hashtable. -ClassB does its thing for a while, but then becomes useless. -ClassA removes ClassB from the hashtable. Now, there is no reference to that object within ClassA. What happens to ClassB? Is it still floating out there in memory? Do I need to tell ClassA to actually destroy the ClassB object, or does the GC already know that ClassA no longer refers to ClassB, and therefore destroys it automatically? Thanks for your help.
tantiboh wrote: Is it still floating out there in memory? Yes, for an indeterminate amount of time, until the GC puts the memory back into the general pool ("collects" it). tantiboh wrote: Do I need to tell ClassA to actually destroy the ClassB object No. Not unless ClassB was using some non-managed resources. does the GC already know that ClassA no longer refers to ClassB, and therefore destroys it automatically? Yes. It's hard to get ones arms around this, coming from C++. There's a lot of good articles out there. Try googling on "Understanding .NET Garbage Collection". Marc MyXaml Advanced Unit Testing YAPO