Replacing Unsafe.allocateInstance

Paul Sandoz paul.sandoz at
Tue Jun 30 12:08:53 UTC 2015

On Jun 27, 2015, at 2:54 AM, Henri Tremblay <henri at> wrote:
> My latest benchmarks were showing it much slower than
> ReflectionFactory.newConstructorForSerialization. I must confess I haven't
> had the time to investigate correctly to see where the difference is coming
> from. And haven't try with JDK9 yet.

I observe similar performance results on JDK9.

Place the type in a static final field and you should see similar performance to ordinary object construction. So in part it might be because HotSpot cannot constant fold certain operations associated with the type.

You will also notice a difference if say java.util.ArrayList is used instead of java.lang.Object. The former is slightly faster. I dunno why.

I suspect ReflectionFactory.newConstructorForSerialization is faster than Unsafe.allocateInstance because the former spins up a class that specifically constructs the type. Although it appears the wrapping Constructor around an instance of that spun up class may be susceptible to profile pollution.


More information about the jdk9-dev mailing list