6

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!

braX
  • 11,506
  • 5
  • 20
  • 33
Leandro Silva
  • 804
  • 1
  • 9
  • 28

2 Answers2

2

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

Eric Levine
  • 13,536
  • 5
  • 49
  • 49
  • Hello ! When I started this application I paid special attention about this memory leaks and thecniques to avoid them. I think that's not the point but the way I need to use my Activities is. Please, check my edit at first post and help me! I'll appreciate it! – Leandro Silva Apr 02 '12 at 19:08
  • You shouldn't have to do any of the things you posted. Can you share the full code for your Activity? My guess is there is a memory leak somewhere, and it would be better to try and fix that versus nulling out all of your Views. – Eric Levine Apr 02 '12 at 22:45
  • Well. I think my problem is a kind of really hard to track. Those functions belongs to mine abstract WActivy (extends Activity) and all of the other activities derivates it. So that activity I use to recreate every form from webservice can or can't be the problem. I really don't know what to do, that WActiviy only has those functions and another one to help me with orientationchange (I have 2 behaviors for all my customviews to them resize according to the screen size). I setted the "Config Changes" to "orientation" months ago to help me in another problem, do you think it can be the problem? – Leandro Silva Apr 03 '12 at 11:15
  • Share WActivity and one or two that inherit from it. You might be better off putting them somewhere like http://pastebin.com/ and adding the link to your post. Adding in tons of code to your question will make it too hard to read. You obviously have a problem somewhere, and I wouldn't make assumptions about what is/isn't the cause. – Eric Levine Apr 03 '12 at 13:39
  • I realize maybe there's a memory leak Here's the thing: I have a static context list that I add a context when a Activity is created on "onCreate" and remove it on "onDestroy" – Leandro Silva Apr 03 '12 at 17:27
  • This is the whole code from my WActivity [link](http://pastebin.com/NxDrc6F5). I removed my comments and put another ones to show you where it happens One of the uses for this static list of context is to inflate views in a PopupWindow to show to the user if there is a problem, or somoething like that. I cant inflate it with an ApplicationContext just with an Activity Context, and I have to know what is the currentActivity that can receive this type of "dialogs" and show it – Leandro Silva Apr 03 '12 at 17:27
  • If you are keeping a static list of contexts, then that may be what is causing your memory leak. You are using WActivityManager in onCreate and onDestroy, but those won't get called very often. It may be better to put them into onResume and onPause instead. However, you should find an alternative to trying to keep a static reference to your current activity. Its hard to recommend something specific without knowing more about your app and seeing the code. You may want to ask for suggestions as a new question on here. – Eric Levine Apr 03 '12 at 18:20
  • The idea of WActivityManager is to access statically the Context for actual activities to use in differents application's points. I'll try to put these operations in onPause and onResume, what you said makes sense to me. I really appreciated your help and your willingness. I don't have reputation to mark this answer with usefull , sorry. I'll make another question asking for helping with these static contexts and will put here, if you want to help me again I'll be glad. cya! – Leandro Silva Apr 03 '12 at 20:44
  • "The idea of WActivityManager is to access statically the Context for actual activities to use in differents application's points." - you should try not to do this, it really goes against the way that the Android framework is designed. Try to avoid the use of static variables and take advantage of mechanisms like Bundle for sharing information across Activities. – Eric Levine Apr 03 '12 at 21:31
  • I maked this question, if you can help me there I'll be glad [link](http://stackoverflow.com/questions/10010073/how-to-avoid-static-context-reference-when-i-need-to-use-a-activity-context) – Leandro Silva Apr 04 '12 at 11:31
1

In onDestroy() set the views to null and call the garbage collector.

@Override
public void onDestroy() {
    super.onDestroy();
    myView = null;
    System.gc();
}

This can help the garbage collector by calling System.gc() but it isn't guaranteed that the memory is cleared. However as long as you don't have a leak, there shouldn't be a problem.

Jan
  • 633
  • 11
  • 22
  • 2
    While there is nothing wrong with this, it is really not necessary. – Eric Levine Mar 30 '12 at 19:51
  • I tried something like that, please check my edit on the first post there is some new informations about my problem. – Leandro Silva Apr 02 '12 at 19:08
  • 1
    `System.gc();` should not call, even calling `System.gc();` does not mean `gc` will collect all objects or run. it is totally depend on gc whether it will run or not – Ashu Kumar Apr 20 '17 at 10:01