메모리관련해서 찾은정보들 대충 정리해놓은것 올립니다.
추가사항들 있으면 댓글 달아주세요.
링크에 연결된 모든 분들께 감사를...

 

힙메모리 정보 코드 

android.util.Log.d("TAG","TOTAL MEMORY : "+(Runtime.getRuntime().totalMemory() / (1024 * 1024)) + "MB");
     android.util.Log.d("TAG","MAX MEMORY : "+(Runtime.getRuntime().maxMemory() / (1024 * 1024)) + "MB");
     android.util.Log.d("TAG","FREE MEMORY : "+(Runtime.getRuntime().freeMemory() / (1024 * 1024)) + "MB");
     android.util.Log.d("TAG","ALLOCATION MEMORY : "+((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (1024 * 1024)) + "MB");

 

힙메모리 임계값 지정
- onCreate에서 dalvik 버추얼 머신에게 힙메모리 임계값 지정하기 (70%)
dalvik.system.VMRuntime.getRuntime().setTargetHeapUtilization(0.7f); --> 메인 액티비티에서 한번만 지정
(관련 글 : Dalvik - Heap Data Structure - http://embenux.blogspot.com/2010/11/dalvik-heap-data-structure.html)

 

native 메모리 관련 정보
- http://blog.naver.com/anywars?Redirect=Log&logNo=140130318506

 

변수 및 비트맵 처리
- 이벤트에서 모든 지역변수 null 처리히기, 모든 비트맵 인스턴스 변수 recycle, null 시키기,
- System.gc() 호출
- 메모리 누수 처리를 onDestory(), onPause(), onResume()등의 라이프사이클을 고려하여 처리해주는게 중요.

예제 :
if(imageView.getDrawable() != null)
((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();

if(mShowingImage != null && !mShowingImage.getBitmap().isRecycled()){
   mShowingImage.getBitmap().recycle();
}

 

Drawable 처리
- setCallback(null); drawable을 뷰에 설정할때 콜백이 자동으로 지정이 되는데 이를 해제시켜준다.

 

child remove와 비트맵recycle로 메모리 누수 처리
- http://givenjazz.tistory.com/48

 

Adapter 메모리 누수 처리
- http://givenjazz.tistory.com/50


이미지 백그라운드로 사용시
- http://jetblog.tistory.com/28

 

뷰를 상속해서 사용할 시
-destroyDrawingCache 메서드 Override해서 메서드 안에서 recycle등의 메모리관련 처리

 

xml로 뷰를 구성할때 최대한 중첩을 피해야 함. 생각보다 메모리에 영향을 크게 미침. (RelativeLayout이 유용함)

 

Context 관련 메모리 누수 처리
- Activity Context 에 관한 참조를 오랫동안 유지하지 말아라. (해당 참조는 반드시 Activity 의 생명주기와 일치 해야 한다.)
- Activity Context 대신, Application Context 를 사용할 것을 고려하라.
- 만일 Activity 내부 클래스의 생명 주기를 잘 관리하는 경우가 아니라면, Activity 를 참조하고 있는 내부 클래스를 사용하지 말아라. 대신 Static 내부 클래스를 사용하고 해당 클래스가 Activity 와 WeakReference 를 갖도록 해라. 구체적인 구현 방식은 ViewRoot 를 참고 해라.
- 가비지 콜렉터는 메모리 누수 문제가 일어나지 않도록 보장하지 않는다.


xml로 화면 구성할시 merge 태그 활용
- http://blog.naver.com/huewu/110083649927

 

이클립스로 안드로이드 앱 개발시 멈춤현상해결
- http://www.androidpub.com/1323151

 

어플 종료시 캐시 삭제
- http://www.androidpub.com/265895

 

StrictMode
- http://www.androidpub.com/1123776

 

이미지 리사이징 처리 (이미지 크기를 알아내고 그 크기에 따라 사이즈를 조절(줄여준다)한다.
- http://chiyo85.tistory.com/entry/Android-Bitmap-Object-Resizing-Tip
예제
private Bitmap decodeFile( InputStream is )

{

Bitmap b = null;

Display display = getWindowManager().getDefaultDisplay();

// Allow for the title

int width = display.getWidth();

int height = display.getHeight() - mPuzzleView.GetOffsetY();

int minImageSize = Math.max( width, height );

// Decode image size

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;

BitmapFactory.decodeStream( is, null, options );

int scale = 1;

if( options.outHeight > minImageSize || options.outWidth > minImageSize )

{

    scale = (int)Math.pow(  2,  (int)Math.round( Math.log( minImageSize / (double)Math.max( options.outHeight, options.outWidth ) )

    / Math.log( 0.5 ) ) );

}

// Decode with inSampleSize
// BitmapFactory를 새로 생성 안하고 options.inJustDecodeBounds = false로 변경하고 진행해도됨.
BitmapFactory.Options o2 = new BitmapFactory.Options();

o2.inSampleSize = scale;

b = BitmapFactory.decodeStream( is, null, o2 );

return b;

}