如何在Java中查看堆中的内容?

时间:2021-05-20 20:44:45

I've managed to get a memory 'leak' in a java application I'm developing. When running my JUnit test suite I randomly get out of memory exceptions (java.lang.OutOfMemoryError).

我设法在我正在开发的java应用程序中获得内存“泄漏”。运行我的JUnit测试套件时,我会随机出现内存异常(java.lang.OutOfMemoryError)。

What tools can I use to examine the heap of my java application to see what's using up all my heap so that I can work out what's keeping references to objects which should be able to be garbage collected.

我可以使用哪些工具来检查我的java应用程序的堆,以查看我的所有堆的使用情况,以便我可以找出保留对应该能够被垃圾收集的对象的引用。

8 个解决方案

#1


24  

VisualVM is included in the most recent releases of Java. You can use this to create a heap dump, and look at the objects in it.

VisualVM包含在最新版本的Java中。您可以使用它来创建堆转储,并查看其中的对象。

Alternatively, you can also create a heapdump commandine using jmap (in your jdk/bin dir):

或者,您也可以使用jmap(在jdk / bin目录中)创建一个heapdump命令:

jmap -dump:format=b,file=heap.bin <pid>

You can even use this to get a quick histogram of all objects

您甚至可以使用它来获得所有对象的快速直方图

jmap -histo <pid>

I can recommend Eclipse Memory Analyzer (http://eclipse.org/mat) for advanced analysis of heap dumps. It lets you find out exactly why a certain object or set of objects is alive. Here's a blog entry showing you what Memory Analyzer can do: http://dev.eclipse.org/blogs/memoryanalyzer/2008/05/27/automated-heap-dump-analysis-finding-memory-leaks-with-one-click/

我可以推荐Eclipse Memory Analyzer(http://eclipse.org/mat)来进行堆转储的高级分析。它可以让您找出某个对象或一组对象的确切原因。这是一篇博客文章,向您展示了Memory Analyzer可以做什么:http://dev.eclipse.org/blogs/memoryanalyzer/2008/05/27/automated-heap-dump-analysis-finding-memory-leaks-with-one-点击/

#2


6  

If you need something free, try VisualVM

如果您需要免费的东西,请尝试使用VisualVM

From the project's description:

从项目的描述:

VisualVM is a visual tool integrating commandline JDK tools and lightweight profiling capabilities. Designed for both development and production time use.

VisualVM是一个集成命令行JDK工具和轻量级分析功能的可视化工具。专为开发和生产时间使用而设计。

#3


3  

This is a pretty old question. A lot of people might have started using IntelliJ since it was originally answered. IntelliJ has a plugin that can show memory usage called JVM Debugger Memory View.

这是一个非常古老的问题。很多人可能已经开始使用IntelliJ,因为它最初的答案。 IntelliJ有一个插件,可以显示称为JVM调试器内存视图的内存使用情况。

#4


1  

Use a profiler like JProfiler or YourKitProfiler

使用JProfiler或YourKitProfiler等分析器

#5


1  

JProfiler worked very well for me....

JProfiler对我来说非常好....

http://www.ej-technologies.com/products/jprofiler/overview.html

#6


1  

Use the Eclipse Memory Analyzer

使用Eclipse Memory Analyzer

There's no other tool that I'm aware of any tool that comes close to it's functionality and performance and price (free and open source) when analysing heap dumps.

在分析堆转储时,没有其他工具可以让我知道任何接近它的功能,性能和价格(免费和开源)的工具。

#7


0  

If you're using a system which supports GTK you could try using JMP.

如果您使用的是支持GTK的系统,则可以尝试使用JMP。

#8


0  

You can try the Memory Leak Detector that is part of the JRockit Mission Control tools suite. It allows you to inspect the heap while the JVM is running. You don't need to take snapshots all the time. You can just connect online to the JVM and then see how the heap changes between garbage collections. You can also inspect objects, follow references graphically and get stack traces from where your application is currently allocating objects. Here is a brief introduction.

您可以尝试作为JRockit Mission Control工具套件一部分的Memory Leak Detector。它允许您在JVM运行时检查堆。您不需要一直拍摄快照。您可以在线连接到JVM,然后查看堆如何在垃圾收集之间进行更改。您还可以检查对象,以图形方式跟踪引用,并从应用程序当前分配对象的位置获取堆栈跟踪。这是一个简短的介绍。

The tool is free to use for development and you can download it here.

该工具可以免费用于开发,您可以在此处下载。

#1


24  

VisualVM is included in the most recent releases of Java. You can use this to create a heap dump, and look at the objects in it.

VisualVM包含在最新版本的Java中。您可以使用它来创建堆转储,并查看其中的对象。

Alternatively, you can also create a heapdump commandine using jmap (in your jdk/bin dir):

或者,您也可以使用jmap(在jdk / bin目录中)创建一个heapdump命令:

jmap -dump:format=b,file=heap.bin <pid>

You can even use this to get a quick histogram of all objects

您甚至可以使用它来获得所有对象的快速直方图

jmap -histo <pid>

I can recommend Eclipse Memory Analyzer (http://eclipse.org/mat) for advanced analysis of heap dumps. It lets you find out exactly why a certain object or set of objects is alive. Here's a blog entry showing you what Memory Analyzer can do: http://dev.eclipse.org/blogs/memoryanalyzer/2008/05/27/automated-heap-dump-analysis-finding-memory-leaks-with-one-click/

我可以推荐Eclipse Memory Analyzer(http://eclipse.org/mat)来进行堆转储的高级分析。它可以让您找出某个对象或一组对象的确切原因。这是一篇博客文章,向您展示了Memory Analyzer可以做什么:http://dev.eclipse.org/blogs/memoryanalyzer/2008/05/27/automated-heap-dump-analysis-finding-memory-leaks-with-one-点击/

#2


6  

If you need something free, try VisualVM

如果您需要免费的东西,请尝试使用VisualVM

From the project's description:

从项目的描述:

VisualVM is a visual tool integrating commandline JDK tools and lightweight profiling capabilities. Designed for both development and production time use.

VisualVM是一个集成命令行JDK工具和轻量级分析功能的可视化工具。专为开发和生产时间使用而设计。

#3


3  

This is a pretty old question. A lot of people might have started using IntelliJ since it was originally answered. IntelliJ has a plugin that can show memory usage called JVM Debugger Memory View.

这是一个非常古老的问题。很多人可能已经开始使用IntelliJ,因为它最初的答案。 IntelliJ有一个插件,可以显示称为JVM调试器内存视图的内存使用情况。

#4


1  

Use a profiler like JProfiler or YourKitProfiler

使用JProfiler或YourKitProfiler等分析器

#5


1  

JProfiler worked very well for me....

JProfiler对我来说非常好....

http://www.ej-technologies.com/products/jprofiler/overview.html

#6


1  

Use the Eclipse Memory Analyzer

使用Eclipse Memory Analyzer

There's no other tool that I'm aware of any tool that comes close to it's functionality and performance and price (free and open source) when analysing heap dumps.

在分析堆转储时,没有其他工具可以让我知道任何接近它的功能,性能和价格(免费和开源)的工具。

#7


0  

If you're using a system which supports GTK you could try using JMP.

如果您使用的是支持GTK的系统,则可以尝试使用JMP。

#8


0  

You can try the Memory Leak Detector that is part of the JRockit Mission Control tools suite. It allows you to inspect the heap while the JVM is running. You don't need to take snapshots all the time. You can just connect online to the JVM and then see how the heap changes between garbage collections. You can also inspect objects, follow references graphically and get stack traces from where your application is currently allocating objects. Here is a brief introduction.

您可以尝试作为JRockit Mission Control工具套件一部分的Memory Leak Detector。它允许您在JVM运行时检查堆。您不需要一直拍摄快照。您可以在线连接到JVM,然后查看堆如何在垃圾收集之间进行更改。您还可以检查对象,以图形方式跟踪引用,并从应用程序当前分配对象的位置获取堆栈跟踪。这是一个简短的介绍。

The tool is free to use for development and you can download it here.

该工具可以免费用于开发,您可以在此处下载。