开发者

How GC deal with this problem on .net platform?

开发者 https://www.devze.com 2023-03-11 07:11 出处:网络
I have this code classObject var1 = new classObject(); classObject var2 = var1;开发者_C百科 Now, I wrote this:

I have this code

 classObject var1 = new classObject();
 classObject var2 = var1;开发者_C百科

Now, I wrote this:

 var1 = null;

How the GC will handle this ? Does GC will collect the instance of this classObject ?

And how GC will hangle this if i will wrote var2= null ? ( and remove the var1=null from the code )


Short answer: no, because var2 still refers to the instance. As long as you have at least one reference to an object, the object will not be collected.

Longer answer: actually, probably it will in this example, because you don't go on to do anything with var2. The GC takes variable liveness into account. If you never use var2 again in the code, var2 is considered not to be live after that last line of code there. And so such a reference is considered not to be meaningful for garbage collection purposes - it's as though var2 no longer exists. So in fact, the object will be eligible for collection:

Additional subtleties: it will also depend on whether you're running in debug or release mode. When debugging, .NET artificially extends the liveness of variables to extend to the end of their lexical scope, so that you have the opportunity to inspect local variables. Also, of couse, objects will only be collected when the GC actually runs.

So strictly speaking, it's not actually possible to give on universally correct answer.


No, because you still have a live reference to the object in var2. If you null both var1 and var2 it will become eligible for collection (assuming no further references exist).


You still have a reference to the object (var2).

It all depends on the context.

The GC can collect objects even if you still have references to an object (if it determines that the object will not be used more in your code)

Interesting read: http://www.theserverside.net/tt/articles/showarticle.tss?id=GarbageCollection

Update

To me it sounds like you are confusing value types with reference types. When you assign a variable, you are not assigning the actual object to it, but a reference. And GC will (most times) not collect objects if a variable somewhere will have a reference to it.

Here is an article showing the difference: http://www.albahari.com/valuevsreftypes.aspx


You are only setting the pointer var1 to null var2 is still pointing to classObject so no GC won't collect classObject until there is at least one reference to classObject is alive


Garbage collection works by looking at the stack to determine whether objects on the heap have live references. If an object on the heap is not referenced by the stack, it is eligible for garbage collection.

You can re-assign references however many times you like, as long as an object is reachable from the stack, it will not be garbage collected!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号