如何从内存中清除动态创建的视图? [英] How to clear dynamically created view from memory?

查看:88
本文介绍了如何从内存中清除动态创建的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从内存中清除一些视图.

I'm trying to clear some views from memory.

这是情况. 我有一个活动,我将其称为A和另一个B.

Here's the situation. I have an Activity that I'll call it A and another B.

现在,我在活动A中按下一个按钮,该按钮将调用活动B,该活动将动态创建大量视图 此后,我按返回按钮以返回到活动A 多次重复这些步骤2.

Now I press a button in Activity A that calls Activity B that creates a lot of views dynamically After this, I press back button to return to Activity A Repeat theses 2 steps a lot of times.

结果是,在DDMS中,对象和内存的数量增加了静止图像(对象的数量增加了88个,而分配的数量增加了0,002MB) 这意味着视图不会从内存中删除!

The result is, in DDMS, the number of objects and memory Allocated stills growing ( the number of objects is increased by 88 and Allocated by 0,002MB ) That means the views dont be removed from memory !

如何从内存中完全清除视图?

How do can I clear the views COMPLETELY from memory ?

谢谢!

更新- 在这里,您可以得到一些新信息

Update- Here you go some new information

基本上,在我的现实世界"中,我只有一个被创建很多次的活动.发生这种情况是因为我必须与Web服务进行通信,并且所有响应都在该Activity的新实例中创建.

Basically, on my "real world" I have only one Activity that is created a lot of times. This occurs because I it have to communicate to a webservice and all the responses are created in that new instance of this Activity.

我尝试使用下面的代码解决此问题

I tried to resolve this issue with the code below

@Override
protected void onDestroy() {
    super.onDestroy();      
    nullViewDrawablesRecursive(mRootView);
    mRootView = null;
    System.gc();
}

那是我的nullViewDrawablesRecursive

and that is my nullViewDrawablesRecursive

private void nullViewDrawablesRecursive(View view) {
    if (view != null) {
        try {
            ViewGroup viewGroup = (ViewGroup) view;


            int childCount = viewGroup.getChildCount();
            for (int index = 0; index < childCount; index++) {
                View child = viewGroup.getChildAt(index);
                nullViewDrawablesRecursive(child);
            }
        } catch (Exception e) {
        }

        nullViewDrawable(view);
    }
}

那是我的nullViewDrawable

And that is my nullViewDrawable

private void nullViewDrawable(View view) {
    try {
        view.setBackgroundDrawable(null);
    } catch (Exception e) {
    }

    try {
        ImageView imageView = (ImageView) view;
        imageView.setImageDrawable(null);
        imageView.setBackgroundDrawable(null);
    } catch (Exception e) {
    }
}

基本上,我试图在破坏活动之前从其父级(mRootView)中删除所有视图和子级.如果我不这样做的话,它会很好地工作,对象和内存使用量会越来越多.

Basically I'm trying to remove all the views and childs from their parent (mRootView) before destroying the activity. It works pretty well, if I don't do this, the objects and memory usage increase more and more.

问题是,这并不完美,显然某些类型的视图并没有被破坏.而且我认为我正在重塑方向盘",对于一件简单的事情,这似乎太难了!

The point is, it's not perfect, apparently some type of views doesn't be destroyed. And I think I'm "reinventing the wheel", it seems to damn hard for a simple thing !

再次感谢您为我提供的帮助!

Again, thanks a lot for trying to help me!

推荐答案

通常,您不必担心从内存中清除视图.您将在必要时让虚拟机和Android框架处理此问题.但是,您确实需要担心内存泄漏.如果您的活动正在共享/保留对视图的引用,并且无法对其进行垃圾回收,那么这就是一个问题.您可以从这里开始阅读: http://android- developers.blogspot.com/2009/01/avoiding-memory-leaks.html

Typically, you don't need to worry about clearing views from memory. You would let the virtual machine and Android framework handle this when it is necessary. However, you do need to be concerned with memory leaks. If your Activities are sharing/holding onto references to views, and they cannot be garbage collected, then that is a problem. You can start by reading about that here: http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html

即使看不到您的代码,也很难提供一些更具体的建议...

Its hard to provide some more specific advice without seeing you code though...

这篇关于如何从内存中清除动态创建的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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