Search code examples
javaprofilingperformance-testing

How to profile a Java program run from start to finish?


I have a Java program that runs for only about 20-30 seconds and I want to profile it.

Firing up the jvisualvm profiler manually at each start is not reliable, because you lose several seconds operating the UI.

Is there a way to profile the entire exectution, like the old -Xhprof:cpu=samples which no longer works?


Solution

  • With Flight Recorder you can add to the command line

    -XX:+UnlockCommercialFeatures -XX:+FlightRecorder 
    -XX:FlightRecorderOptions=defaultrecording=true,dumponexit=true,settings=profile
    

    https://docs.oracle.com/javacomponents/jmc-5-5/jfr-runtime-guide/run.htm#JFRRT176

    It only records the memory allocation on a new TLAB, so I make it much smaller than the default to get more samples.

    -XX:TLABSize=128k
    

    You need to run jmc from the $JAVA_HOME/bin directory to open the .jfr file created.

    I find this harder to use than VisualVM, but produces much more detailed results with less noise. i.e. you get far fewer allocations causes by the profiler itself.