垃圾收集 - 关于链表的两个案例

时间:2014-01-15 03:51:59

标签: java garbage-collection linked-list

案例1: 如果有像这样的LinkedList:

A --> B --> C --> D

如果我制作A.next = null,那么图表将是A --> nullB --> C --> D

节点B,C,D会被垃圾收集吗?

案例2: 如果有像这样的LinkedList:

A --> B --> C --> D

如果我生成A.next = A.next.next,那么图表将为A --> C --> D,但B仍然指向C,如下所示:B --> C节点B是否会被垃圾收集?

1 个答案:

答案 0 :(得分:0)

案例1

Case 1: If there is a LinkedList like this:

A --> B --> C --> D
if I make A.next = null, so the graph would be A --> null and B --> C --> D

Will node B,C,D be garbage collected?

在这种情况下,如果B中没有引用,则B,C,D是垃圾收集。如果您有任何引用B的对象,那么所有三个都存在并被视为独立链表

案例2

Case 2: If there is a LinkedList like this:

A --> B --> C --> D
if I make A.next = A.next.next, so the graph would be A --> C --> D but also B still points to C like this: B --> C Will node B be garbage collected?

同样的概念适用于此处,如果有任何参考,那么再次B-> C被视为独立的链表

相关问题