堆栈溢出以及finally测试

  1. 堆溢出:
    在jvm运行java程序时,如果程序运行所需要的内存大于系统的堆最大内存(-Xmx),就会出现堆溢出问题。
  2. 栈溢出:
    线程请求的栈深度大于虚拟机允许的最大深度 StackOverflowError
    虚拟机在扩展栈深度时,无法申请到足够的内存空间 OutOfMemoryError
/**
 * 返回结果1
 * finally测试
 * @return
 */
public int print() {
    
    
    try {
    
    
        System.exit(0);
    } catch (Exception e) {
    
    
        e.printStackTrace();
    } finally {
    
    
        return 1;
    }
}

/**
 * 堆溢出
 * java.lang.OutOfMemoryError: Java heap space
 */
public void headOverFlow() {
    
    
    List<Object> list = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
    
    
        Byte[] bytes = new Byte[1 * 1024 * 1024];
        list.add(bytes);
    }
}

/**
 * 递归没有出口时栈溢出
 * java.lang.StackOverflowError
 * @param count
 */
public void stackOverFlow(int count) {
    
    
    stackOverFlow(count++);
}

猜你喜欢

转载自blog.csdn.net/for62/article/details/108091478
今日推荐