生成转储文件:
- 使用
-XX:+HeapDumpOnOutOfMemoryError参数
/**
* -Xms10M
* -Xmx10M
* -XX:+HeapDumpOnOutOfMemoryError
*/
public class HeapOOM {
public static void main(String[] args) {
List<OOMObject> list = new ArrayList<>();
while (true) {
list.add(new OOMObject());
}
}
static class OOMObject {
}
}
结果:

image.png
分析:
- 使用 jhat打开
jhat java_pid10652.hprof

image.png
在浏览器中打开:http://localhost:7000/

image.png
主要关心的是:show instance counts for all classes 和 show heap histogram
如果需要更详细的查询,可以通过 Execute Object Query Language(OQL),具体的 OQL 语法和使用请 google
-
show instance counts for all classes
image.png -
show heap histogram
image.png -
使用 jvisualvm
打开堆转储快照文件
image.png

image.png


