Wrapping sun.misc.Unsafe's native methods

Bernd Eckenfels ecki at zusammenkunft.net
Sat Jun 6 17:39:50 UTC 2015


I had some application problems and thought it would be a good idea to
wrap Unsafe.allocateMemory(long) to get a histogram of call-sites and
to trace the memory consumptions.

When I register an Java agent then sun.misc.Unsafe will not be loaded
through the transformer, since it is a rather early system component.

So I tried to redefine the class, which actually works. But I cannot
get the native methods wrapped. allocateMemory() does not not delegate
to a allocateMemory0() native method, so in order to wrap it I need to
use the setNativeMethodPrefix() and use retransformClass(). Hower this
does not work as I have to actually add the wrapped
prefix_allocateMemoty() method - which is not possible for

It looks like the netbeans/jvisualvm profiler has the same problem, it
just cannot see invocations of those methods. Is there a way around
this planned or possible? (I am not so good in JNI, maybe the native
method could be registered to a totally new class?)

For Java 9, when the Unsafe becomes more supported APIs, it would be
good if native methods could be easier accessible (if this does not
conflict with intrinsics?)


PS: on gc-dev I just asked if it would be possible to extend
BufferPoolMXBean with some more stats (allocation total cound, failed
allocations, alignment flag, max size):

More information about the serviceability-dev mailing list