我经常使用 VisualVM 分析正在运行的 Java 应用程序,但它需要 X 才能在计算机上运行。
我知道我可以通过管理端口进行连接,但这将是离线采样分析,这对我来说还不够。
因此,我正在寻找一种解决方案,通过该解决方案,我可以从命令行分析正在运行的 Java 应用程序的方法的 CPU 使用情况。对我来说,在服务器上收集数据就足够了,然后可以在不同的机器上分析收集到的数据。
更新:
看来我需要更具体。我想从命令行分析正在运行的 Java 应用程序,我不想停止它并重新运行它。
I profile running Java applications often with VisualVM but it needs X to run on the machine.
I know I can connect through management port but that will be an offline sampled profiling which is not enough for me.
So I'm looking for a solution with which I can profile the CPU usage of the methods of a running Java application from command-line. It's enough for me to collect data on the server and then the collected data can be analyzed on a different machine.
Update:
It seems I need to be more specific. I want to profile a running Java application from command line, I don't want to stop it and rerun it.
发布评论
评论(7)
jvmtop 应用程序是一个方便的工具,用于从命令行。无需停止 jvm。用法:
将为您提供这样的输出,该输出将在应用程序运行时更新:
优点是它不使用仪器。待分析的 jvm 的类不会改变。
如果您正在寻找更直观的东西,请查看基于 jvmtop 的 jvm-mon
The jvmtop application is a convenient tool for profiling from the commandline. No need to stop the jvm. Usage:
Will give you output like this which will be updating while the app runs:
The advantage is that it does not take the use of instrumentation. The classes of the to-be-profiled jvm will not be altered.
If you are looking for something more visual then have a look at jvm-mon which is based on jvmtop
看起来从命令行分析 java 应用程序的“内置”方法是使用分析命令行参数启动它,如下所示
然后使用一些 GUI 工具(或 Web 服务器工具,如 < a href="https://stackoverflow.com/a/185926/32453">jhat) 或 命令行工具在进程退出后(并且当时创建文件)。
如果您使用https://stackoverflow.com/a/2344436/32453提到的“QUIT”信号技巧,那么您可以在不退出JVM的情况下随意生成文件(它似乎附加到之前的输出文件中)。或者等到进程退出,它就会生成文件。
该(内置)分析器很少进行采样,因此总体减速/影响通常较低。
参考:http://web.archive.org/web/20160623224137/https://thunderguy.com/semicolon/2004/04/18/profiling-a-java-program-easily/
您还可以通过收集大量 jstack 并将它们转储到 ex: a flamegraph 或其他一些分析器/聚合器...
Looks like the "built-in" way to profile a java app from the command line is to start it with profiling command line parameters, like this
Then examine the file "myprogram.hprof" with some GUI tool (or web server tool like jhat) or command line tool after the process exits (and the file is created at that time).
If you use the "QUIT" signal trick, mentioned https://stackoverflow.com/a/2344436/32453 then you can generate a file at will without exiting the JVM (it appears to append to the previous output file). Or wait until the process exits and it will generate the file.
This (built-in) profiler does a sample infrequently so typically low slowdown/impact overall.
ref: http://web.archive.org/web/20160623224137/https://thunderguy.com/semicolon/2004/04/18/profiling-a-java-program-easily/
You could also just do the "poor man's profiler" by collecting lots of jstacks and dumping them into ex: a flamegraph or some other analyzer/conglomerator...
您可以使用 jstack 收集 10 或 20 个堆栈样本吗?那么如果 Foo 是一个方法,那么它的总时间使用量就是包含它的样本的分数。其 CPU 使用率是那些不以 I/O 或系统调用终止的样本的比例。它的“自身时间”是其本身作为终点的样本的分数。
我不需要任何漂亮的东西。我要么在 IDE 下运行它并以这种方式收集它们,要么使用 jstack 之类的东西来快照正在运行的应用程序的堆栈。
这就是随机暂停 技术。
Can you collect 10 or 20 stack samples with jstack? Then if Foo is a method, its overall time usage is the fraction of samples containing it. Its CPU usage is the fraction of those samples that don't terminate in I/O or a system call. Its "self time" is the fraction of samples in which it itself is the terminus.
I don't need anything pretty. I either run it under the IDE and collect them that way, or use something like jstack that snapshots the stack of a running app.
That's the random-pause technique.
使用 https://github.com/jvm-profiling-tools 可以实现最精确的分析/async-profiler。
这是我从命令行安装和运行它的脚本:
async-profiler.sh
它假设应用程序在同一用户下运行,并且有一个由 jps 列出的 java 进程 PID。
分析持续时间为 60 秒。
无需修改应用程序的启动选项或重新启动应用程序。
用于检查转储的 GUI 内置于 IntelliJ IDEA Ultimate 中:https://www. jetbrains.com/help/idea/cpu-profiler.html。
The most precise profiling can be achieved with https://github.com/jvm-profiling-tools/async-profiler.
Here is my script to install and run it from command-line:
async-profiler.sh
It assumes that app is run under same user and there is a single java process PID to be listed by jps.
Profiling duration is 60 seconds.
No modification of app's startup options or app restart is needed.
GUI for examining dumps is built-in into IntelliJ IDEA Ultimate: https://www.jetbrains.com/help/idea/cpu-profiler.html.
我们在我们的服务器上使用了 hprof ,它绝对是比 sysouts 更好,以防您无法运行完整的 VisualVM 会话。
使用 hprof 的示例有很多:
We have used hprof on our servers and it definitely is better than sysouts in case you can't run a full fledged VisualVM session.
Examples of using hprof are plenty out there:
分析“已启动”JVM 的一种方法是聚合一段时间内的多个 jstack。
例如,您可以将它们解析并显示为 FlameGraph(请参阅该链接的各种答案中的详细信息,我不会在此处多余地包含它们)。
One way to profile an "already started" JVM is to aggregate multiple jstacks taken over time.
You can for instance parse and display them as a FlameGraph (see details at the various answers for that link, I won't redundantly include them here).
您可以远程运行大多数商业分析器,以便在服务器上运行代理,然后通过开发计算机上的客户端连接到该代理。我最喜欢的分析器是 JProfiler。这是相当合理的购买,并且非常稳定(并非所有商业分析器都是如此)。
http://www.ej-technologies.com/products/jprofiler/overview.html
其他稳定但不是我最喜欢的商业分析器是 YourKIT。
http://www.yourkit.com/
这些较小的供应商提供了很好的工具。这些工具将为您提供大量有关方法计时、内存使用、GC 等的信息。比 jconsole 多得多。
You can run most commercial profilers remotely so an agent is run on the server then connect to that agent through a client on your dev machine. My absolute favorite profiler is JProfiler. It's fairly reasonable purchase, and it's very stable (which not all commercial profilers that's true).
http://www.ej-technologies.com/products/jprofiler/overview.html
Other commercial profilers that are stable, but not my favorite are YourKIT.
http://www.yourkit.com/
Those smaller vendors make good tools. These tools will provide you tons of information about method timings, memory use, GC, etc. Much more than jconsole.