title: How to free up memory from all running or paused activities and return to started activity
body: <p>I spent all my week digging into android heap memory control.</p>
  
  <p>The story is that we changed the login screen background to something more HD. That caused serious of problem. It eat up more memory than before so the rest of activity, especially when open an large image from it, will cause a out of memory problem.</p>
  
  <p>After using DDMS to analysis, I finally find out a solution to solve it. I added a bitmap control over the image when it start, and recycle it when user leave the activity. Here is my code:</p>
  
  <pre><code>protected void findByViews() {
  
      //Find all widgets view on the layout by ID
      background = (RelativeLayout) findViewById(R.id.mainTableLayout);
  
      Bitmap bm = BitmapFactory.decodeResource(this.getResources(), R.drawable.pbackground);
      BitmapDrawable bd = new BitmapDrawable(this.getResources(), bm);
      background.setBackgroundDrawable(bd);
      }
  </code></pre>
  
  <p>And when user leave the activity or stop it:</p>
  
  <pre><code>public void onPause() {
      super.onPause();
      java.lang.System.out.println("MAIN onPause");
              //Clear Image from Heap.
      BitmapDrawable bd = (BitmapDrawable)background.getBackground();
      background.setBackgroundResource(0);
      bd.setCallback(null);
      bd.getBitmap().recycle();
  }
  </code></pre>
  
  <p>By doing this, the rest of the application will run under around 40%-50% free memory. Before it just run under less than 10%.</p>
  
  <p>However, this is not good end. I encountered another problem. I have a function that will let user logout and back to main screen. The login in screen itself, however, require around 80% of heap to run. While at runtime(After login), rest activities require about 50%-60% to run. </p>
  
  <p>In simple, I just don't have enough heap memory to lunch the main screen again from runtime!.</p>
  
  <p>Here is the code at exit and re-lunch:</p>
  
  <pre><code>    public void onReceive(Context context, Intent intent) {
          // TODO Auto-generated method stub
          String serviceType = intent.getStringExtra("serviceType").trim();
          System.out.println("serviceType:" + serviceType);
          if (serviceType.equals("exit")) {
              AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                      getCurrentActivity());
              alertDialog.setTitle("Logout");
              alertDialog.setMessage("Do you want to logout?");
              alertDialog.setPositiveButton("Yes",
                      new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog,
                                  int which) {
                              // Unreg from App server so do not receive
                              // notification when logout
                              MultiData.getMultiData().serverLogOut();// never
                                                                      // serverlogout
                                                                      // before
                                                                      // DeviceRegistrar.unregisterWithServer
                              ImageManagerMemCache.INSTANCE.clear();
                              Intent intent = null;
                              intent = new Intent(getCurrentActivity(),
                                      main.class);
                              intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                              // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                              startActivity(intent);
                          }
                      });
              alertDialog.setNegativeButton("No",
                      new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog,
                                  int which) {
                              // here you can add functions
                          }
                      });
              alertDialog.show();
          }
  </code></pre>
  
  <p>How can I improve these code, to free up all memory of the application heap, then restart the main activity from the fresh beginning? Or only I could do is to reduce the memory use of the login screen(Which I doubt is really hard).</p>
  
  <p>Thank you!</p>