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?

M. le Rutte
  • 3,525
  • 3
  • 18
  • 31
  • 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 https://stackoverflow.com/questions/449409/does-assigning-objects-to-null-in-java-impact-garbage-collection – Daniele Jul 01 '18 at 09:09
  • 7
    Because the article's author doesn't understand Java. – David Conrad Jul 01 '18 at 09:15
  • 3
    It won't. It's completely nonsensical. – Boris the Spider Jul 01 '18 at 09: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. – M. le Rutte Jul 01 '18 at 10:24
  • *"That still doesn't explain the need for the null assignment"* There is no need for it. – Max Vollmer Jul 01 '18 at 13:34

2 Answers2

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.

Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
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.

user207421
  • 305,947
  • 44
  • 307
  • 483
mroman
  • 1,354
  • 9
  • 14