代碼以下:java
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 }
執行結果圖:git
堆(heap)與棧(stack)github
能夠看到這裏是堆內存溢出spa
其實也有棧內存溢出code
...blog