I suppose that a program like this...
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.");
}
}
...may possibly output "this object is known to never be referenced."
before "done"
. (Correct me if I'm wrong here!)
Furthermore, it is easy for a compiler/JVM to detect "unread locals". In the program below for instance, Eclipse notices that "The local variable t is never read".
However would it be illegal for the JVM to output "this object is known to never be referenced."
before "done"
given the (.class version) 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 documentation of garbage collection talk about reachability. Given the fact that t
is never read, the object is obviously not "reachable", or?
References to the JLS are appreciated.
An object is eligible to be garbage collected if its reference variable is lost from the program during execution. Sometimes they are also called unreachable objects. What is reference of an object? The new operator dynamically allocates memory for an object and returns a reference to it.
When an object created in Java program is no longer reachable or used it is eligible for garbage collection.
An object is eligible for garbage collection when there are no more references to that object. References that are held in a variable are usually dropped when the variable goes out of scope. Or, you can explicitly drop an object reference by setting the variable to the special value null.
gc() asks the garbage collector to run, but the garbage collector never makes any guarantees about when it will run or what unreachable objects it will free from memory. Option C is wrong.
In 12.6.1 of the Java Language Specification says:
12.6.1 Implementing Finalization
Every object can be characterized by two attributes: it may be reachable, finalizer- reachable, or unreachable, and it may also be unfinalized, finalizable, or finalized. A reachable object is any object that can be accessed in any potential continuing computation from any live thread. Optimizing transformations of a program can be designed that reduce the number of objects that are reachable to be less than those which would naively be considered reachable.
For example, a compiler or code generator may choose to set a variable or parameter that will no longer be used to null to cause the storage for such an object to be potentially reclaimable sooner.
The last phrase seems to me covering exactly the case you are asking about. The variable t
can be set to null
implicitly before the end of the scope, therefore making the object unreachable.
This in C++ would be a disaster because a lot of code depends on exact destruction timing at the end of scope (e.g. for locks).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With