简体   繁体   English

.net CLR相关问题

[英].net CLR related question

This is a .net CLR related question. 这是一个与.net CLR相关的问题。

I have 3 objects object A,B,C 我有3个对象A,B,C

A refres B and B refers c A引用B,B引用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?) 如果我明确地杀死对象“ A”,堆中的这些对象会发生什么。将对哪些对象进行垃圾回收?(对象A或B或c或全部?)

Can some one explain the garbage collection process in this scenario in detail. 有人可以详细解释这种情况下的垃圾收集过程。

Thanks in advance SNA 预先感谢SNA

First - you can't "kill the Object "A" explicitly"; 首先-您不能“明确杀死对象“ A””; 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. 您可以清除对其的引用,但这只是将局部变量设置为null ,而与托管堆上的实际对象无关。 You can Dispose() it, but that is nothing to do with GC. 您可以使用Dispose() ,但这与GC 无关

It all depends; 一切取决于; can anything else see B / C ? 还能看到B / C吗? If not, they are eligible for collection. 如果没有,则有资格领取 But GC is non-deterministic; 但是GC是不确定的。 it only happens when it chooses. 它只会在选择时发生。 At some indeterminate time, GC will kick in, detect that these objects are unreachable, and remove them. 不确定的时间,GC将启动,检测到这些对象不可达,然后将其删除。 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). 如果B / C订阅了一个长期对象上的事件,则B / C是可到达的(由事件发布者)。


To clarify; 澄清; GC works by building a tree from the root objects (threads, etc). GC通过从根对象 (线程等)构建树来工作。 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). 这避免了由于数据孤岛(X => Y和Y => X)而导致的内存泄漏而导致的COM / COM +问题(X和Y都具有正的引用计数,因此都不会清除)。

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. 当您将对A的引用设置为null或超出范围时,将不会对B&C进行任何引用,下一个GC集合将予以解决。

In .NET there is no way to actually kill/remove an object. 在.NET中,没有办法真正杀死/删除对象。 All you can do explicitly is to disose an object. 您唯一可以做的就是分配一个对象。 This nothing more that a simple call to Dispose() on your object. 这仅是对对象的Dispose()的简单调用。 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. 有关更多详细信息,请参见IDisposable The 2nd option to get a chance to cleanup your object before it gets collected by the GC is to implement a finalizer . 在GC收集对象之前有机会清理对象的第二种选择是实现终结器 Unlike the Dispose() this will be called by the GC automatically. 与Dispose()不同,GC将自动调用它。 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. 因此,要回答您的问题,是否仅当对象A不再被其他对象引用时才会发生“杀死”,而如果B和C仅通过A进行引用,B和C就会被“杀死”。通常,您没有任何影响力何时实际发生。 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. GC是一项后台服务,它在单独的线程上运行,该线程在实际删除对象时遵循复杂的逻辑。

If you want to get a basic understanding on how the GC works I would suggest the following two articles . 如果您想对GC的工作原理有一个基本的了解,建议您参考以下两篇 文章 Allthough a bit old they still fully apply. 尽管有些陈旧,但它们仍然完全适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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