Garbage collection details: Is this object suitable for the GC?

I believe such a program ...

class Test { public static void main(String[] args) { new Test(); System.out.println("done"); } protected void finalize() { System.out.println("this object is known to never be referenced."); } } 

... perhaps print "this object is known to never be referenced." to "done" . (Correct me if I am wrong!)

In addition, the / JVM compiler can easily detect "unread locales". In the program below, for example, Eclipse notes that "The local variable t is never read."

However, it would be illegal for the JVM to output "this object is known to never be referenced." to "done" given the version (.class) of the program below?

 class Test { public static void main(String[] args) { Test t = new Test(); System.out.println("done"); } protected void finalize() { System.out.println("this object is known to never be referenced."); } } 

Most garbage collection documentation talks about reachability. Given that t never readable, the object is obviously not "accessible", or?

Links to JLS are welcome.

+8
java garbage-collection
source share
2 answers

In 12.6.1, the Java Language Specification says:

12.6.1 Finalizing

Each object can be characterized by two attributes: it can be accessed, the finalizer is reachable or inaccessible, and it can also be non-finalized, finalized or completed. A reach object is any object that can be accessed by any potential calculation from any live thread. Optimization of transformations can be developed a program that reduces the number of objects that are achievable less than would be naively considered achievable.

For example, the compiler or code generator may choose to set a variable or a parameter that will no longer be used for failure to cause storage for such an object to be potentially regenerated sooner or later.

The last phrase, it seems to me, refers to exactly the case that you are asking for. The variable t can be set to null implicitly to the end of the scope, so make the object inaccessible.

This in C ++ will be a disaster, because a lot of code depends on the exact time of destruction at the end of the scope (for example, for locks).

+5
source share

... maybe it could return "this object, as you know, never refers." to "done."

There are three possible behaviors:

  • the message is displayed before "done",

  • the message is displayed after "done",

  • "this object is never known," the message is not displayed at all.

(In practice, the most likely behavior is the latter. In fact, it is a virtual assurance if you do not create a lot of garbage between creating an instance of Test and printing β€œdone”.)

However, it would be illegal for the JVM to output "this object, as you know, never refers." to "done" taking into account the version (.class) of the program below?

No, that would not be illegal. The Test instance cannot be "available in any potential ongoing calculation from any live thread" and therefore not available. Therefore, it would be legal for the JVM to collect garbage and terminate it immediately.

However, three events that must occur for a message:

  • GC must start,
  • GC must detect the object as unreachable, and
  • GC must complete the object.

There is no guarantee that all these events will occur before the application exits, but only in the window between creating the instance and printing "done".


The bottom line is that you should never depend on the finalization available in your Java application.

+2
source share

All Articles