Issue
When I create, add and remove views within one activity, what is the lifecycle of the removed views? They could be removed with removeView()
or removeAllViews()
.
Are they available for garbage collection immediately upon removing or do they stay until the activity goes away? For example because of any register of all views attached or not.
Should I do additional steps to get them available for garbage collection quickly?
Solution
If you have a View
you have created programatically then you would need to ensure two things to make sure the View
is GC'd.
- Call
removeView()
from theViewGroup
where you added theView
. This is because aViewGroup
will maintain a reference to the children. - Make sure you aren't maintaining any references to the
View
in your code, for instance as a field of theActivity
class. Maintaining a reference to theView
in this way will prevent it from being GC'd.
There are several good articles about how to avoid memory leaks, including this one
Answered By - David Rawson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.