메모리 누수 문제가 있습니다. 활동을 보여 주면 모든 것이 작동합니다. 뒤로 누르고 활동을 다시로드하려고하면 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로 설정 하시겠습니까?
또한 메모리 누수가 발생하는 이미지 로딩 코드를 추가했습니다. – Vincent
비트 맵 처리 기능을 사용하지 않았지만 걱정 스럽지만 거기에 관해서는 많은 스레드가있는 것처럼 보입니다. 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
다음은 메모리 누수를 찾는 프로세스에 대한 훌륭한 리소스입니다. https://www.linkedin.com/pulse/fixing-memory-leaks-android-studio-albert-lai – rakoonise