package com.atul;
public class StackOverFlow {
public StackOverFlow() {
callStackOverFlow();
}
public void callStackOverFlow() {
StackOverFlow st = new StackOverFlow();
}
public static void main(String[] args) {
StackOverFlow st2 = new StackOverFlow();
}
}
在上面的程序中,我试图得到 OutOfMemory 错误,但我得到了 StackOverFlow 错误。据我所知,所有对象都是在堆中创建的。在这里,我们正在使用构造函数进行递归,但我仍然收到 StackOverFlow 错误。
为什么?
最佳答案
在您用完堆内存之前很久,您就用完了堆栈(对于简单情况,堆栈的最大深度约为 10,000)。这是因为每个线程都有自己的堆栈,所以它肯定比共享堆小很多。
如果你想用完内存,你需要更快地用完堆。
public class OutOfMemoryMain {
byte[] bytes = new byte[100*1024*1024];
OutOfMemoryMain main = new OutOfMemoryMain();
public static void main(String... args) {
new OutOfMemoryMain();
}
}
关于java - 我期待 outOfMemory 但在这里我在 java 中得到 stackOverFlow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12686785/