Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android memory leak example from Google I/O

I just had a look at the google io video "memory management for android". Slides are available here http://dubroy.com/memory_management_for_android_apps.pdf. The memory leak example is on slide 36.

I do not understand why this causes a leak after orientation change. I do understand that leak is an inner class and has a reference to the outer class. Also, I do understand that the static variable "leak" references the "Leaky" object..thus the whole activity. I think that's special because of the static keyword. Static variables have a certain memory and are probably not gc'ed (at least as long as the application runs)?!?

Well, what happens on oriantation change? A new activity instance is created and the activities onCreate is called. leak == null is false. Leak still points to the "old" activity. That's a leak. The old activity cant be gc'ed, right?

Why does memory usage increase with every oriantation change? In my (wrong) understanding I'd assume that just the first activity can't be gc'ed. The other activites that are created because of oriantation change can be gc'ed because they aren't referenced by that static variable "leak".

However..obviously..I'm completely wrong!

like image 955
207 Avatar asked Oct 01 '11 22:10

207


1 Answers

A classic explanation of the orientation change Context memory leak from Google blog. You were most of the way there, I think, noting static reference of the inner to the outer class.

like image 167
Walter K Avatar answered Oct 14 '22 11:10

Walter K