JVM StackOverflowError vs. OutOfMemoryError
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
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 StackOverflowError vs. OutOfMemoryError的更多相关文章
- Java面试题:写代码使得分别出现StackOverflowError和OutOfMemoryError
转载自:http://www.cnblogs.com/xudong-bupt/p/3360206.html 今天做了个笔试,这是其中的一道题目:写代码使得分别出现StackOverflowError和 ...
- java stackoverflowerror与outofmemoryerror区别(转)
1.stackoverflow: 每当java程序启动一个新的线程时,java虚拟机会为他分配一个栈,java栈以帧为单位保持线程运行状态:当线程调用一个方法是,jvm压入一个新的栈帧到这个线程的栈中 ...
- java stackoverflowerror与outofmemoryerror区别
1.stackoverflow: 每当java程序启动一个新的线程时,java虚拟机会为他分配一个栈,java栈以帧为单位保持线程运行状态:当线程调用一个方法是,jvm压入一个新的栈帧到这个线程的栈中 ...
- StackOverflowError 和 OutOfMemoryError
package cn.zno.outofmomery; import java.util.ArrayList; import java.util.List; public class Test { v ...
- 读书笔记<深入理解JVM>01 关于OutOfMemoryError 堆空间的溢出
代码片段如下: package com.gosaint.shiro; import java.util.ArrayList; import java.util.List; public class H ...
- JVM—内存溢出、OutOfMemoryError、StackOverflowError
学习jvm时看到几篇非常好的系列文章,转载了: <深入理解Java虚拟机>学习小记一之自动内存管理机制(一) http://my.oschina.net/linuxfelix/blog/1 ...
- JVM的参数设置与OutOfMemoryError异常关系
Java堆中存放Object对象数据,例如new出来的Object.当没有任何引用指向某对象时,该对象可能被垃圾回收.有关垃圾回收算法,可参考其他有关文章,网上很多.关于对象引用,按强弱还有强引用,软 ...
- JVM异常之:栈溢出StackOverflowError
在java虚拟机规范中,虚拟机栈和本地方法栈都会出现StackOverflowError和OutofMemoryError,程序计数器是java虚拟机中唯一一块不会产生error的内存区域. 一.St ...
- StackOverflowError&OutOfMemoryError区别
在Java虚拟机规范中,针对内存分配规定两种异常状况,即StackOverflowError和OutOfMemoryError. StackOverflowError:当线程请求的内存大小大于所配置的 ...
随机推荐
- java timer 指定某时间点执行
package com.northeasttycoon.service; import java.util.Calendar;import java.util.Timer;import java.ut ...
- iOS:苹果企业证书通过网页分发安装app
本文转载至 http://blog.sina.com.cn/s/blog_6afb7d800101fa16.html 苹果的企业级证书发布的应用,是不用设备授权即可直接安装,并且不限设备上限.为了方便 ...
- centos开放80端口
增加一条规则 #/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT 将更改进行保存 /etc/rc.d/init.d/iptables save 查 ...
- linux c编程:文件的读写
Linux系统中提供了系统调用函数open()和close()用于打开和关闭一个存在的文件 int open(const char *pathname,int flags) int open(cons ...
- LeetCode 017 4Sum
[题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- Eclipse使用方法和技巧二十六:浅谈快捷键
网络上到处都是eclipse有哪些经常使用的快捷键,当中还有非常多讲得着实不错.这里就不再狗尾续貂而是谈谈别的这段时间的一些思考.近期增加了开发团队.代码量突突的上去了.同一时候也发现 ...
- ABAP-创建信息记录
CALL FUNCTION 'ME_INITIALIZE_INFORECORD'. CALL FUNCTION 'ME_DIRECT_INPUT_INFORECORD' *&--------- ...
- Java for LeetCode 115 Distinct Subsequences【HARD】
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 0x01
随便记录点想法什么的, 这个博客的编辑界面挺简陋的...
- 机器学习 F1-Score, recall, precision
在机器学习,模式识别中,我们做分类的时候,会用到一些指标来评判算法的优劣,最常用的就是识别率,简单来说,就是 Acc=Npre/Ntotal 这里的 Npre表示预测对的样本数,Ntotal表示测试集 ...