가끔씩 마주치게 되는 "OutOfMemoryError : bitmap size exceeds VM budget" 에러는 메모리 누수가 주요 원인이다.
이와 관련된 링크를 모아봤다.


* 액티비티가 멈출 때 비트맵을 재활용(즉 GC)되게 하라.
http://stackoverflow.com/questions/1949066/java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget-android
http://stackoverflow.com/questions/2191407/changing-imageview-content-causes-outofmemoryerror

- bitmap 이미지인 경우 recycle() 호출
- onPause에서 수행하는게 좋음

- ((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();


* 이미지를 미리 줄여서 읽어들여라
http://chiyo85.tistory.com/entry/Android-Bitmap-Object-Resizing-Tip
http://www.memofy.com/memofy/show/1008ab7f2836ab7f01071c2dbfe138/outofmemory-exception-when-decoding-with-bitmapfactory

- BitmapFactory.Options.inSampleSize 활용


* Activity Context에 대한 참조(reference)를 오랫동안 유지하지 말아라
http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html
(한글 번역 : http://blog.naver.com/huewu/110082062273 )

- Drawable.setCallback(null) 사용
- WeakReference를 가진 static 내부 클래스
- 이미지를 static 변수로 처리하지 말것.


* 외부 클래스의 상태에 의존하지 않는 내부 클래스는 static으로 선언하라
- 내부 클래스는 외부 클래스를 크게 만들며 또한 외부클래스 객체가 필요 이상으로 오래 살아있게 되어 메모리를
더 차지할 수 있다.
- 외부 클래스의 상태 필드에 접근하지 않는(즉, 외부 객체의 상태에 의존하지 않는) 내부 클래스는 static으로 선언.


* Attacking memory problems on Android
http://ttlnews.blogspot.com/2010/01/attacking-memory-problems-on-android.html

2011/12/06 11:27 2011/12/06 11:27

Trackback Address :: https://youngsam.net/trackback/1683