Java - 自动垃圾收集如何为LinkedList工作?

时间:2021-05-07 03:49:54

In C++ you need to manually delete nodes in the LinkedList:

在C ++中,您需要手动删除LinkedList中的节点:

  Node* node1 = new Node(s);
  Node* node2 = new Node(s);
  Node* node3 = new Node(s);

  node1 -> next = node2;
  node2 -> next = node3;

  //remove node2 by: 
  delete node2;
  node1 -> next = node3;

For Java (it's my first 2 days learning it) how does automatic garbage collection know when to take action? Would:

对于Java(这是我的前两天学习它),自动垃圾收集如何知道何时采取行动?将:

    node1.next = node3;

be adequate?

3 个解决方案

#1


3  

An object in java will become eligible for garbage collection once no references to it are in scope.

一旦没有对它的引用在范围内,java中的对象就有资格进行垃圾收集。

So if you have a singly linked list like so

所以如果你有一个像这样的单链表

(1) -> (2) -> (3)

And assume head = node 1

并假设head =节点1

Then yes, setting head.next = head.next.next will allow node 2 to be GC'd at some point.

然后是的,设置head.next = head.next.next将允许节点2在某个时刻进行GC。

However, in your example, your node2 could not go away until you explicitly declare

但是,在您的示例中,在您明确声明之前,您的node2无法消失

node2 = null in addition to node1.next = node3 because the reference as node2 would keep it in scope.

除了node1.next = node3之外,node2 = null,因为作为node2的引用会将其保留在范围内。

Note that node2 = null is not actually doing ANYTHING to the Node object that node2 used to point to. Instead it simply sets the reference (since all objects in java are really pointers) to null.

请注意,node2 = null实际上并不对node2用于指向的Node对象执行任何操作。相反,它只是将引用(因为java中的所有对象都是指针)设置为null。

#2


2  

The Java garbage collector PERIODICALLY runs through the entire object reference map to find and delete objects that are unreferenced or cyclically referenced. You can hint the JVM to start an iteration of garbage collection, but there is no way for you to pragmatically instruct the JVM to garbage collect deterministically.

Java垃圾收集器PERIODICALLY遍历整个对象引用映射,以查找和删除未引用或循环引用的对象。您可以提示JVM启动垃圾收集的迭代,但是您没有办法以实际方式指示JVM确定性地进行垃圾收集。

in your case

在你的情况下

node1.next = node3;

would be adequate as node2 will no longer have any objects referencing it

因为node2将不再有引用它的任何对象,这将是足够的

#3


1  

Assuming you modify the LinkedList in a way such that there are no more references to node2, the garbage collector can release its memory. In other words, node1.next = node3; should be enough, assuming there are no other references to node2.

假设您以不再有对node2的引用的方式修改LinkedList,垃圾收集器可以释放其内存。换句话说,node1.next = node3;应该足够了,假设没有其他对node2的引用。

#1


3  

An object in java will become eligible for garbage collection once no references to it are in scope.

一旦没有对它的引用在范围内,java中的对象就有资格进行垃圾收集。

So if you have a singly linked list like so

所以如果你有一个像这样的单链表

(1) -> (2) -> (3)

And assume head = node 1

并假设head =节点1

Then yes, setting head.next = head.next.next will allow node 2 to be GC'd at some point.

然后是的,设置head.next = head.next.next将允许节点2在某个时刻进行GC。

However, in your example, your node2 could not go away until you explicitly declare

但是,在您的示例中,在您明确声明之前,您的node2无法消失

node2 = null in addition to node1.next = node3 because the reference as node2 would keep it in scope.

除了node1.next = node3之外,node2 = null,因为作为node2的引用会将其保留在范围内。

Note that node2 = null is not actually doing ANYTHING to the Node object that node2 used to point to. Instead it simply sets the reference (since all objects in java are really pointers) to null.

请注意,node2 = null实际上并不对node2用于指向的Node对象执行任何操作。相反,它只是将引用(因为java中的所有对象都是指针)设置为null。

#2


2  

The Java garbage collector PERIODICALLY runs through the entire object reference map to find and delete objects that are unreferenced or cyclically referenced. You can hint the JVM to start an iteration of garbage collection, but there is no way for you to pragmatically instruct the JVM to garbage collect deterministically.

Java垃圾收集器PERIODICALLY遍历整个对象引用映射,以查找和删除未引用或循环引用的对象。您可以提示JVM启动垃圾收集的迭代,但是您没有办法以实际方式指示JVM确定性地进行垃圾收集。

in your case

在你的情况下

node1.next = node3;

would be adequate as node2 will no longer have any objects referencing it

因为node2将不再有引用它的任何对象,这将是足够的

#3


1  

Assuming you modify the LinkedList in a way such that there are no more references to node2, the garbage collector can release its memory. In other words, node1.next = node3; should be enough, assuming there are no other references to node2.

假设您以不再有对node2的引用的方式修改LinkedList,垃圾收集器可以释放其内存。换句话说,node1.next = node3;应该足够了,假设没有其他对node2的引用。