循环引用导致内存泄漏? [英] Circular References Cause Memory Leak?

查看:179
本文介绍了循环引用导致内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想办下来的Windows窗体应用程序的内存泄漏。我现在看它包含多个嵌入式表格的方式。我担心的是,孩子的形式,在其构造,需要引用父窗体,并保持在一个私有成员域。因此,在我看来,来的垃圾收集时间:

I'm trying to run down a memory leak in a windows forms application. I'm looking now at a form which contains several embedded forms. What worries me is that the child forms, in their constructor, take a reference to the parent form, and keep it in a private member field. So it seems to me that come garbage-collection time:

父的引用,子窗体,通过控件集合(子窗体嵌入那里)。子窗体未GC'd。

Parent has a reference to the child form, via the controls collection (child form is embedded there). Child form is not GC'd.

子窗体的引用,父窗体,通过私有成员字段。父窗体不GC'd。

Child form has a reference to the parent form, via the private member field. Parent form is not GC'd.

这是的垃圾收集器将如何评估情况的准确理解?任何的方式来证明其用于测试目的?

Is this an accurate understanding of how the garbage collector will evaluate the situation? Any way to 'prove' it for testing purposes?

推荐答案

大问题!

没有,这两种形式都将是(可能是)GC'd因为GC不直接去找其他参考参考。它只会寻找所谓的根引用...这包括在堆栈上的参考变量(变量是在栈上,实际的目标当然是在堆上),在CPU寄存器变量的引用和参考变量是在类的静态字段...

No, Both forms will be (can be) GC'd because the GC does not directly look for references in other references. It only looks for what are called "Root" references ... This includes reference variables on the stack, (Variable is on the stack, actual object is of course on the heap), references variables in CPU registers, and reference variables that are static fields in classes...

所有其它的参考变量仅访问(和GC'd)如果它们在根的参考对象之一由上述方法发现...(的属性引用的或在通过在参考引用的对象根对象,等等...)

All other reference variables are only accessed (and GC'd) if they are referenced in a property of one of the "root" reference objects found by the above process... (or in an object referenced by a reference in a root object, etc...)

因此​​,只有当被引用的形式之一,其他地方的根引用 - 那么这两种形式将从GC安全。

So only if one of the forms is referenced somewhere else in a "root" reference - Then both forms will be safe from the GC.

只有这样,我能想到的证明的,(不使用内存跟踪工具)将创造几百这些形式千元,在一个循环的方法里面,然后,而在方法,看看应用程序的内存占用量,然后从方法退出,调用GC,并期待在足迹了。

only way I can think of to "prove" it, (without using memory trace utilities) would be to create couple hundred thousand of these forms, in a loop inside a method, then, while in the method, look at the app's memory footprint, then exit from the method, call the GC, and look at the footprint again.

这篇关于循环引用导致内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆