Starting the Java HPROF profiler on-the-fly programmatically
Asked Answered
C

1

8

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?

Cankered answered 22/10, 2011 at 23:30 Comment(0)
W
1

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.

Wiseacre answered 22/7, 2012 at 10:52 Comment(2)
Sadly, this seems to be the right answer, at least as far as HPROF is concerned... +1Cankered
I'm not convinced this is a good answer. It depends on more precisely what the original question is addressing. The cited documentation says the profiling agent must start before the JVM executes any code... it does not say that the profiling must begin before code starts executing. The profiler can start and the profiling can start later. A programmatic way to control when profiling starts would be very useful. Can you say that profiling must start before any code executes? And cite a reference?Inferential

© 2022 - 2024 — McMap. All rights reserved.