Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

destroy view(layout)

I'm fighting with memory leaks now. So i'm curious if there any way to manually destroy view(in activity onDestroy method) ? The whole layout(activity contentView) is a bit complex because of parent-child references, context references, tags, etc.

GC is not able to collect my layout now. And the problem is hiding deeply in view structure... So the only way to find it - is to try destroy leaf views manualy so at some moment GC will collect root view and give me a knowlege of where is problem located.

My layout structure: ViewFlipper(RelativeLayout, ListView(ViewFlipper(RelativeLayout, RelativeLayout)))

like image 548
Oleksandr Avatar asked Mar 24 '11 12:03

Oleksandr


1 Answers

You can remove a View from a ViewGroup, but there is no way to manually destroy a view. If you are getting memory leaks, it is usually because you are holding a long-lived reference to your Context outside of your Views.

  1. Don't store anything that has a Context in a static field (i.e. Drawables - Bitmaps are fine)
  2. Remove all handlers, clear all timers
  3. Don't hold onto Contexts in Threads/AsyncTasks, or if you do make sure they're weak-referenced.

It is fine for Views to contain information relating to other Views (i.e. the Context of another view) since all views are destroyed -- it is most likely because the Context is held onto by something (and the context has a handle on most things - i.e. all of your Views) that you are unable to free the memory.

like image 50
Joseph Earl Avatar answered Sep 26 '22 17:09

Joseph Earl