简体   繁体   English

该对象什么时候可以进行垃圾收集?

[英]When does this object become eligible for garbage collection?

I have this piece of code, and I am confused about when the Object o becomes eligible for garbage collection in Java. 我有这段代码,我对Object o何时有资格在Java中进行垃圾回收感到困惑。

    public class JustSo
    {
        public static void main(String[] args)
        {
             for(int i=0;i<4;i++)
             {
                     Object o=new Object();
                     //o.doSomething();
             }
             System.out.println("DONE");
        }
    }

Since it is inside a loop(or any block for that matter), it will become eligible at the end of the loop right? 由于它位于循环(或与此相关的任何块)中,因此它将在循环结束时变为合格状态,对吗?

But I found an answer on a reliable developer site(Don't want to disclose where) that says the earliest the object becomes eligible is at the print statement. 但是我在一个可靠的开发人员站点上找到了一个答案(不想透露位置),该答案说最早的对象符合条件的是打印声明。

Please clarify. 请澄清。

When it becomes out of scope, so after each iteration of the for loop. 当它超出范围时,请在for循环的每次迭代之后进行。 An object is eligible for garbage collection when it is no longer reachable, this occurs in 2 situations. 当对象不再可访问时,有资格进行垃圾回收,这在两种情况下会发生。

  • The object no longer has any references point to it. 该对象不再具有指向它的引用。
  • All references to the object have gone out of scope. 对对象的所有引用均超出范围。

The latter is the situation for the object in question, and it the object's reference is out of scope after the for loop. 后者是所讨论对象的情况,并且在for循环之后,对象的引用超出范围。 Prior to the print statement. 在打印报表之前。

Since you are not saving the reference into some outer-scope variable,and creating new instance each time as soon as single iteration complete, its eligible for garbage collection. 由于您没有将引用保存到某个外部作用域变量中,并且每次一次迭代完成后都不会立即创建新实例,因此可以进行垃圾回收。

So when you came out of loop, all the objects created inside eligible for GC. 因此,当您退出循环时,在内部创建的所有对象都可以使用GC。

May be you confused with author wording here. 可能是您对作者的措辞感到困惑。 Consider below code 考虑下面的代码

 for(int i=0;i<4;i++)
             {
                     Object o=new Object();
                     //o.doSomething();
             } -----> objects ready for GC here. 
             // some other code  
             // some other code
             System.out.println("DONE");

As the oracle documentation about the garbagecollector says: 正如有关垃圾收集器的oracle文档所说:

Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. 自动垃圾收集是查看堆内存,识别正在使用和未使用的对象以及删除未使用的对象的过程。 An in use object, or a referenced object, means that some part of your program still maintains a pointer to that object. 使用中的对象或引用的对象意味着程序的某些部分仍维护着指向该对象的指针。 An unused object, or unreferenced object, is no longer referenced by any part of your program. 程序的任何部分都不再引用未使用的对象或未引用的对象。 So the memory used by an unreferenced object can be reclaimed. 因此,可以回收未引用对象使用的内存。

This means as soon as an Object isn't referenced from any other Object or is out of scope, it will be marked for deletion. 这意味着一旦一个Object没有从任何其它引用的Object或超出范围,它将被标记为删除。 In this case, the reference is out of scope, each time the loop starts again (just before every increment). 在这种情况下,每次循环再次开始时(每次增量之前),引用就超出范围。 Thus each object will be marked for deletion at the end of the loop (before the next loop-repetition). 因此,每个对象将在循环结束时标记为删除(在下一个循环重复之前)。

每次循环时,大约在i++发生时,该对象将符合GC的条件。

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

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