摧毁视图(布局) [英] destroy view(layout)

查看:126
本文介绍了摧毁视图(布局)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和内存泄漏现在的战斗。所以我很好奇,如果有任何的方式来手动销毁视图(活动的onDestroy方法)?
整个布局(活动内容查看)是因为亲子引用,引用背景下,标签等。

的有点复杂

GC是不是现在能够收集我的布局。而问题是从结构上看深深隐藏。所以只有这样,才能找到它 - 就是试图摧毁叶意见manualy所以在某个时刻GC将收集根视图,并给我的其中问题所在的knowlege

我的布局结构:ViewFlipper(RelativeLayout的,ListView控件(ViewFlipper(RelativeLayout的,RelativeLayout的)))


解决方案

您可以删除从ViewGroup中的视图,但没有办法手动销毁视图。如果您遇到内存泄漏,它通常是因为你牵着你的意见之外的长寿命参考上下文。


  1. 请不要存放任何具有静态字段上下文(即图形内容 - 位图是罚款)

  2. 删除所有处理程序,清除所有的计时器

  3. 请不要守住上下文在线程/ AsyncTasks,或者如果你确定他们是弱引用。

这是罚款视图包含与其他浏览信息(即另一种观点的上下文中),因为所有的观点被破坏 - 因为上下文的东西扶住(和上下文有一个把手上这是最有可能大多数事情 - 即所有的意见),你是无法释放内存

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)))

解决方案

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.

这篇关于摧毁视图(布局)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆