Java Reference
In-Depth Information
There are other issues when using padding to prevent false sharing. The size of the padding
is hard to predict, since different CPUs will have different cache sizes. And the padding ob-
viously adds significant size to the instances in question, which will have an impact on the
garbage collector (depending, of course, on the number of instances required). Still, absent
an algorithmic solution, padding of the data can sometimes offer significant advantages.
One of the new features of Java 8 is the ability to reduce cache contention on specified fields (JEP
142). This is achieved by using a new annotation ( @sun.misc.Contended ) to mark variables that
should be automatically padded by the JVM.
The package that this annotation belongs to is significant: although this is a JDK Enhancement
Proposal (JEP) feature, it is intended for use primarily by the JVM itself. There is no guarantee
that the annotation will carry forward into future releases (nor that its behavior will remain the
If you are unable to solve false sharing through any other means, consider using this annotation.
One benefit is that, since the JVM understands the architecture of the CPU it is running on, it can
automatically figure out the size of the required padding—though when AMD (or whoever)
comes out with a new processor with a new cache line size, older versions of the JVM will have
to guess what the necessary size is. Like all padding solutions, though, using this annotation
greatly expands the size of the target instance, so use great caution as to where it is applied.
By default, the JVM ignores this annotation except within classes in the JDK. To enable applica-
tion code to use the annotation, include the -XX:-RestrictContended flag, which by default is
true (meaning that the annotation is restricted to JDK classes). On the other hand, to disable the
automatic padding that occurs in the JDK, set the -XX:-EnableContended flag, which by default
is true . This will lead to reductions in the size of the Thread and ConcurrentHashMap classes.
Search WWH ::

Custom Search