JVM StackOverflowError vs. OutOfMemoryError
2016-08-26 22:46
555 查看
if the computation in a thread needs a larger Java Virtual Machine stack than is permitted, the Java Virtual Machine throws a StackOverflowError;
if Java Virtual Machine stacks can be dynamically expanded, and expansion is attempted but insufficient memory can be made available to effect the expansion, or if insufficient memory can be made available to create the initial Java Virtual Memory stack for a new thread, the Java Virtual Machine throws an OutOfMemoryError.
Example of StackOverflowError: limit the size of a thread's stack size and the do a deep recursion
Example of OutOfMemoryError: limit -Xmx and create large objects (like using StringBuilder)
if Java Virtual Machine stacks can be dynamically expanded, and expansion is attempted but insufficient memory can be made available to effect the expansion, or if insufficient memory can be made available to create the initial Java Virtual Memory stack for a new thread, the Java Virtual Machine throws an OutOfMemoryError.
Example of StackOverflowError: limit the size of a thread's stack size and the do a deep recursion
public class Main { public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { { System.out.println(fact(1<<15)); } } private long fact(int n) { return n < 2 ? 1 : n * fact(n-1); } }, "thread", 1<<20).start(); } }
Example of OutOfMemoryError: limit -Xmx and create large objects (like using StringBuilder)
public class Main2 { public static void main(String[] args) { new Thread(null, () -> { { StringBuilder builder = new StringBuilder(); for (int cnt = 0; cnt < 100000000; cnt++) { builder.append(cnt); } try { Thread.sleep(15000000); } catch (InterruptedException e) { e.printStackTrace(); } } }, "thread", 1 << 20).start(); } }
相关文章推荐
- 调优JVM内存,并解决OutOfMemoryError,StackOverflowError等异常问题
- 调优JVM内存,并解决OutOfMemoryError,StackOverflowError等异常问题
- StackOverflowError spaceOutOfMemoryError 通过jvm调优解决(转自http://blog.sina.com.cn/s/blog_790229d301015vz)
- JVM—内存溢出、OutOfMemoryError、StackOverflowError
- 调优JVM内纯,并解决OutOfMemoryError,StackOverflowError等异常问题
- 调优JVM内存,并解决OutOfMemoryError,StackOverflowError等异常问题
- JVM学习之两种异常StackOverflowError和OutOfMemoryError产生的原因
- 调优JVM内存,并解决OutOfMemoryError,StackOverflowError等异常问题
- 调优JVM内存,并解决OutOfMemoryError,StackOverflowError等异常问题
- 调优JVM内存,并解决OutOfMemoryError,StackOverflowError等异常问题
- 调优JVM内存,并解决OutOfMemoryError,StackOverflowError等异常问题
- 调优JVM内存,并解决OutOfMemoryError,StackOverflowError等异常问题
- 调优JVM内存,并解决OutOfMemoryError,StackOverflowError等异常问题
- 调优JVM内存,并解决OutOfMemoryError,StackOverflowError等异常问题
- StackOverflowError&OutOfMemoryError区别
- 理解StackOverflowError与OutOfMemoryError
- JAVA内存学习1——玩一玩StackOverFlowError和OutOfMemoryError
- 服务器程序问题记录OutOfMemoryError And StackOverflowError
- java编程题:写代码实现一个StackOverFlowError和OutOfMemoryError
- Java中StackOverflowError与OutOfMemoryError详解