简体   繁体   中英

.net CLR related question

This is a .net CLR related question.

I have 3 objects object A,B,C

A refres B and B refers c

What happens to these objects in the heap if i kill the Object "A" explicitly.Which will be garbage collected?(Object A or B or c or all?)

Can some one explain the garbage collection process in this scenario in detail.

Thanks in advance SNA

First - you can't "kill the Object "A" explicitly"; you can clear the reference to it, but that just sets a local variable to null , and has nothing to do with the actual object on the managed heap. You can Dispose() it, but that is nothing to do with GC.

It all depends; can anything else see B / C ? If not, they are eligible for collection. But GC is non-deterministic; it only happens when it chooses. At some indeterminate time, GC will kick in, detect that these objects are unreachable, and remove them. In the process, it will check for any that have finalizers (that are still outstanding), and execute the finalizers (in a 2-step process).

One thing people often forget about in terms of reachability is events; if B / C subscribe to events on a long-lived object, then B / C are reachable (by the event publisher).


To clarify; GC works by building a tree from the root objects (threads, etc). It walks every reference , flagging all the objects that can be reached. Anything that isn't flagged at the end is eligible for collection. This avoids the COM/COM+ issue of getting memory leaks due to disconnected islands of data, where X => Y and Y => X (so both X and Y have positive ref-counts, so neither gets cleared).

The first misunderstanding might be that you cannot kill a managed object explicitly.

You can free unmanaged memory that you allocated yourself, but that isn't managed and by that not subject to garbage collection anyway.

When you set the reference to A to null or it runs out of scope, then there wouldn't be any references to B & C, and the next GC collection will take care of it.

In .NET there is no way to actually kill/remove an object. All you can do explicitly is to disose an object. This nothing more that a simple call to Dispose() on your object. This will allow you to cleanup your object before it might get collected by the garbage collector at a later time (that you can not really infulence). See IDisposable on more details. The 2nd option to get a chance to cleanup your object before it gets collected by the GC is to implement a finalizer . Unlike the Dispose() this will be called by the GC automatically. Again, both are just way to cleanup any resources before an object might stop to exist.

So to answer your question if your object A gets "killed" wich only happens when it is not referenced by any other object anymore B and C will get "killed" to IF they are only referenced through A. Usually you do not have any influence on when this actually happens. All you can do is to implement the finalizer to get notified when it happens. The GC is a background service that runs on a separate thread that follows a complex logic when to actually delete objects.

If you want to get a basic understanding on how the GC works I would suggest the following two articles . Allthough a bit old they still fully apply.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM