一 代码
import java.util.ArrayList;
import java.util.Random;
/**
* -Xms600m -Xmx600m
*
*/
public class OOMTest1 {
public static void main(String[] args) {
ArrayList<Picture> list = new ArrayList<>();
while (true) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
list.add(new Picture(new Random().nextInt(1024 * 1024)));
}
}
}
class Picture {
private byte[] pixels;
public Picture(int length) {
this.pixels = new byte[length];
}
}
二 测试
1 用分析工具进行分析,观察内存变化情况
2 内存在不断发生变化
3 堆内存溢出
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at com.atguigu.java.chapter08.Picture.<init>(OOMTest1.java:28)
at com.atguigu.java.chapter08.OOMTest1.main(OOMTest1.java:19)