【发布时间】:2011-04-24 01:17:00
【问题描述】:
我有一个使用服务和一些列表活动的应用程序。当活动打开时,我可以看到 DDMS 中的堆使用量增加,当活动关闭时,堆使用量略有下降。此时服务仍在后台运行。如果通过重新运行应用程序并关闭来再次启动活动,则堆使用量会再次增加然后减少,但永远不会回到活动首次打开之前的原始水平。如果它反复(10-15 次)打开活动然后关闭活动,堆大小(MB 和 # 个对象)会膨胀!
我希望 ListActivity 的 onDestroy 在它被销毁时能够自行处理。我错过了什么?我是否错误地使用了 ListActivity?
类似于我的真实代码的测试应用程序如下。创建一个新的 android 应用程序,将其添加到清单中:
还有这些 java 文件:
泄漏测试活动.java ------------- 包泄漏测试。测试; 导入 java.util.ArrayList; 导入 java.util.HashMap; 导入android.app.Activity; 导入android.app.ListActivity; 导入android.content.Intent; 导入android.os.Bundle; 导入 android.widget.ArrayAdapter; 导入 android.widget.SimpleAdapter; 公共类 LeakActivity 扩展 ListActivity { ArrayList> _Data=new ArrayList>(); ArrayAdapter _适配器; /** 在第一次创建活动时调用。 */ @覆盖 公共无效 onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent svc = new Intent(this.getApplicationContext(), LeakTestService.class); 启动服务(svc); // SimpleAdapter 和 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"} ); // 如果删除此行,如果您反复打开+关闭它,堆使用量永远不会膨胀 getListView().setAdapter(_Adapter); } @覆盖 公共无效 onDestroy() { _适配器=空; // 这行没有帮助 getListView().setAdapter(null); // 这行也没有 super.onDestroy(); } } 泄漏测试服务.java -------- 包泄漏测试。测试; 导入android.app.Service; 导入android.content.Intent; 导入android.os.IBinder; 导入 android.widget.Toast; 公共类 LeakTestService 扩展服务 { @覆盖 公共无效onStart(意图意图,int startId){ Toast.makeText(getBaseContext(), "Service onStart", Toast.LENGTH_SHORT).show(); } @Override public void onDestroy() { Toast.makeText(getBaseContext(), "Service onDestroy", Toast.LENGTH_SHORT).show(); } @覆盖 公共IBinder onBind(意图意图){ // TODO 自动生成的方法存根 返回空值; } }【问题讨论】:
标签: android memory-leaks listactivity listadapter