3

In C++ you need to manually delete nodes in the 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:

    node1.next = node3;

be adequate?

3 Answers3

3

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

So if you have a singly linked list like so

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

And assume head = node 1

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

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

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

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.

CollinD
  • 7,304
  • 2
  • 22
  • 45
  • 1
    "In scope" isn't quite right, though. Scope is a lexical structure which is only loosely related to object lifetime. Objects will become eligible for GC when they are no longer *reachable* by any running code, which is not the same as "in scope". – Daniel Pryden Oct 17 '15 at 07:47
  • That's a fair distinction. I suppose it would be more appropriate to say that most of the time, the GC is able to pick up objects because their references go out of scope, thus making them unreachable. Thanks for the improved technical explanation! – CollinD Oct 17 '15 at 07:49
  • 2
    Actually, your edited answer isn't quite right either, since an object *can* become unreachable while a reference to it is still in scope. Basically as soon as the compiler can prove that a reference is never read anymore, it is free to reuse the local variable in the frame for something else, at which point the object in question no longer has any references and is eligible for GC. Raymond Chen explains this (for C#, although Java is similar) in his blog post [When does an object become eligible for garbage collection?](http://blogs.msdn.com/b/oldnewthing/archive/2010/08/10/10048149.aspx) – Daniel Pryden Oct 17 '15 at 19:37
  • 1
    @Daniel Pryden: you are right, similar things for Java are discussed [here](http://stackoverflow.com/a/24380219/2711488) and [here](http://stackoverflow.com/q/26642153/2711488) – Holger Oct 19 '15 at 13:48
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.

in your case

node1.next = node3;

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

Ling Zhong
  • 1,744
  • 14
  • 24
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.

Eran
  • 387,369
  • 54
  • 702
  • 768