Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting the Java HPROF profiler on-the-fly programmatically

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?

like image 728
thkala Avatar asked Oct 22 '11 23:10

thkala


1 Answers

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.

like image 88
Soulman Avatar answered Oct 05 '22 23:10

Soulman