2

The Oracle Java Magazine contains an article about JavaFX design patterns, containing the following code:

while(!pointQueue.isEmpty()) {
   PointPojo point = pointQueue.poll();
   //coordinate transform data to canvas pixel
   double x = transformXToScreen(point.x);
   double y = transformYToScreen(point.y);
   //encourage the object to be garbage collected sooner
   point = null;
   g.fillOval(x, y, radius, radius);
}

I can't see why the point = null assignment would make the point object be garbage collected sooner. There variable is (re)assigned every loop, the loop doesn't block as it only runs as long as there is something to get.

Even if the loop would do a blocking call, the point variable would move out of scope each loop iteration, so it would make the reference eligible for garbage collection.

What am I missing? Why would the article's author add this specific null assignment?

5
  • 1
    there is a related question on the performance of nulling out a reference vs. waiting for the variable to go out of scope at stackoverflow.com/questions/449409/… Commented Jul 1, 2018 at 9:09
  • 7
    Because the article's author doesn't understand Java. Commented Jul 1, 2018 at 9:15
  • 3
    It won't. It's completely nonsensical. Commented Jul 1, 2018 at 9:20
  • "The author is a recently elected Java Champion", so the odds that the author doesn't understand Java seems unlikely. That still doesn't explain the need for the null assignment, and I remain at the conclusion that there is no advantage. Commented Jul 1, 2018 at 10:24
  • "That still doesn't explain the need for the null assignment" There is no need for it. Commented Jul 1, 2018 at 13:34

2 Answers 2

2

Summarizing the spot-on comments by David Conrad and Boris the Spider:

I can't see why the point = null assignment would make the point object be garbage collected sooner.

It won't. It's completely nonsensical.

What am I missing?

Nothing.

Why would the article's author add this specific null assignment?

Because the article's author doesn't understand Java.

Sign up to request clarification or add additional context in comments.

2 Comments

You wouldn't expect this in Oracle's Java Magazine.
Why not? It's a magazine, not the language specs.
1

Assigning null would make sense as a hint to the compiler that the object is no longer needed. However, this doesn't work. The compiler can't possible know that there are other references to the Point object. After all, the object might be in another List, another Queue or something else might hold a reference to it. Thus, it's not a 'hint' for the compiler neither is it really a useful hint for the garbage collector because all you're doing is setting a reference to null. However... iff the Queue is the sole object holding a reference to a Point then by setting point = null releases the last reference to it which means from this point forward the object is not reachable anymore and can be garbage collected.

Setting a reference to null that leaves scope immediately seems a bit superfluous to me. Setting something to null doesn't magically cause the GC to kick in. The GC kicks in whenever it wants to kick in or in some cases for languages with parallel GC the GC might be constantly running.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.