查看+标签=内存泄漏?内存、标签

2023-09-05 09:30:05 作者:暗恋最省钱

基础:

活动 - 再现(的onCreate-的onDestroy)每个orientatin修改 查看由ViewFlipper的有两个孩子的:简单的RelativeLayout的和ListView 在ListView的行具有复杂的布局和相关的标签

现在的问题是,我对每一个方向变化的内存泄漏 - 活动停留在记忆与全视图布局。活动本身就是一个环境,所以它会留在内存中,只要相关联的对象会。所以现在我试图找到原因泄漏发生。

The problem is that i have memory leak on each orientation change - activity stays in memory with whole view layout. Activity itself is a context so it'll stay in memory as long as associated objects will. So now i'm trying to find why leaks are happen.

查看具有 setTag()方式。我用它来存储行的一些信息(所以每一行(视图)ListView中具有关联标签)。

View has setTag() method. I'm using it to store some information about rows(so every row(View) in ListView has associated tags).

但如何看法和GC行为的标记?我的标签对象(持有人)中包含的参考意见,但如果认为删除提及它的标签此引用(与标签本身)将很容易收集。

But how does views and GC acts with tags ? My tag objects(holders) contains references to views but if view removes reference to it's tag this references(with tag itself) will be easily collected.

任何人都面临着类似的问题列表视图?

Anybody have faced similar problems with ListViews ?

P.S。我想知道如何GC清洗布局 - 循环引用的tonns,背景,持有人,等​​等...

P.S. i'm wondering how GC cleans layouts - tonns of cyclic references, contexts, holders, etc...

推荐答案

它很容易泄露引用的方向变化的活动。也有少数关于这个博客帖子 - 我觉得需要阅读:

Its easy to leak references to the Activity on orientation change. There are a handful of blog posts about this - which I feel are required reading:

http://ttlnews.blogspot.com/ 2010/01 /攻击,内存的问题上,android.html

http://android-developers.blogspot.com/ 2009/01 /避免内存,leaks.html

HTTP://$c$c.google。 COM / P /安卓/问题/详细信息?ID = 2391

在一个超级简单地说在你的 onRetainNonConfigurationInstance 方法,你只是要小心,你空出的任何引用视图对象,反过来活动引用,进度条等。

In a super nutshell in your onRetainNonConfigurationInstance method you just want to be careful that you null out any references to View objects and in turn Activity references, Progress bars, etc.

一个很好的模式我用的是具有不包含活动参考的StateHolder内部类,但我实现了一个 setActivityForTasks 的方法,我只是传递NULL来,这反过来将所有活动引用为NULL。然后,当你回去通过你的活动的方向改变后,你可以叫 setActivityForTasks(本)重置当前活动。

A good pattern I use is having a "StateHolder" inner class which does contain an Activity reference, but I implement a setActivityForTasks method, which I just pass NULL to, it in turn sets all Activity references to NULL. Then when you're going back through your Activity after the orientation change you can just call setActivityForTasks(this) to reset the current activity.

单外卖只是为了空出,以什么活动在 onRetainNonConfigurationInstance

The single take-away is just to NULL out any references to anything Activity related in onRetainNonConfigurationInstance