javaoptimizationjvmvm-implementation

How to find out what optimizations the JVM applied to my code?


The JVM (especially the HotSpot VM) is famous for having a huge number of optimizations it can apply at runtime.

Is there a way to look at a certain piece of code and see what the JVM has actually done to it?


Solution

  • One problem is that "what JVM has actually done to it" changes between invocations as the JVM is free to re-generate code.

    As an example I investigated some days ago what Hotspot does with final methods compared to virtual methods. Judging by microbenchmarks, my conclusions were:

        if (object instanceof ClassOfNonVirtualCall) {
            do non-virtual call to ClassOfNonVirtualCall.method
        } else {
            do virtual call to object.method
        }
    

    If you are really interested in seeing generated code, you may play with DEBUG JVMs from OpenJDK:

    http://dlc.sun.com.edgesuite.net/jdk7/binaries/index.html

    http://wikis.sun.com/display/HotSpotInternals/PrintAssembly