Maggie
Maggie

Reputation: 8071

android memory leaks

I've been reading about memory leaks in Android. In my application, I was getting java.lang.OutOfMemoryError: bitmap size exceeds VM budget exception. As I googled it, I found it to be a common error (this SO question and this SO question deal with the problem) and I used a suggested solution.
I found an official Android documentation dealing with the issue and SO question explaining the official example. It says in the example:

the drawable has a reference to the TextView which itself has a reference to the activity (the Context) which in turns has references to pretty much anything

I still don't understand one thing: if Drawable isn't declared as static, why isn't it destroyed in activity's onDestroy() method? Drawable is destroyed, TextView is destroyed, Activity is destroyed - no references and no memory leaks. Isn't it how it is supposed to work?

Upvotes: 3

Views: 740

Answers (1)

Caner
Caner

Reputation: 59150

I still don't understand one thing: if Drawable isn't declared as static, why isn't it destroyed in activity's onDestroy() method? Drawable is destroyed, TextView is destroyed, Activity is destroyed - no references and no memory leaks. Isn't it how it is supposed to work?

Yes, that is how it supposed to work but they might not be immediately destroyed in onDestroy(). Exact time when Garbage Collector will run cannot be predicted. There are many things that trigger that. For example if your application is running low on allowed heap memory or if some other application is trying to allocate more memory...

If you want your bitmap to be destroyed immediately you should call Bitmap.recycle().

How GC runs, and many other useful information about memory management in android are explained in this video:

http://www.youtube.com/watch?v=_CruQY55HOk

Upvotes: 2

Related Questions