0

I'm having problems with memory leaks. When I show an Activity every thing works. When I press back and try to reload the Activity I get an outOfMemoryException.

So from the docs I've read I can conclude that in this activity not all references are deleted and so the activity isn't recycled by the garbage collector (because Activitys with active references arent collected by the gc).

For instance could the code below cause a memory leak (Suppose there is a small amount of memory available)? Because I initialise the gestureDetector but I never uninitialise it:

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());



}
    }

edit: I already have this problem when I set an image in an imageView.

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

esses back and then put all variables to null?

Vincent
  • 6,058
  • 15
  • 52
  • 94

2 Answers2

2

What does your application (or rather, this Activity) actually do? I don't see how that code alone could cause much harm, but I could be wrong.

I had a bit of trouble with memory leaks & memory tracking which I later fixed, these articles gave me a good bit of insight:

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

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

A bit more info may be needed to help you though. Your Activity is called FotoGallery so I'm going to have a quick guess and say your problem lies with the pictures that you load in and whatnot. There's been quite a bit of questions on SO regarding limited memory & loading pictures.

Sean O'Toole
  • 4,304
  • 6
  • 35
  • 43
Codemonkey
  • 3,412
  • 3
  • 20
  • 24
  • I added the image loading code which also causes the memory leak – Vincent Apr 28 '11 at 09:17
  • I haven't really played with Bitmap handling I am afraid, but there seems to be a lot of threads about it on SO and similar though from what I can tell on Google. Did you try the answer posted at http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966 (78 point one)? I am afraid I am not much use otherwise. :/ http://stackoverflow.com/questions/5697760/android-out-of-memory-exception-when-creating-bitmap has a good list of links as well. – Codemonkey Apr 28 '11 at 10:07
  • Here's a really great resource for the process of finding a memory leak: https://www.linkedin.com/pulse/fixing-memory-leaks-android-studio-albert-lai – rakoonise Mar 27 '15 at 05:58
0

I solved this issue by adding System.gc(); in the beginning of the setImage method (without any if's or anything like that).