sat
sat

Reputation: 41076

Understanding memory leak in Android app

After going through few articles about performance, Not able to get this statement exactly.

"When a Drawable is attached to a view, the view is set as a callback on the drawable"
Soln: "Setting the stored drawables’ callbacks to null when the activity is destroyed."

What does that mean, e.g.

In my app , I initialize an imageButton in onCreate() like this,

imgButton= (ImageButton) findViewById(R.id.imagebtn);

At later stage, I get an image from an url, get the stream and convert that to drawable, and set image btn like this,

imgButton.setImageDrawable(drawable);

According to the above statement, when I am exiting my app, say in onDestroy() I have to set stored drawables’ callbacks to null, not able to understand this part ! In this simple case what I have to set as null ?

I am using Android 2.2 Froyo, whether this technique is required, or not necessary.

Upvotes: 1

Views: 1118

Answers (2)

dstefanox
dstefanox

Reputation: 2222

Here is exactly what was the case in example you cited:

  1. Phone orientation has been changed, and this should mean that old activity should be "dumped" and new one created
  2. If you have stored reference to bitmap as a static field, it has reference to old activity that was supposed to be dumped (drawable has reference to TextView, view has reference to activity)
  3. New activity is created, but your drawable still has the reference to old one, so old one can't be dumped.

Of course, all this is right if you store drawable as static like in cited example:

private static Drawable sBackground;

Upvotes: 2

Romain Guy
Romain Guy

Reputation: 98501

You would have to do this only if you kept the drawable as a static field somewhere, or in a cache of some sort. In this particular situation, there's no reason to set the callback to null.

Upvotes: 4

Related Questions