Search code examples
javaprofilingjvisualvm

How does JVisualVM start and stop CPU profiling?


I want to add an option in my application where the end user can start CPU profiling, reproduce a performance issue, stop CPU profiling, and then send the profiling information to our support team as an attachment to an email. I'm hoping I can use the same mechanism that JVisualVM uses to stop and start the profiling and return the results.

Does anybody know how JVisualVM makes this happen? It doesn't require the -agentlib:hprof option to be set on the command line - does it use hprof or some other mechanism?


Solution

  • JVisualVM instruments the code by using a JMX command to add an agent dynamically. This is non-trivial to implement. You might find that JVisualVM has an API, if so I would use that rather than attempt to implement it yourself. If it doesn't have a public API and I suspect it doesn't, I suggest attempting something simpler.