Reachability of an object when a thread is running one of its methods

Carlo de Wolf cdewolf at redhat.com
Tue Dec 4 04:12:46 PST 2012


Hello EG,

I would like to ask you to clarify the reachability of an object when a 
thread is running one of its methods. [1]

JLS 7 12.6.1:
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 Java 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.

Based on this the IBM JDK sets 'this' (local variable #0) to null when 
it is no longer actively being used by the current method invocation (or 
a different implementation with similar effect). Because of their highly 
aggressive GC, the object is then immediately finalized.

Personally I find this a bit dirty, as the object is semantically still 
in use.

JLS 7 12.6.1:
Note that this sort of optimization is only allowed if references are on 
the stack, not stored in
the heap.

This hints to me that an object which is on the stack (of the previous 
frame) is actually strong referenced.

Could you please clarify the following behaviors?
1. Is it allowed to nullify 'this' and thus clearing the local variable 
reference to 'this'?
2. Is a reference from the stack of the previous frame considered a 
strong reference?

Thanks,

Carlo

[1] 
https://www.ibm.com/developerworks/forums/message.jspa?messageID=14915477#14915477 



More information about the java-se-8-spec-comments mailing list