Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear dynamically created view from memory?

I'm trying to clear some views from memory.

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

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.

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 ?

Thx !

Update- Here you go some new information

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();
}

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

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

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!

like image 546
Leandro Silva Avatar asked Mar 30 '12 19:03

Leandro Silva


1 Answers

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

like image 154
Eric Levine Avatar answered Sep 29 '22 23:09

Eric Levine