我到处寻找,找不到可靠的答案。根据文档,Java抛出 java.lang.StackOverflowError 错误在以下情况下:
I've looked everywhere and can't find a solid answer. According to the documentation, Java throws a java.lang.StackOverflowError error under the following circumstance:
当由于应用程序过于冗长而发生堆栈溢出时抛出。
Thrown when a stack overflow occurs because an application recurses too deeply.
但这引发了两个问题:
- 没有其他方法可以进行堆栈溢出发生,不仅仅是通过递归?
- StackOverflowError是否在JVM实际溢出堆栈之前或之后发生?
详细说明第二个问题:
当Java抛出StackOverflowError时,你能安全地假设堆栈没有写入堆中吗?如果你在一个抛出堆栈溢出的函数的try / catch中缩小堆栈或堆的大小,你能继续工作吗?这是在任何地方记录的吗?
When Java throws the StackOverflowError, can you safely assume that the stack did not write into the heap? If you shrink the size of the stack or heap in a try/catch on a function that throws a stack overflow, can you continue working? Is this documented anywhere?
答案我不是要找:
- A StackOverflow因错误的递归而发生。
- 当堆遇到堆栈时发生StackOverflow。
看来你正在考虑 stackoverflow错误就像本机程序中的缓冲区溢出异常,当存在写入尚未为缓冲区分配的内存的风险时,从而破坏其他一些内存位置。根本不是这样的。
It seems you're thinking that a stackoverflow error is like a buffer overflow exception in native programs, when there is a risk of writing into memory that had not been allocated for the buffer, and thus to corrupt some other memory locations. It's not the case at all.
JVM为每个线程的每个堆栈分配了一个给定的内存,如果尝试调用方法恰好填满了这个内存,JVM抛出错误。就像你试图在长度为N的数组的索引N处写一样。没有内存损坏可能发生。堆栈无法写入堆。
JVM has a given memory allocated for each stack of each thread, and if an attempt to call a method happens to fill this memory, JVM throws an error. Just like it would do if you were trying to write at index N of an array of length N. No memory corruption can happen. The stack can not write into the heap.
StackOverflowError是堆栈OutOfMemoryError对堆的唯一信号:它只是表示没有更多可用内存。
A StackOverflowError is to the stack what an OutOfMemoryError is to the heap: it simply signals that there is no more memory available.
StackOverflowError : Java虚拟机实现已经耗尽了线程的堆栈空间,通常是因为执行程序中的错误导致线程执行无限数量的递归调用。
StackOverflowError: The Java Virtual Machine implementation has run out of stack space for a thread, typically because the thread is doing an unbounded number of recursive invocations as a result of a fault in the executing program.
更多推荐
实际导致Stack Overflow错误的原因是什么?
发布评论