简体   繁体   中英

It it possible to take a snapshot for Java Profilers with Java?

I would like to periodically take a snapshot of my runtime (the .snapshot files used on profilers for Java, like YourKit, JProfiler, VisualVM, etc), is possible to take a snapshot by calling a method or something else? With Java, running on the same jvm?

I would suggest periodically running a Java Flight Recorder.

What is it? It is a profiling tool similar to the ones you already mentioned, it captures all the usual data .

How to use it? Here is a short description on how to start it up. Be careful that the application you want to profile needs these extra JVM params :

java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder

And finally for a periodic scheduling you need a command that you can use from crontab. For that you can use the example command from the previous link:

jcmd 5368 JFR.start duration=60s filename=myrecording.jfr

Where "5368" is the PID of your profiled application and the rest is self-explanatory.

Each profiler will have a different mechanism for periodically saving snapshots.

For JProfiler , use a "Timer" trigger with a "Save snapshot" action. If you want to record data for a limited time just before saving the snapshot, add the following sequence of trigger actions:

  • "Start recording" (with the desired recording types selected)
  • "Sleep" (for the desired amount of time)
  • "Stop recording"
  • "Save snapshot" (with "add unique number to file name" selected)

在此处输入图片说明

If you would rather like to control recording and saving snapshots programmatically from the same JVM, use the Controller API like this:

Controller.startCPURecording(true);
Thread.sleep(10000);
Controller.saveSnapshot(new File("snapshot.jps"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM