티스토리 뷰



Stackoverflow 를 둘러보던 중, 

아래와 같은 글을 발견했다.



Java 에서 try-catch 로 OutOfMemoryError 를 잡을 수 있나??

아니 어떻게 OS 단에서 컨트롤 하는 메모리에러를 잡지? 라고 생각하며 스크롤을 내렸는데..

잡을수 있다??


안드로이드로 테스트를 한번 해보기로 했다..




1. OutOfMemory Generator 






2. 실행


TestModel tm = new TestModel();

        try {

            tm.generateOOM();

        } catch (OutOfMemoryError e) {

            android.util.Log.d(null, "==================> " + e.toString());

        }




3. 결과

01-05 15:11:09.693 11900-11900/? D/Leeyoungwoo1: =================> OOM test started..

01-05 15:11:09.693 11900-11900/? D/Leeyoungwoo1: Iteration 1 Free Mem: 430730

01-05 15:11:09.693 11900-11900/? D/Leeyoungwoo1: Required Memory for next loop: 100

01-05 15:11:10.693 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Iteration 2 Free Mem: 410378

01-05 15:11:10.693 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Required Memory for next loop: 500

01-05 15:11:11.693 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Iteration 3 Free Mem: 409962

01-05 15:11:11.693 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Required Memory for next loop: 2500

01-05 15:11:12.694 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Iteration 4 Free Mem: 407754

01-05 15:11:12.694 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Required Memory for next loop: 12500

01-05 15:11:13.695 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Iteration 5 Free Mem: 395306

01-05 15:11:13.699 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Required Memory for next loop: 62500

01-05 15:11:14.700 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Iteration 6 Free Mem: 341898

01-05 15:11:14.703 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Required Memory for next loop: 312500

01-05 15:11:15.705 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Iteration 7 Free Mem: 2407306

01-05 15:11:15.707 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Required Memory for next loop: 1562500

01-05 15:11:16.708 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Iteration 8 Free Mem: 1150506

01-05 15:11:16.709 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Required Memory for next loop: 7812500

01-05 15:11:17.710 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Iteration 9 Free Mem: 6406032

01-05 15:11:17.713 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Required Memory for next loop: 39062500

01-05 15:11:18.713 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Iteration 10 Free Mem: 16758488

01-05 15:11:18.714 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Required Memory for next loop: 195312500

01-05 15:11:19.714 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: Iteration 11 Free Mem: 16746560

01-05 15:11:19.889 11900-11900/jcompia.com.mvppatterntest D/Leeyoungwoo1: ==================> java.lang.OutOfMemoryError: Failed to allocate a 781250012 byte allocation with 16777216 free bytes and 39MB until OOM


헐.....????



catch 안에 OOM도 잡히고..

앱도 안튕김......



결론 : Java OOM 은 Try-catch 로 잡을 수 있다.





공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함