How to detect if the VM is running with compact refs from within the VM (no agent)?

Uwe Schindler uschindler at
Wed Mar 28 14:45:45 PDT 2012



This was very helpful. We used the Unsafe methods to improve our RAM usage
estimation, the MX bean was not needed to detect the ref size, but we use it
to detect another thing: object alignment (all object sizes are multiples of
that because they are aligned using that factor). For 32 bit JVMs it is
always 8, but in 64 bit JVMs it can be changed by -XX:ObjectAlignmentInBytes


To detect this parameter we use the following code:


    // Try to get the object alignment (the default seems to be 8 on

    // regardless of the architecture).

    int objectAlignment = 8;

    try {

      final Class<?> beanClazz =

      final Object hotSpotBean = ManagementFactory.newPlatformMXBeanProxy(





      final Method getVMOptionMethod = beanClazz.getMethod("getVMOption",

      final Object vmOption = getVMOptionMethod.invoke(hotSpotBean,

      objectAlignment = Integer.parseInt(




    } catch (Exception e) {

      // Ignore.



    NUM_BYTES_OBJECT_ALIGNMENT = objectAlignment;


This works fine for detection of the object alignment. The above code is in
a static class initializer. Full class can be found here:


Now the question:


The daily Lucene benchmark tests had suddenly a slowdown in query rate
(queries to Lucene index per second, see - it went down
after the commit of above code on March 18th). We were looking the whole day
to find the issue (there was another possible commit on this day causing
problems, but this one caused this). The interesting thing, by uncommenting
the above code that retrieves the HotspotMXBream, the slowdown disappears.


Does getting the HotSpotDiagnosticMXBean somehow trigger some internal state
in the virtual machine (e.g. attaching an agent lib), so this slows down
hotspot a little bit?





Uwe Schindler

uschindler at 

Apache Lucene PMC Member / Committer

Bremen, Germany


From: hotspot-compiler-dev-bounces at
[mailto:hotspot-compiler-dev-bounces at] On Behalf Of Krystal
Sent: Wednesday, March 14, 2012 7:13 PM
To: John Rose
Cc: hotspot compiler
Subject: Re: How to detect if the VM is running with compact refs from
within the VM (no agent)?


Couldn't find these two fields in sun.misc.Unsafe (in JDK6u30), found
arrayIndexScale(Class) and addressSize() methods instead.


Works as advertised. Thanks John! Learning new bits everday for me, too :-)

- Kris

On Thu, Mar 15, 2012 at 1:53 AM, John Rose <john.r.rose at> wrote:

On Mar 14, 2012, at 1:41 AM, Krystal Mok wrote:

I'm not aware of any way to get the exact "reference size" (or "oop size" in
HotSpot's terms). But it's possible to know whether compressed oops is in
use or not. 


For some platforms, you could inspect
sun.misc.Unsafe.ARRAY_OBJECT_INDEX_SCALE (= oopSize) and ADDRESS_SIZE (=


- John


-------------- next part --------------
An HTML attachment was scrubbed...

More information about the hotspot-compiler-dev mailing list