简体   繁体   English

双对象内存泄漏?

[英]double object memory leak?

here's the setup for c# program: 这是C#程序的设置:

Inside Main class: ObjA MyObjA = new ObjA(); 内部主类: ObjA MyObjA = new ObjA();

Inside ObjA: ObjB MyObjB = new ObjB(); 内部ObjA: ObjB MyObjB = new ObjB();

So main has a reference to ObjA, and ObjA has a reference to ObjB. 因此main引用了ObjA,而ObjA引用了ObjB。

Now let's say in the main class, i set MyObjA = null; 现在,在主类中,我将MyObjA = null;设置MyObjA = null; Will the garbage collector come along and destroy ObjA? 垃圾收集器会出现并摧毁ObjA吗? Or will it save it since it has a reference to ObjB (which is not destroyed yet)? 还是由于引用了ObjB(尚未销毁)而将其保存?

You can see how this would be a huge memory leak in the latter case. 您可以看到在后一种情况下这将是一个巨大的内存泄漏。 But as a side note, if the former case is true, then would it take 1 pass or 2 passes of the GC before ObjB is destroyed? 但是作为一个旁注,如果前一种情况是正确的,那么在销毁ObjB之前需要花1遍还是2遍GC?

Also, could someone tell me what would happen in Android Java too? 另外,有人可以告诉我Android Java中会发生什么吗? Those are the two languages I'm working with atm. 这些是我正在使用atm的两种语言。

Both MyObjA and MyObjB will be destroyed since they're unreachable from any GC roots (things like static variables, stack variables, or things that are recursively pointed to by things like this). MyObjA和MyObjB都将被销毁,因为它们无法从任何GC根目录访问(静态变量,堆栈变量之类的东西,或诸如此类的东西递归指向的东西)。

I'm not exactly sure what you mean by two passes. 我不确定两次通过的意思。 The details of the Java garbage collector are somewhat complicated but, if the objects are unreachable they should be cleaned up in a single GC cycle. Java垃圾收集器的细节有些复杂,但是,如果对象不可访问,则应在单个GC周期中清理它们。

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

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