Starting the Java HPROF profiler on-the-fly programmatically

1.3k views Asked by At

I am trying to selectively monitor/profile a Java application when certain runtime conditions are met. I am already able to dump the heap on-the-fly using the HotSpotDiagnosticMXBean, which has been quite useful.

I am now trying to do something similar with CPU profiling, hoping to more effectively target specific code paths. I have some experience with the command-line HPROF interface and the NetBeans profiler, but both need to be started beforehand.

Is there a way to activate the HPROF profiler programatically from within a running application? Preferably something that would allow me to start and stop the profiling process at will?

1

There are 1 answers

2
Soulman On BEST ANSWER

According to the JVMTI documentation, native agent libraries like hprof must be loaded very early during the JVM initialization, before any bytecode is executed. So no, it is not possible to do this on-the-fly.

An alternative could be to re-launch the java process with modified JVM parameters within the main function, although this is not trivial.