我在 Android 中使用 ListActivity 出现内存泄漏 [英] I have a memory leak using ListActivity in Android

查看:33
本文介绍了我在 Android 中使用 ListActivity 出现内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用服务和一些列表活动的应用程序.当活动打开时,我可以看到 DDMS 中的堆使用量增加,当活动关闭时,堆使用量略有下降.此时该服务仍在后台运行.如果通过重新运行应用程序并关闭该活动再次启动,则堆使用量再次增加然后减少,但永远不会回到活动首次打开之前的原始水平.如果它重复(10-15 次)打开活动然后关闭活动,堆大小(MB 和 # 个对象)就会膨胀!

我希望 ListActivity 的 onDestroy 在被销毁时能够自行处理.我错过了什么?我是否错误地使用了 ListActivity?

类似于我的真实代码的测试应用程序如下.创建一个新的 android 应用程序,将其添加到清单中:

<前><service android:name="LeakTestService"/>

和这些java文件:

<前>泄漏测试活动.java-------------包 LeakTest.Test;导入 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 _Adapter;/** 在第一次创建活动时调用.*/@覆盖public void 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--------包 LeakTest.Test;导入 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 自动生成的方法存根返回空;}}

解决方案

我也遇到了同样的问题,但我发现问题只发生在我调试的时候.在正常"运行中,所有活动都消失了.

I have an application that uses a Service and some list activities. When the activities are opened, I can see the heap usage increase in DDMS, when the activities are closed, the heap usage decreases slightly. The service is still running in the background at this point. If the activity is started again by re-running the application and the closed, the heap usage increases again then decreases, but never returns to the original level before the activity was first opened. If it repeatedly (10-15 times) open the activity then close the activity, the heap size (both MB and # Objects) balloons!

I'd expect ListActivity's onDestroy to take care of itself when it gets destroyed. What am I missing with this? Am I using ListActivity incorrectly?

A test app similar to my real code is below. 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;
    }
}

解决方案

I had the very same problem but I found out that the problem only occurred when I was debugging. On a "normal" run all the activities are gone.

这篇关于我在 Android 中使用 ListActivity 出现内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆