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
- public class MainActivity extends Activity implements Button.OnClickListener{
-
-
-
- final private static String TAG = "MainActivity";
-
-
-
- private static Drawable mBackground1;
-
- private static Drawable mBackground2;
-
- private static Drawable mBackground3;
-
- private static Drawable mBackground4;
-
-
-
- private static Activity a0 ;
-
- private static Activity a1 ;
-
- private static Activity a2 ;
-
- private static Activity a3 ;
-
- private static Activity a4 ;
-
-
-
-
-
- /*
-
- * Shutdown intent
-
- */
-
- private final String INTENT_ACTION_REQUEST_SHUTDOWN =
-
- "android.intent.action.ACTION_REQUEST_SHUTDOWN";
-
-
-
- @Override
-
- public void onCreate(Bundle savedInstanceState) {
-
- Log.v(TAG, "onCreate Activity="+this);
-
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.activity_main);
-
-
-
- //1
-
- if(false){
-
- if(a0 == null){
-
- a0 = this;
-
- Log.v(TAG, "onCreate Activity a0="+a0);
-
- }
-
- else if(a1 == null){
-
- a1 = this;
-
- Log.v(TAG, "onCreate Activity a1="+a1);
-
- }
-
- else if(a2 == null){
-
- a2 = this;
-
- Log.v(TAG, "onCreate Activity a2="+a2);
-
- }
-
- else if(a3 == null){
-
- a3 = this;
-
- Log.v(TAG, "onCreate Activity a3="+a3);
-
- }
-
- else if(a4 == null){
-
- a4 = this;
-
- Log.v(TAG, "onCreate Activity a4="+a4);
-
- }
-
- }
-
-
-
-
-
- //set up button listener
-
- Button myButton = (Button)findViewById(R.id.button_poweroff);
-
- myButton.setOnClickListener(this);
-
-
-
-
-
- myButton = (Button)findViewById(R.id.button_reboot);
-
- myButton.setOnClickListener(this);
-
-
-
- //2
-
- if (mBackground1 == null) {
-
- Log.v(TAG, "onCreate mBackground1");
-
- mBackground1 = getResources().getDrawable(R.drawable.adbroot_004);
-
- myButton.setBackgroundDrawable(mBackground1);
- }
-
- else if(mBackground2 == null){
-
- Log.v(TAG, "onCreate mBackground2");
-
- mBackground2 = getResources().getDrawable(R.drawable.test002);
-
- myButton.setBackgroundDrawable(mBackground2);
-
- }
-
- else if(mBackground3 == null){
-
- Log.v(TAG, "onCreate mBackground3");
-
- mBackground3 = getResources().getDrawable(R.drawable.test003);
-
- myButton.setBackgroundDrawable(mBackground3);
-
- }
-
- else if(mBackground4 == null){
-
- Log.v(TAG, "onCreate mBackground4");
-
- mBackground4 = getResources().getDrawable(R.drawable.adbroot_003);
-
- myButton.setBackgroundDrawable(mBackground4);
-
- }
-
-
-
-
-
-
-
- }