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)

可以看到這里是堆內存溢出

其實也有棧內存溢出

 ...

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM