title: How to clear dynamically created view from memory?
body: <p>I'm trying to clear some views from memory.</p>
  
  <p>Here's the situation.
  I have an Activity that I'll call it A and another B.</p>
  
  <p>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.</p>
  
  <p>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 !</p>
  
  <p>How do can I clear the views COMPLETELY from memory ?</p>
  
  <p>Thx !</p>
  
  <p>Update-
  Here you go some new information</p>
  
  <p>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.</p>
  
  <p>I tried to resolve this issue with the code below</p>
  
  <pre><code>@Override
  protected void onDestroy() {
      super.onDestroy();      
      nullViewDrawablesRecursive(mRootView);
      mRootView = null;
      System.gc();
  }
  </code></pre>
  
  <p>and that is my nullViewDrawablesRecursive</p>
  
  <pre><code>private void nullViewDrawablesRecursive(View view) {
      if (view != null) {
          try {
              ViewGroup viewGroup = (ViewGroup) view;
  
  
              int childCount = viewGroup.getChildCount();
              for (int index = 0; index &lt; childCount; index++) {
                  View child = viewGroup.getChildAt(index);
                  nullViewDrawablesRecursive(child);
              }
          } catch (Exception e) {
          }
  
          nullViewDrawable(view);
      }
  }
  </code></pre>
  
  <p>And that is my nullViewDrawable</p>
  
  <pre><code>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) {
      }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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 !</p>
  
  <p>Again, thanks a lot for trying to help me! </p>