相互链接的Java对象数组[duplicate]

时间:2022-04-22 11:58:53

This question already has an answer here:

这个问题已经有了答案:

Suppose that, in Java, you have an array of objects that can point to each other. When the array goes out of scope, is the whole thing garbage collected, even though the elements reference each other?

假设在Java中,您有一个对象数组,这些对象可以指向彼此。当数组超出范围时,即使元素之间相互引用,整个事情是否被垃圾收集?

2 个解决方案

#1


3  

The Garbage Collector actually works out what is reachable and then disposes of everything else, rather than the other way around.

垃圾收集器实际上计算出什么是可访问的,然后处理其他所有东西,而不是反过来。

As you've realised a "reference counting" approach falls down as soon as you have circular references.

当你意识到“引用计数”的方法一旦循环引用就会失效。

There is a full description of Java GC here: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

这里有对Java GC的完整描述:http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

#2


2  

Yes. The garbage collector is smart enough to know the whole linked list is eligible for garbage collection.

是的。垃圾收集器足够聪明,可以知道整个链表都适合进行垃圾收集。

#1


3  

The Garbage Collector actually works out what is reachable and then disposes of everything else, rather than the other way around.

垃圾收集器实际上计算出什么是可访问的,然后处理其他所有东西,而不是反过来。

As you've realised a "reference counting" approach falls down as soon as you have circular references.

当你意识到“引用计数”的方法一旦循环引用就会失效。

There is a full description of Java GC here: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

这里有对Java GC的完整描述:http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

#2


2  

Yes. The garbage collector is smart enough to know the whole linked list is eligible for garbage collection.

是的。垃圾收集器足够聪明,可以知道整个链表都适合进行垃圾收集。