简体   繁体   English

究竟什么是可用于垃圾收集的对象?

[英]At what point exactly is an object available for garbage collection?

I'm battling out of memory issues with my application and am trying to get my head around garbage collection. 我正在与我的应用程序争夺内存问题,并试图了解垃圾收集。 If I have the following code: 如果我有以下代码:

public void someMethod() {
   MyObject myObject = new MyObject();
   myObject.doSomething();  //last use of myObject in this scope
   doAnotherThing();
   andEvenMoreThings();
}

So my question is, will myObject be available for garbage collection after myObject.doSomething() which is the last use of this object, or after the completion of someMethod() where it comes out of scope? 所以我的问题是,将myObject可用于垃圾收集后myObject.doSomething()这是最后一次使用这个对象,或者完成后someMethod()它散发出来的范围是什么? Ie is the garbage collection smart enough to see that though a local variable is still in scope, it won't be used by the rest of the code? 即垃圾收集足够聪明,虽然局部变量仍在范围内,但其余代码不会使用它?

"Where it comes out of scope" “它来自范围”

public void someMethod() {
   MyObject myObject = new MyObject();
   myObject.doSomething();  //last use of myObject in this scope
   myObject = null; //Now available for gc
   doAnotherThing();
   andEvenMoreThings();
}

The best thing you can do is to take your code put it in a loop with a delay and hook up a profiler to it. 你可以做的最好的事情就是把你的代码放在一个有延迟的循环中,并将一个探查器连接到它。

If you are using a later version of Java then JVisualVm comes as standard. 如果您使用的是更高版本的Java,那么JVisualVm是标准配置。

If you are on windows and have JAVA_HOME set 如果您在Windows上并设置了JAVA_HOME

%JAVA_HOME%/bin/jvisualvm %JAVA_HOME%/ bin中/ jvisualvm

This will launch a profiler and you can see what objects are being collected and what are not. 这将启动一个分析器,您可以看到正在收集的对象和不收集的对象。 In my opinion this is an essential part of being a programmer and its fun to find the memory leaks. 在我看来,这是成为程序员的重要组成部分,以及查找内存泄漏的乐趣。

Hope this helps 希望这可以帮助

顺便说一下,在后面的java 6中,有一种类型的转义分析 ,其中JVM可以发现你的MyObject实例没有离开方法,因此它甚至可以将它完全放在堆栈上,你根本不需要任何GC。

So my question is, will myObject be available for garbage collection after myObject.doSomething() which is the last use of this object, or after the completion of someMethod() where it comes out of scope? 所以我的问题是,myObject.doSomething()是myObject.doSomething()的最后一次使用之后,还是在someMethod()完成后,myObject可用于垃圾收集?

The former. 前者。

Ie is the garbage collection smart enough to see that though a local variable is still in scope, it won't be used by the rest of the code? 即垃圾收集足够聪明,虽然局部变量仍在范围内,但其余代码不会使用它?

Scope is not visible to the GC which sees only registers, stacks, global variables and references from heap blocks to other heap blocks. GC看不到范围,只能看到寄存器,堆栈,全局变量以及从堆块到其他堆块的引用。 So scope is irrelevant. 所以范围无关紧要。

After local scope is out. 在本地范围之后。 as object can be reused and can live in the local scope. 对象可以重用,可以存在于本地范围内。 ie. 即。 it is marked for gc. 它标记为gc。 but not actually gc'ed . 但实际上不是gc'ed。

代码优化可能会注意到myObject的最后一次使用的位置,并使其可用于那里的垃圾收集,但从技术上讲,它将直到变量不再引用它(通过分配给其他东西)或超出范围。

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

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