Tengo una pérdida de memoria usando ListActivity en Android
Tengo una aplicación que usa un Servicio y algunas actividades de la lista. Cuando se abren las actividades, puedo ver el aumento del uso del montón en DDMS, cuando se cierran las actividades, el uso del montón disminuye ligeramente. El servicio todavía se está ejecutando en segundo plano en este momento. Si la actividad se inicia nuevamente al volver a ejecutar la aplicación y se cierra, el uso del montón aumenta nuevamente y luego disminuye, pero nunca vuelve al nivel original antes de que la actividad se abriera por primera vez. Si abre la actividad repetidamente (10-15 veces), luego cierre la actividad, ¡el tamaño del montón (tanto MB como # Objetos) aumenta!
Esperaría que onDestroy de ListActivity se cuide solo cuando se destruya. ¿Qué me estoy perdiendo con esto? ¿Estoy usando ListActivity incorrectamente?
A continuación se muestra una aplicación de prueba similar a mi código real. Cree una nueva aplicación de Android, agregue esto al manifiesto:
<service android:name="LeakTestService"/>
y estos archivos java:
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; } }