final vs. volatile instance fields

Peter Levart peter.levart at gmail.com
Wed Dec 26 03:58:21 PST 2012


Hi hotspot gurus,

I have a question (actually two) someone might answer off the top of 
his/her hat:

Are there any performance differences in reading and writing of volatile 
vs. final instance fields? I can see that reflection is doing the same 
for both types (via UnsafeQualifiedFieldAccessorImpl subtypes) and is 
reading/writing the fields using Unsafe.getXxxVolatile/putXxxVolatile. 
So what does the JIT compiled code do?

a) Are there any differences between writing to those two types of 
fields? The Java spec. mandates writing to final fields exactly once in 
the constructors and Java Memory Model says that they are "frozen" at 
the end of constructor. What does "freeze" actually translate to in the 
compiled code?

b) I can understand that the JIT compiler can generate code that caches 
the value of final field say in a register, but initially the code has 
to load the value from a final field and the question is whether this 
initial load is any different from a volatile read?

Thank you,

Peter



More information about the hotspot-runtime-dev mailing list