Android App Memory leakage Check Tool MAT

Source: Internet
Author: User

Eclipse has a plug-in Tool MAT (Memory Analyzer Tool) to help locate Memory leakage objects.

Conclusion: The MAT tool does not directly tell you Where memory leaks occur, but lists suspicious objects. You need to carefully check why these objects are not released.

The following is the test code, tested on Android 4.2.2. 1. In this case, the Activity cannot be recycled, because the created Activity can be referenced directly using a private static Activity a0, and the Activity cannot be recycled. 2. In this case, the Activity cannot be recycled. This should also cause static Drawable to lock the Activity and reference the relationship mBackground1 --> Button --> Activity. To be analyzed

 
  1. public class MainActivity extends Activity implements Button.OnClickListener{   
  2.  
  3.     
  4.  
  5.  final private static String TAG = "MainActivity";   
  6.  
  7.     
  8.  
  9.  private static Drawable mBackground1;   
  10.  
  11.  private static Drawable mBackground2;   
  12.  
  13.  private static Drawable mBackground3;   
  14.  
  15.  private static Drawable mBackground4;   
  16.  
  17.      
  18.  
  19.  private static Activity  a0 ;   
  20.  
  21.  private static Activity  a1 ;   
  22.  
  23.  private static Activity  a2 ;   
  24.  
  25.  private static Activity  a3 ;   
  26.  
  27.  private static Activity  a4 ;   
  28.  
  29.      
  30.  
  31.    
  32.  
  33.  /*   
  34.  
  35.   * Shutdown intent   
  36.  
  37.  */  
  38.  
  39.  private final String INTENT_ACTION_REQUEST_SHUTDOWN =   
  40.  
  41.      "android.intent.action.ACTION_REQUEST_SHUTDOWN";   
  42.  
  43.      
  44.  
  45.  @Override  
  46.  
  47.  public void onCreate(Bundle savedInstanceState) {   
  48.  
  49.      Log.v(TAG, "onCreate  Activity="+this);   
  50.  
  51.     super.onCreate(savedInstanceState);   
  52.  
  53.      setContentView(R.layout.activity_main);   
  54.  
  55.      
  56.  
  57.      //1   
  58.  
  59.     if(false){   
  60.  
  61.      if(a0 == null){   
  62.  
  63.         a0 = this;   
  64.  
  65.         Log.v(TAG, "onCreate  Activity a0="+a0);   
  66.  
  67.      }   
  68.  
  69.      else if(a1 == null){   
  70.  
  71.         a1 = this;   
  72.  
  73.          Log.v(TAG, "onCreate  Activity a1="+a1);   
  74.  
  75.      }   
  76.  
  77.      else if(a2 == null){   
  78.  
  79.         a2 = this;   
  80.  
  81.          Log.v(TAG, "onCreate  Activity a2="+a2);   
  82.  
  83.      }   
  84.  
  85.      else if(a3 == null){   
  86.  
  87.          a3 = this;   
  88.  
  89.          Log.v(TAG, "onCreate  Activity a3="+a3);   
  90.  
  91.     }   
  92.  
  93.   else if(a4 == null){   
  94.  
  95.         a4 = this;   
  96.  
  97.         Log.v(TAG, "onCreate  Activity a4="+a4);   
  98.  
  99.      }   
  100.  
  101.      }   
  102.  
  103.     
  104.  
  105.     
  106.  
  107.     //set up button listener   
  108.  
  109.      Button myButton = (Button)findViewById(R.id.button_poweroff);   
  110.  
  111.     myButton.setOnClickListener(this);   
  112.  
  113.     
  114.  
  115.     
  116.  
  117.     myButton = (Button)findViewById(R.id.button_reboot);   
  118.  
  119.      myButton.setOnClickListener(this);   
  120.  
  121.      
  122.  
  123.      //2   
  124.  
  125.     if (mBackground1 == null) {     
  126.  
  127.         Log.v(TAG, "onCreate  mBackground1");   
  128.  
  129.          mBackground1 = getResources().getDrawable(R.drawable.adbroot_004);     
  130.  
  131.          myButton.setBackgroundDrawable(mBackground1);   
  132.     }   
  133.  
  134.     else if(mBackground2 == null){   
  135.  
  136.          Log.v(TAG, "onCreate  mBackground2");   
  137.  
  138.         mBackground2 = getResources().getDrawable(R.drawable.test002);   
  139.  
  140.         myButton.setBackgroundDrawable(mBackground2);   
  141.  
  142.     }   
  143.  
  144.      else if(mBackground3 == null){   
  145.  
  146.         Log.v(TAG, "onCreate  mBackground3");   
  147.  
  148.          mBackground3 = getResources().getDrawable(R.drawable.test003);   
  149.  
  150.          myButton.setBackgroundDrawable(mBackground3);   
  151.  
  152.     }   
  153.  
  154.     else if(mBackground4 == null){   
  155.  
  156.         Log.v(TAG, "onCreate  mBackground4");   
  157.  
  158.         mBackground4 = getResources().getDrawable(R.drawable.adbroot_003);   
  159.  
  160.          myButton.setBackgroundDrawable(mBackground4);   
  161.  
  162.      }   
  163.  
  164.     
  165.  
  166.    
  167.  
  168.   
  169.  
  170.  }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.