如何在Java中查看堆中的内容? [英] How can I see what is in my heap in Java?

查看:1080
本文介绍了如何在Java中查看堆中的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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应用程序的堆看看我的堆已经用完了什么,这样我就可以找出对应该能够被垃圾收集的对象的引用。

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.

推荐答案

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

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.

或者,您也可以使用jmap创建heapdump命令(在您的jdk / bin中) dir):

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

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

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

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

jmap -histo <pid>

我可以推荐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-一键/

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/

这篇关于如何在Java中查看堆中的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆