2011-04-28 2 views
0

메모리 누수 문제가 있습니다. 활동을 보여 주면 모든 것이 작동합니다. 뒤로 누르고 활동을 다시로드하려고하면 outOfMemoryException이 발생합니다.Android 메모리 누수 솔루션

그래서이 문서에서는 모든 참조가 삭제되지 않으므로 활동이 가비지 수집기에 의해 재활용되지 않는다고 결론을 내릴 수 있습니다 (활성 참조가있는 활동은 gc에서 수집하지 않기 때문에).

예를 들어 아래의 코드가 메모리 누수를 일으킬 수 있습니까? (소량의 메모리가 있다고 가정) 나는 gestureDetector을 초기화하지만 때문에 나는 그것을 uninitialise 결코 :

public class FotoGallery extends Activity { 
    private GestureDetector gestureDetector; 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     gestureDetector = new GestureDetector(new MyGestureDetector()); 



} 
    } 

편집 : 나는 이미지 뷰에 이미지를 설정할 때 이미이 문제가 있습니다.

public class FotoGallery extends Activity { 
    private GestureDetector gestureDetector; 
    private String path = "/mnt/sdcard/DCIM/img001.jpg"; 
private Bitmap currentBitmap; 


public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     gestureDetector = new GestureDetector(new MyGestureDetector()); 
     setImage(path); 


} 

private static final int SWIPE_MIN_DISTANCE = 30; 
private static final int SWIPE_MAX_OFF_PATH = 250; 
private static final int SWIPE_THRESHOLD_VELOCITY = 200; 

class MyGestureDetector extends SimpleOnGestureListener { 
    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
      float velocityY) { 
     try { 
      if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
       return false; 
      // right to left swipe 
      if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE 
        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       //rightFling detected 
      } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE 
        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       //leftFling detected 
      } 
     } catch (Exception e) { 
      // nothing 
     } 
     return false; 
    } 

} 


private void setImage(String path) { 
    if (currentBitmap != null) { 
     currentBitmap.recycle(); 
    } 
    final ImageView imageView = (ImageView) findViewById(R.id.imageview); 
    currentBitmap = BitmapFactory.decodeFile(path); 
    imageView.setImageBitmap(currentBitmap); 
} 
    } 


Now my final question is how can you uninitialise all the variables at the right time? Do you specifically need to listen for when a user pr 

다시 모든 변수를 null로 설정 하시겠습니까?

답변

2

응용 프로그램 (또는이 활동)이 실제로 무엇을합니까? 그 코드만으로는 많은 피해를 입힐 수있는 방법을 알 수는 없지만 잘못 될 수 있습니다.

I 메모리에 문제의 비트가,이 기사는 나에게 통찰력의 좋은 비트를 준 나중에 고정 & 메모리 추적을 누설했다 :

http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html

http://android-developers.blogspot.co.uk/2009/02/track-memory-allocations.html

조금 더 많은 정보가 도움이 필요할 수도 있습니다. 귀하의 활동을 FotoGallery라고 부르겠습니다. 그래서 저는 빠른 추측을하고 문제가 당신이로드하는 그림들과 거짓말이라고 말 할 것입니다. 제한된 메모리에 관한 매우 많은 질문이 있습니다. & 사진을로드 중입니다.

+0

또한 메모리 누수가 발생하는 이미지 로딩 코드를 추가했습니다. – Vincent

+0

비트 맵 처리 기능을 사용하지 않았지만 걱정 스럽지만 거기에 관해서는 많은 스레드가있는 것처럼 보입니다. Google에. http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966 (78 점 하나)에 게시 된 답변을 시도해 보셨습니까? 나는 다르게 사용하지 않을까 두렵다. :/http://stackoverflow.com/questions/5697760/android-out-of-memory-exception-when-creating-bitmap 잘 링크 목록이 있습니다. – Klaus

+0

다음은 메모리 누수를 찾는 프로세스에 대한 훌륭한 리소스입니다. https://www.linkedin.com/pulse/fixing-memory-leaks-android-studio-albert-lai – rakoonise

0

이 문제는 System.gc()를 추가하여 해결되었습니다. setImage 메소드의 시작 부분 (if 나 그와 비슷한 것을 사용하지 않고).