在java虚拟机规范中,虚拟机栈和本地方法栈都会出现StackOverflowError和OutofMemoryError,程序计数器是java虚拟机中唯一一块不会产生error的内存区域。

一、StackOverflowError(栈溢出)

StackOverflowError代表的是,当栈深度超过虚拟机分配给线程的栈大小时就会出现此error。

在eclipse中增加jvm参数见《eclipse调试时增加jvm参数

示例1:

package com.dxz.jvm;

/**
* @Described:栈层级不足
* @VM args:-Xss128k
*/
public class StackOverFlow {
private int i; public void plus() {
i++;
plus();
} public static void main(String[] args) {
StackOverFlow stackOverFlow = new StackOverFlow();
try {
stackOverFlow.plus();
} catch (Error e) {
System.out.println("Error:stack length:" + stackOverFlow.i);
e.printStackTrace();
}
}
}

-vm args-Xss128k  :说明后面是VM的参数,所以后面的其实都是JVM的参数了

结果:

Error:stack length:997
java.lang.StackOverflowError
at com.dxz.jvm.StackOverFlow.plus(StackOverFlow.java:11)

示例2:

package com.dxz.jvm;

/**
* @Described:递归Constructer
* @VM args:-Xss128k
*/
public class StackOverFlow2 { public class OneObject {
OneObject oneObject = new OneObject();
} public static void main(String[] args) {
StackOverFlow2 stackOverFlow2 = new StackOverFlow2();
try {
OneObject oneObject = stackOverFlow2.new OneObject();
} catch (Exception e) {
e.printStackTrace();
}
}
}

结果:

Exception in thread "main" java.lang.StackOverflowError
at com.dxz.jvm.StackOverFlow2$OneObject.<init>(StackOverFlow2.java:10)

反编译代码:

package com.dxz.jvm;

public class StackOverFlow2
{
public static void main(String[] args)
{
StackOverFlow2 stackOverFlow2 = new StackOverFlow2();
try
{
StackOverFlow2 tmp13_12 = stackOverFlow2; tmp13_12.getClass(); OneObject localOneObject = new OneObject();
} catch (Exception e) {
e.printStackTrace();
}
} public class OneObject
{
OneObject oneObject = new OneObject(StackOverFlow2.this); public OneObject()
{
}
}
}

看下命令行:

说明:在这里  constructer 中是调用 init , 而 static 是调用 cinit , 固我们如果将自己的对象放入到 static 中是不会造成递归的, 而如果将自己本身放到 constructer 中他就会不断的调用 init ,递归并不是马上返回,而是一层一层的保存在Stack里边,满足结束条件后才一层一层的返回。

当Stack满了就抛出error了。 所以才发生了上面的java.lang.StackOverflowError 溢出错误。
注意:是递归的错误,才出现Stack满的情况,而无限循环一般不会占用更多的内存或者具体的Stack,只是占cpu而已,所以不会抛此错误。

JVM异常之:栈溢出StackOverflowError的更多相关文章

  1. 记一次jvm异常排查及优化

    为方便自己查看,根据工作遇到的问题,转载并整理以下jvm优化内容 有次接到客服反馈,生产系统异常,无法访问.接到通知紧急上后台跟踪,查看了数据库死锁情况--正常,接着查看tomcat 内存溢出--正常 ...

  2. 异常如果一直被throws抛出的话就会被jvm异常处理器处理了

    异常如果一直被throws抛出的话就会被jvm异常处理器处理了,这时jvm会跳出正常运行状态. 异常如果一直被throws抛出的话就会被jvm异常处理器处理了,这时jvm会跳出正常运行状态. 异常如果 ...

  3. JVM--你常见的jvm 异常有哪些? 代码演示:StackOverflowError , utOfMemoryError: Java heap space , OutOfMemoryError: GC overhead limit exceeded, Direct buffer memory, Unable_to_create_new_native_Thread, Metaspace

    直接上代码: public class Test001 { public static void main(String[] args) { //java.lang.StackOverflowErro ...

  4. JVM中OutOFMemory和StackOverflowError异常代码

    1.Out of Memory 异常 右键Run As --->Run Configuration 设置JVM参数 -Xms20m -Xmx20m 上代码: /** * VM Args:-Xms ...

  5. 小心sae的jvm异常导致的Error 404 – Not Found.No context on this server matched or handled this request.

    本来用着sae好好的,结果第二天部署的应用突然不好使了,各种Error 404 – Not Found.No context on this server matched or handled thi ...

  6. JVM异常之:方法区溢出OutOfMemoryError: PermGen space

    1.方法区溢出(Perm持久代溢出) 在jdk1.6及之前的版本中,常量池放在Perm区也即是方法区中,所以在jdk1.6版本中,常量池溢出可以说是方法区溢出. 示例一: 方法区溢出的示例见<J ...

  7. JVM异常之:堆溢出OutofMemoryError

    1.堆溢出 Java 堆内存的OutOfMemoryError异常是实际应用中最常见的内存溢出异常情况.出现Java 堆内存溢出时,异常堆栈信息“java.lang.OutOfMemoryError” ...

  8. 在k8s中收集jvm异常dump文件到OSS

    现状 加参数 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=logs/test.dump 可以实现在jvm发生内存错误后 会生成dump文件 方便开 ...

  9. JVM异常之:直接内存溢出

    示例: package com.dxz.jvm; import java.lang.reflect.Field; import sun.misc.Unsafe; /** * @Described:直接 ...

随机推荐

  1. set 基础知识

    #include <iostream> #include <set> using namespace std; int main() { set<int> s; s ...

  2. java exception 01

    问题:java.util.concurrentmodificationexception 背景:java thread 网上找到的出现的例子如下(项目中真实的code不便给出) public clas ...

  3. 打开网页直接弹出qq对话框?

    代码一: http://wpa.qq.com/msgrd?v=3&uin=此处输入QQ号&site=qq&menu=yes 代码二: <iframe src=" ...

  4. ng-repeat的用法:

    -------------------------------------转载: 遍历数组:    <li ng-repeat="item in array">{{it ...

  5. Go Example--协程

    package main import "fmt" func main() { //main gorouting中调用f函数 f("direct") //重新建 ...

  6. LeetCode – All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  7. codeforces 788A Functions again

    …… 原题: Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandia ...

  8. day28Spark

    PS:因为Spark是用内存运行 的,非常快 PS: 1.下面就是将conf的spark-env.template改变成spark-env.sh,并添加红色部分 2.修改slaves文件添加从设备 启 ...

  9. shell excute mongo query command

    use shell command method one: #!/bin/bash ] then echo 'Please input cid' exit fi HOST= mongo ${HOST} ...

  10. 谈谈 SOA

    为什么要 讨论 SOA 呢 ? 请参考我写的另一篇文章 <论 微服务 和 Entity Framework 对 数据 的 割裂>    https://www.cnblogs.com/KS ...