티스토리 뷰

안드로이드 진저브리드(2.3)부터 이미지 기본 디코딩방식이 16비트에서 32비트로 변경되었고, 이미지를 처리할 때 메모리를 3~4배쯤 더 사용하는 듯하다. 메모리누수는 더 심해져서 액티비티를 종료해도 상황에 따라 메모리가 다 반환이 되질 않는다. 결국 메모리를 직접 환원해줘야한다.

내일인 17일부터 갤럭시S의 진저브리드 업데이트가 시작되고, 앱이 죽는 걸 많은 사람들이 겪게 될텐데, 이 문제를 해결하기 위해 자원마다 null로 설정해주고 gc를 하는 것은 자바에서 작성하기 꽤나 괴로운 일이다. 다행히 메모리를 많이 잡아먹는 drawable만 리커시브로 해제해줘도 대부분의 메모리는 환원이 된다.

스택오버플로우랑 구글을 검색해도 질문만 있고 이렇다할 해결방법이 없길래 그냥 직접 작성해서 아파치2.0 라이센스로 공개한다. 다음의 메소드는 View에 붙어있는 View의 child를 리커시브로 null로 설정해주는 메소드다. 액티비티가 죽으면 가비지콜렉팅을 해도 레퍼런스가 삭제되서 메모리 환원이 안되므로 onDestroy안에서 System.gc()를 해줘야한다.

  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.givenjazz.android;
  17. import android.view.View;
  18. import android.view.ViewGroup;
  19. import android.widget.AdapterView;
  20. import android.widget.ImageView;
  21. /**
  22. * @author givenjazz
  23. *
  24. */
  25. public class RecycleUtils {
  26. private RecycleUtils(){};
  27. public static void recursiveRecycle(View root) {
  28. if (root == null)
  29. return;
  30. root.setBackgroundDrawable(null);
  31. if (root instanceof ViewGroup) {
  32. ViewGroup group = (ViewGroup)root;
  33. int count = group.getChildCount();
  34. for (int i = 0; i < count; i++) {
  35. recursiveRecycle(group.getChildAt(i));
  36. }
  37. if (!(root instanceof AdapterView)) {
  38. group.removeAllViews();
  39. }
  40. }
  41. if (root instanceof ImageView) {
  42. ((ImageView)root).setImageDrawable(null);
  43. }

  44. root = null;
  45. return;
  46. }
  47. }


사용 예제)

  1. @Override
  2. protected void onDestroy() {
  3. RecycleUtils.recursiveRecycle(getWindow().getDecorView());
  4. System.gc();
  5. super.onDestroy();
  6. }

 


위에 코드만으로도 어느정도 효과를 볼 수 있을 것이다. 하지만 코드를 보면 짐작할 수 있듯이 AdapterView는 제대로 환원되지 않는다. AdapterView를 사용한다면 Adapter에 null로 설정해주는 메소드를 만들어서 onDestory()에 삽입해야한다. -수정- WeakReference로 만들지 않아도 액티비티를 종료할 때는 메모리 환원이 되지만 액티비티가 돌아가는 동안 어댑터에서 활용하는 뷰들은 메모리환원이 되질 않는다. Reference를 활용하면 어댑터가 돌아가는 동안 안쓰는 뷰도 메모리 환원이 가능하다.

어댑터가 가비지컬렉팅을 제대로 못해서 죽는 경우가 있는데, 이 때는 OutOfMemoryError 예외를 잡아내서 제대로 환원 못한 뷰를 수동으로 풀어줘야한다. 다음에 예제와 곁들여서 제대로 설명하겠다.

null 설정해주는 메소드 만드는 예제)

 

주석으로 설명한 부분만 예제처럼 만들어주면 되고 나머지는 그냥 예제일 뿐이다.

  1. package com.givenjazz.android;
  2. import java.util.ArrayList;
  3. import java.util.HashSet;
  4. import java.util.Set;
  5. import android.app.Activity;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.ImageView;
  10. import com.givenjazz.android.RecycleUtils;
  11. public class OptionAdapter extends BaseAdapter {
  12. private ArrayList<Integer> mOptionList;
  13. private Activity mContext;
  14. //멤버변수로 해제할 Set을 생성
  15. private List<WeakReference<View>> mRecycleList = new ArrayList<WeakReference<View>>();
  16. public int selectedIndex = -1;
  17. OptionAdapter(Activity c, ArrayList<Integer> list) {
  18. mContext = c;
  19. mOptionList = list;
  20. }
  21. //onDestory에서 쉽게 해제할 수 있도록 메소드 생성
  22. public void recycle() {
  23. for (WeakReference<View> ref : mRecycleList) {
  24. RecycleUtils.recursiveRecycle(ref.get());
  25. }
  26. }
  27. @Override
  28. public int getCount() {
  29. return mOptionList.size();
  30. }
  31. @Override
  32. public Object getItem(int position) {
  33. return mOptionList.get(position);
  34. }
  35. @Override
  36. public long getItemId(int position) {
  37. return position;
  38. }
  39. @Override
  40. public View getView(int position, View convertView, ViewGroup parent) {
  41. ImageView i = new ImageView(mContext);
  42. i.setImageResource(mOptionList.get(position));
  43. //메모리 해제할 View를 추가
  44. mRecycleList.add(new <WeakReference<View>(i));
  45. return i;
  46. }
  47. }

 


액티비티에서 어댑터까지 제거하는 예제)

  1. @Override
  2. protected void onDestroy() {
    //Adapter가 있으면 어댑터에서 생성한 recycle메소드를 실행
  3. if (mImageAdapter != null)
  4. mImageAdapter.recycle();
  5. RecycleUtils.recursiveRecycle(getWindow().getDecorView());
  6. System.gc();
  7. super.onDestroy();
  8. }


 

액티비티를 종료하기 전부터 메모리 오류가 발생한다면 가비지콜렉터 문제가 아니라 메모리에 로딩자체를 못하는 것이므로 xml을 inflate시키지말고 BitmapFactory.Options로 작게 디코드하거나 16비트타입으로 디코딩해야한다.

--------------------------------------------------

http://givenjazz.tistory.com/48 펌.

이런 글을 보면 아직 내 코딩 실력이 얼마나 부족한지 깨닫는다.. 계속해서 정진하자.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
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
글 보관함