OOM内存溢出java代码实例1

代码如下:

 1 package com.github.mayangbo666.test;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 public class OOM1 {
 7     public static void main(String[] args) {
 8         List<Byte[]> list = new ArrayList<>();
 9         int i = 0;
10         try {
11             while (true) {
12                 list.add(new Byte[1024 * 1024]);//每次1M
13                 i++;
14             }
15         } catch (Throwable throwable) {
16             throwable.printStackTrace();
17             System.out.println("次数: " + i);
18         }
19     }
20 }

执行结果图:

 堆(heap)与栈(stack)

可以看到这里是堆内存溢出

其实也有栈内存溢出

 ...

原文地址:https://www.cnblogs.com/m-yb/p/11576006.html