OOM java code memory overflow Example 1

code show as below:

 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 }

Figure execution results:

 Heap (heap) and Stack (stack)

Here you can see a heap overflow

In fact, there are a stack memory overflow

 ...

 

Guess you like

Origin www.cnblogs.com/m-yb/p/11576006.html