Search code examples
javaperformancemetricsjmcjfr

How to enable heap metrics for jfr via command line


I'm trying to run Java Flight Recorder using the jcmd binary located /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/jcmd (Mac OS X). Is there a command line option to enable heap metrics that corresponds to Heap Statistics option on the Start Flight Recording UI:

enter image description here


Solution

  • JDK 17

    $ java -XX:StartFlightRecording:jdk.ObjectCount#enabled=true ...
    
    $ jcmd <pid> JFR.start jdk.ObjectCount#enabled=true
    

    JDK 9 - JDK 16

    For JDKs prior to JDK 17 there is no command line or jcmd option to enable Heap Statistics.

    The event is disabled in both the default and profile settings file as it induces a safepoint when iterating over the heap.

    You can however use the template manager in JMC, Windows -> Template Manager. and check the settings you want to have, i.e Heap Statistics, and export the file. Then use jcmd to apply it

    jcmd <pid> JFR.start settings=my-configuration-file.jfc
    

    If you are trying to diagnose a memory leak, it may be of interest to know that Oracle JDK 10+ and OpenJDK 11+ records the Old Object Sample event which collects information that is typically more useful for solving Java memory leaks (and without the safepoint overhead). Later JDKs have the option path-to-gc-roots=true (for both jcmd and -XX:StartFlightRecording) that can give additional information to solve memory leaks. That option will however induce similar overhead as Heap Statistics, but much less overhead than an HPROF dump as magnitudes less data is written to disk.