1.先准备一个可以出现内存溢出的代码
package test;
import java.util.ArrayList;
public class TestOOMA {
static class OOMObject{
private String content;
public OOMObject(String content){
this.content = content;
}
}
public static void main(String[] args) throws InterruptedException {
ArrayList<OOMObject> list = new ArrayList<OOMObject>();
Thread.sleep(30000);
int a =1;
while(true){
list.add(new OOMObject(String.valueOf(a++)));
}
}
}
2.添加配置
-Xms2m -Xmx4m -XX:+HeapDumpOnOutOfMemoryError
3.内存泄漏,堆栈信息被导入到执行文件
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid14584.hprof ...
Heap dump file created [7162080 bytes in 0.088 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.lang.Integer.toString(Integer.java:401)
at java.lang.String.valueOf(String.java:3099)
at test.TestOOMA.main(TestOOMA.java:23)
4.将java_pid14584.hprof文件导入到jvisualvm
图片.png
5.查看内存溢出线程
图片.png 图片.png 图片.png
网友评论