Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does View.removeAllViews() release memory?

Tags:

android

I experienced OutOfMemory exception in my code, so I am reviewing the code.

I have a question, does View.removeAllViews() release the memory that were used by the child views that were previously added into this the parent view?

Thanks.

like image 306
user256239 Avatar asked Feb 24 '10 23:02

user256239


2 Answers

View.removeAllViews() might release the Views, and will schedule them for garbage collection (which could occur later) however you might still have a problem because @drawables in the XML or in your code with 'getDrawable()' could still consume memory.

I have struggled with OutOfMemory errors a lot myself and found that the biggest issue was not the Layout XML or the View objects, but the Drawables and Resources that they loaded (or I loaded to attach to them.)

A great talk on this can be found on YouTube from Google IO 2011:

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

This talk introduces using the Eclipse MAT (Memory Analysis Tool) to troubleshoot OutOfMemory exceptions. I suggest it because you mention you're reviewing code and the best place to start looking for an OutOfMemory error is ... "what is taking up my memory?"

like image 143
Cory Trese Avatar answered Oct 24 '22 23:10

Cory Trese


Except if your code has references on the ViewGroup's child views, all ViewGroup's child views should be "garbage collectable".

like image 36
david Avatar answered Oct 25 '22 01:10

david