Search code examples
javaexceptionprofilingjmcjfr

How to profile the number of exceptions generated, categorized by exception class or line of code


I'm using a number of open-source third-party libraries, and a flight recording shows that the code is generating tens of thousands of exceptions per second.

How can I track down which type of exception is being thrown, and where in the source code it's being thrown, so that I can see if I can fix the third-party code myself?

Java Mission Control does not show any breakdown of exception types or sources, as far as I can see.

Please do not recommend expensive commercial profilers, as this is for an open source project.


Solution

  • Java Mission Control does not show any breakdown of exception types or sources, as far as I can see.

    That is not correct, Java Mission Control does provide that information.

    First you need to enabled tracing of exceptions in Flight Recorder (by default only java.lang.Error descendants are traced).

    See flight recorder settings screen shot below.

    Flight Recorder options

    Once recorder you need to go to "Exceptions" report.

    There you would find breakdown of exceptions by class. "Stack Trace" view would allow to get exact stack traces.

    Mission Control

    I little JMC live hack: bring context menu on "Stack Trace" view and select "Distinguish Frames By" > "Line Number" (see on screenshot above). That would allow you to see line numbers.

    If you prefer CLI tools take a look at SJK (disclaimer, this is my own OSS profiler). E.g. command below would produce SVG flame graph of exceptions from you JFR dump. There are also few other report types.

    java -jar sjk.jar ssa -f dump.jfr --jfr-event THROW --flame > throw_flame.svg