NathanPB
NathanPB

Reputation: 745

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?

Upvotes: 1

Views: 1431

Answers (2)

Ingo Kegel
Ingo Kegel

Reputation: 48090

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)

enter image description here

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"));

Upvotes: 1

Gergely Bacso
Gergely Bacso

Reputation: 14661

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.

Upvotes: 1

Related Questions