如何在 Java 中查看堆中的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/145922/
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
How can I see what is in my heap in Java?
提问by Free Wildebeest
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 应用程序的堆,以查看哪些内容占用了我的所有堆,以便我可以确定哪些内容保留了对应该能够被垃圾收集的对象的引用。
回答by Tom
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) 对堆转储进行高级分析。它可以让您确切地找出某个对象或一组对象存在的确切原因。这是一个博客条目,向您展示了内存分析器可以做什么:http: //dev.eclipse.org/blogs/memoryanalyzer/2008/05/27/automated-heap-dump-analysis-finding-memory-leaks-with-one-点击/
回答by André
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 工具和轻量级分析功能的可视化工具。专为开发和生产时间使用而设计。
回答by mkasberg
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 Debugger Memory View。
回答by kohlerm
Use the 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.
在分析堆转储时,没有其他工具可以接近它的功能、性能和价格(免费和开源)。
回答by skaffman
Use a profiler like JProfileror YourKitProfiler
使用像JProfiler或YourKitProfiler这样的分析器
回答by cagcowboy
JProfiler worked very well for me....
JProfiler 对我来说效果很好......
http://www.ej-technologies.com/products/jprofiler/overview.html
http://www.ej-technologies.com/products/jprofiler/overview.html
回答by Free Wildebeest
回答by Kire Haglin
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.
该工具可免费用于开发,您可以在此处下载。

