public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
oom();
// so();
}
ArrayList<String[]> list = new ArrayList<>();
private void oom() {
String[] arrays = new String[1000000000];
while (true) {
list.add(arrays);
}
}
private void so() {
while (true) {
so();
}
}
}
java.lang.OutOfMemoryError: Java heap space
at com.zhjy.my2048.ExampleUnitTest.oom(ExampleUnitTest.java:23)
at com.zhjy.my2048.ExampleUnitTest.addition_isCorrect(ExampleUnitTest.java:16)
OutOfMemoryErro发生这种问题的原因是java虚拟机创建的对象太多,在进行垃圾回收之间,虚拟机分配的到堆内存空间已经用满了。
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
// oom();
so();
}
ArrayList<String[]> list = new ArrayList<>();
private void oom() {
String[] arrays = new String[1000000000];
while (true) {
list.add(arrays);
}
}
private void so() {
while (true) {
so();
}
}
}
java.lang.StackOverflowError
at com.zhjy.my2048.ExampleUnitTest.so(ExampleUnitTest.java:32)
at com.zhjy.my2048.ExampleUnitTest.so(ExampleUnitTest.java:32)
at com.zhjy.my2048.ExampleUnitTest.so(ExampleUnitTest.java:32)
at com.zhjy.my2048.ExampleUnitTest.so(ExampleUnitTest.java:32)
at com.zhjy.my2048.ExampleUnitTest.so(ExampleUnitTest.java:32)
StackOverflowError是由于当前线程的栈满了 ,也就是函数调用层级过多导致。比如死递归。