I have a memory leak using ListActivity in Android

I have an application that uses the service and some activities. When actions are open, I see an increase in heap usage in DDMS, when actions are closed, heap usage is slightly reduced. At the moment, the service is still running in the background. If the action is restarted again, starting the application and closed, the use of the heap increases again and then decreases, but never returns to its original level before the activity was first opened. If it opens activity several times (10-15 times), close the action, heap size (both MB and # Objects)!

I would expect ListActivity onDestroy to take care of itself when it is destroyed. What am I missing with this? Am I using ListActivity incorrectly?

Below is a test application similar to my real code. Create a new Android application, add this to the manifest:

  <service android: name = "LeakTestService" />

and these java files:

  LeakTestActivity.java
 -------------
 package LeakTest.Test;

 import java.util.ArrayList;
 import java.util.HashMap;

 import android.app.Activity;
 import android.app.ListActivity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.widget.ArrayAdapter;
 import android.widget.SimpleAdapter;

 public class LeakActivity extends ListActivity {
     ArrayList> _Data = new ArrayList> ();
     ArrayAdapter _Adapter;

     / ** Called when the activity is first created.  * /
     @Override
     public void onCreate (Bundle savedInstanceState) {
         super.onCreate (savedInstanceState);

         Intent svc = new Intent (this.getApplicationContext (), LeakTestService.class);
         startService (svc);

         // the problem happens with both SimpleAdapter and ArrayAdapter
         // _ Adapter = new SimpleAdapter (this.getApplicationContext (), _Data, android.R.layout.two_line_list_item, new String [] {"line1", "line2"}, new int [] {android.R.id.text1, android.R.id.text2});
         _Adapter = new ArrayAdapter (this.getApplicationContext (), android.R.layout.simple_list_item_1, new String [] {"data1", "data2"});

         // if this line is removed, the heap usage never balloons if you repeatedly open + close it
         getListView (). setAdapter (_Adapter);
     }

     @Override
     public void onDestroy () {
         _Adapter = null;  // this line doesn't help
         getListView (). setAdapter (null);  // neither does this line
         super.onDestroy ();
     }
 }



 LeakTestService.java
 --------
 package LeakTest.Test;

 import android.app.Service;
 import android.content.Intent;
 import android.os.IBinder;
 import android.widget.Toast;

 public class LeakTestService extends Service {
     @Override
     public void onStart (Intent intent, int startId) {
         Toast.makeText (getBaseContext (), "Service onStart", Toast.LENGTH_SHORT) .show ();
     }

     @Override public void onDestroy () {
         Toast.makeText (getBaseContext (), "Service onDestroy", Toast.LENGTH_SHORT) .show ();
         }

     @Override
     public IBinder onBind (Intent intent) {
         // TODO Auto-generated method stub
         return null;
     }
 }
+3
source share
2 answers

I had the same problem, but I found out that the problem only occurred when I was debugging. With a "normal" start, all actions disappeared.

+8
source

If it opens activity several times (10-15 times), close the action, heap size (both MB and # Objects)!

Did you run the garbage collector? There is a button for DDMS.

Once you have started the garbage collector and it seems that you are not collecting anything for collection (as registered in LogCat), you can always dump the heap and check it with MAT or jhat .

Am I using ListActivity incorrectly?

Remove all occurrences of getApplicationContext() in your activity and replace them with this . Activity is Context , and you want to use Activity in these places.

Similarly, I don't know why you are calling getBaseContext() on the Service , and not just using this , since Service also has a Context .

+4
source

Source: https://habr.com/ru/post/905131/


All Articles