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

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

问题描述

我有一个使用服务和一些列表活动的应用程序。当活动被打开,我可以看到在DDMS堆的使用量增加,当活动被关闭,堆的使用量略有下降。该服务是在后台此时仍在运行。如果活动是通过重新运行该应用程序和关闭的再次启动,该堆的使用再次增加,然后减小,但不会返回到原来的水平的活性,在第一次打开之前。如果反复(10-15倍)打开活动,然后关闭该活动,堆的大小(包括MB和#对象)的气球!

我期望ListActivity的的onDestroy到自己照顾自己,当它被摧毁。我缺少这个?我使用ListActivity不正确?

一个测试程序相似,我真正的code是如下。创建一个新的Andr​​oid应用程序,将其添加到清单:

<服务机器人:名称=LeakTestService/>

和这些Java文件:

LeakTestActivity.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 =新的ArrayList>();
    ArrayAdapter _Adapter;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        意图SVC =新的意图(this.getApplicationContext(),LeakTestService.class);
        startService(SVC);

        //这个问题发生在这两个SimpleAdapter和ArrayAdapter
        // _适配器=新SimpleAdapter(this.getApplicationContext(),_Data,android.R.layout.two_line_list_item,新的String [] {行1,2号线},新的INT [] {android.R.id.text1, android.R.id.text2});
        _Adapter =新的ArrayAdapter(this.getApplicationContext(),android.R.layout.simple_list_item_1,新的String [] {数据1,数据2});

        //如果该行被删除,堆的使用量从未气球,如果你反复打开+关闭
        getListView()setAdapter(_Adapter)。
    }

    @覆盖
    公共无效的onDestroy(){
        _Adapter = NULL; //这一行没有帮助
        getListView()setAdapter(空)。 //同样没有这一行
        super.onDestroy();
    }
}



LeakTestService.java
--------
包LeakTest.Test;

进口android.app.Service;
进口android.content.Intent;
进口android.os.IBinder;
进口android.widget.Toast;

公共类LeakTestService延伸服务{
    @覆盖
    公共无效ONSTART(意向意图,诠释startId){
        Toast.makeText(getBaseContext(),服务ONSTART,Toast.LENGTH_SHORT).show();
    }

    @覆盖公共无效的onDestroy(){
        Toast.makeText(getBaseContext(),服务的onDestroy,Toast.LENGTH_SHORT).show();
        }

    @覆盖
    公众的IBinder onBind(意向意图){
        // TODO自动生成方法存根
        返回null;
    }
}

解决方案

我有同样的问题,但我发现这个问题,只有当我在调试时发生。在一个正常运行所有的活动都没有了。

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天全站免登陆