在 Java 应用程序中分析内存使用情况的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/756873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:06:53  来源:igfitidea点击:

Best way to profile memory usage in a Java application?

javamemoryprofilingprofile

提问by Matt

I realize that similar questions have been asked about this before here on SO, but let me describe exactly what I need to do:

我意识到之前在 SO 上也有人问过类似的问题,但让我确切地描述一下我需要做什么:

I have a set of tests which run a command line java application and I'd like to add memory profiling to them. One option I see would be to add code (possibly using 3rd party tools/libraries) to my application that would provide a memory snapshot. Another option would be to use a third party tool which manages/instruments my application and the JVM for me (and ideally does not require me to change my code). I'm thinking of something like Valgrind but for Java. Also open source if at all possible.

我有一组运行命令行 java 应用程序的测试,我想向它们添加内存分析。我看到的一种选择是将代码(可能使用 3rd 方工具/库)添加到我的应用程序中,以提供内存快照。另一种选择是使用第三方工具来管理/检测我的应用程序和 JVM(理想情况下不需要我更改代码)。我正在考虑类似 Valgrind 的东西,但适用于 Java。如果可能的话,也开源。

What I'd really like to do is set up the memory tests so that my memory usage is being monitored at regular intervals, let's say every second, and dumped to a text file. That way I'd be able to see if the memory usage oscillates/increases/decreases over time. I'll also be able to calculate the max and min peaks.

我真正想做的是设置内存测试,以便定期监控我的内存使用情况,比如说每秒,并转储到文本文件中。这样我就可以看到内存使用量是否随着时间的推移而波动/增加/减少。我还可以计算最大和最小峰值。

Has anyone here done anything like this?

这里有人做过这样的事情吗?

Thanks in advance.

提前致谢。

采纳答案by Peter D

With something like JProfiler all you need to do is add certain parameters to the JVM. It uses JVMTI.

使用 JProfiler 之类的东西,您只需将某些参数添加到 JVM。它使用JVMTI。

I think you should be reading up on profilers and exactly what they can do for you. I also suggest reading up on JVMTI.

我认为您应该阅读分析器以及它们可以为您做什么。我还建议阅读 JVMTI。

The JVMTM Tool Interface (JVM TI) is a new native programming interface for use by tools. It provides both a way to inspect the state and to control the execution of applications running in the Java virtual machine (JVM). JVM TI supports the full breadth of tools that need access to JVM state, including but not limited to: profiling, debugging, monitoring, thread analysis, and coverage analysis tools.

JVMTM 工具接口 (JVM TI) 是一种供工具使用的新本机编程接口。它提供了一种检查状态和控制在 Java 虚拟机 (JVM) 中运行的应用程序的执行的方法。JVM TI 支持需要访问 JVM 状态的所有工具,包括但不限于:分析、调试、监控、线程分析和覆盖率分析工具。

Note: JVM TI replaces the Java Virtual Machine Profiler Interface (JVMPI) and the Java Virtual Machine Debug Interface (JVMDI). JVMPI and JVMDI will be removed in the next major release of J2SETM.

注意:JVM TI 替换了 Java 虚拟机分析器接口 (JVMPI) 和 Java 虚拟机调试接口 (JVMDI)。JVMPI 和 JVMDI 将在 J2SETM 的下一个主要版本中删除。

回答by Liran Orevi

Have you checked

你检查了吗

VisualVMand Eclipse-Callisto?

VisualVMEclipse-Callisto?

回答by Rad

Yourkitalso has a pretty good profiler

Yourkit也有一个很好的分析器

回答by kohlerm

Several profiler's such as yourkithave API's for tracing memory allocations. Another option here are monitoring tools such as jxinsightor glassboxor jamon

一些分析器(例如yourkit)具有用于跟踪内存分配的 API。这里的另一个选择是监控工具,例如jxinsightglassboxjamon

For analyzing heap dumps theEclipse Memory Analyzeris the best tool you can get. It's free and open source, so you can automate the analysis of heap dumps as much as you want.

对于分析堆转储,Eclipse Memory Analyzer是您可以获得的最佳工具。它是免费和开源的,因此您可以根据需要自动分析堆转储。

回答by uriDium

In addition to the above answers I also enjoyed using profiler a couple of years ago. Don't know if that helps.

除了上述答案,几年前我还喜欢使用分析器。不知道这是否有帮助。

回答by Thorbj?rn Ravn Andersen

A good place to start is to see if your JVM supports "java -Xrunhprof" as this can generate heap profiling information without making your scenario more complex.

一个好的起点是查看您的 JVM 是否支持“java -Xrunhprof”,因为这可以生成堆分析信息,而不会使您的场景更加复杂。

See http://docs.oracle.com/javase/7/docs/technotes/samples/hprof.html

请参阅http://docs.oracle.com/javase/7/docs/technotes/samples/hprof.html

You might find that enough for you for starting.

您可能会发现这足以让您开始。

回答by Matt Large

I develop in Eclipse but I have Netbeans around to use its excellent Profiler. It is limited compared to some commercial ones but still good enough for spotting most bottlenecks

我在 Eclipse 中开发,但我有 Netbeans 来使用其出色的 Profiler。与一些商业的相比,它是有限的,但仍然足以发现大多数瓶颈

回答by Kire Haglin

You can use jrcmdwhich is a command line utility that comes with the JRockit JVM. If you know the pid of the Java process you can just do:

您可以使用jrcmd,它是 JRockit JVM 附带的命令行实用程序。如果您知道 Java 进程的 pid,则可以执行以下操作:

JROCKIt_HOME\bin\jrcmd <pid> print_object_summary

and it will give you:

它会给你:

31.8% 3198k    41907   -137k [C
11.9% 1196k      300     +0k [B
11.4% 1151k    49118     +6k java/lang/String
 6.1% 612k     5604     +0k java/lang/Class
 4.3% 431k     2388     +0k [I
 3.5% 353k    15097     +0k java/util/HashMap$Entry
 ...

回答by Andre Santos

I do recommend dynaTrace production edition. Amazing tool to profile memory application. Low overhead and 100% of executed transactions.

我确实推荐 dynaTrace 生产版。用于分析内存应用程序的惊人工具。低开销和 100% 的已执行事务。