简体   繁体   English

是否可以使用Java为Java Profiler拍摄快照?

[英]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? 我想定期为运行时制作快照(用于Java探查器的.snapshot文件,如YourKit,JProfiler,VisualVM等),是否可以通过调用方法或其他方法来制作快照? With Java, running on the same jvm? 使用Java,可以在同一个jvm上运行吗?

I would suggest periodically running a Java Flight Recorder. 我建议定期运行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 : 请注意, 您要分析的应用程序需要这些额外的JVM参数

java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder

And finally for a periodic scheduling you need a command that you can use from crontab. 最后,对于定期计划,您需要可以在crontab中使用的命令。 For that you can use the example command from the previous link: 为此,您可以使用上一个链接中的example命令:

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

Where "5368" is the PID of your profiled application and the rest is self-explanatory. 其中“ 5368”是您已概要分析的应用程序的PID,其余内容是不言自明的。

Each profiler will have a different mechanism for periodically saving snapshots. 每个探查器将具有不同的机制来定期保存快照。

For JProfiler , use a "Timer" trigger with a "Save snapshot" action. 对于JProfiler ,请使用带有“保存快照”操作的“计时器”触发器。 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: 如果您想以编程方式控制来自同一JVM的记录和保存快照,请使用Controller API,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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