简体   繁体   English

Java object 垃圾回收

[英]Java object garbage collection

I am working on the OCA Java certification and unsure of how to understand the answer to one question.我正在研究 OCA Java 认证,不确定如何理解一个问题的答案。

public class Test {

public static void main(String[] args){
    Student s1 = new Student("o1"); 
    Student s2 = new Student("o2");
    Student s3 = new Student("o3");
    s1=s3; 
    s3=s2;
    s2=null;
    //HERE
    }
}

The question is which object will be available for garbage collection after the //HERE point.问题是在 //HERE 点之后哪个 object 将可用于垃圾回收。

The answer provided by the online test is: One object (o1).在线测试提供的答案是:一个object(o1)。

Can someone explain me why?有人可以向我解释为什么吗?

Consider a simple class Student.考虑一个简单的 class 学生。

Step 1:第1步:

Student s1 = new Student("s1"); 
Student s2 = new Student("s2");
Student s3 = new Student("s3");

s1, s2, s3 are referencing 3 object in the heap space s1, s2, s3 在堆空间中引用了 3 object

在此处输入图像描述

Step 2:第2步:

Student s1 = s3; 

s1 is now referencing the s3 object in the heap space s1 现在在堆空间中引用 s3 object

the object s1 in the heap space has lost his reference堆空间中的 object s1 失去了他的引用

在此处输入图像描述

Step 3:第 3 步:

Student s1 = s3;
Student s3 = s2;

Variable s1 reference s3 heap space变量s1引用s3堆空间

Variable s3 reference s2 heap space变量s3引用s2堆空间

在此处输入图像描述

Step 4:第四步:

Student s1 = s3;
Student s3 = s2;
Student s2 = null;

Variable s1 reference s3 heap space变量s1引用s3堆空间

Variable s3 reference s2 heap space变量s3引用s2堆空间

Variable s2 lost his reference (null pointer)变量 s2 丢失了他的引用(空指针)

在此处输入图像描述

Conclusion:结论:

After line 11, one object is eligible for garbage collection第11行之后,一个object符合垃圾回收条件

Every time these kind of questions pop-up, the answer is still the same: after the comment HERE , every single object is eligible for garbage collection.每次弹出这样的问题,答案都是一样的:在HERE评论后,每一个 object 都有资格进行垃圾回收。 Nothing is used after that line, thus no strong reference exist to any object, thus everything is eligible for GC.该行之后没有使用任何内容,因此不存在对任何object 的强引用,因此所有内容都符合 GC 条件。 Unfortunately, these kind of questions make sense only in the context of getting the correct "points" for the exam, thus people learn them the way they are.不幸的是,这些问题只有在获得正确的考试“分数”的情况下才有意义,因此人们按照它们的方式学习它们。 The reality is that without a broader context of reachability , they only confuse users, imo.现实情况是,如果没有更广泛的可达性背景,它们只会让用户感到困惑,imo。

Think about - are there are any live references to any of your objects after that comment?想一想 - 在该评论之后是否有任何对您的任何对象的实时引用? No. As such, is every single instance eligible for GC?否。因此,是否每个实例都符合 GC 的条件? yes.是的。 And note that they are eligible after that comment, not after the methods ends.请注意,他们在评论之后才有资格,而不是在方法结束之后。 Do not mash scope an reachability together.不要将scope可达性混合在一起。

Student s1 = new Student("o1"); 
Student s2 = new Student("o2");
Student s3 = new Student("o3");
s1=s3; // The "s1" variable now points to the object referenced to by s3, ie "o3"
s3=s2; // The "s3" variable now points to the object referenced to by s2, ie "o2"
s2=null; // The "s2" variable points to nothing

At the end of the execution, objects "o3" and "o2" are referenced by variables ("s1" and "s3" respectively).在执行结束时,对象“o3”和“o2”被变量引用(分别为“s1”和“s3”)。 As such, the object "o1" is not pointed to by any variable, and is available for destruction by the garbage collector.因此,object“o1”没有被任何变量指向,并且可以被垃圾收集器销毁。

After exiting the method all object will be eligible for garbage collection, since s1,s2,s3 are local variables and references are not passed outside.退出该方法后,所有 object 都将符合垃圾回收条件,因为 s1、s2、s3 是局部变量,引用不会传递到外部。

However on the last line of the method, s1 holds reference to o3, s2 points to nowhere, s3 points to o2.然而在该方法的最后一行,s1 持有对 o3 的引用,s2 指向无处,s3 指向 o2。 Only o1 has no reference pointing on him, therefore it is eligible for garbage collection.只有 o1 没有指向他的引用,因此它有资格进行垃圾收集。

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

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