【问题标题】:Android Memory leak in activity活动中的Android内存泄漏
【发布时间】:2012-05-25 21:28:40
【问题描述】:

我正在使用 MAT 工具测试我的应用程序,发现存在内存泄漏,但我无法在代码中找到它。请帮助我。

//第一个活动

package intent.sample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class IntentA extends Activity implements OnClickListener {
    Handler handler = new Handler();
    private Intent i;
    private Button button;

    public void onClick(View src) {
        if (src == (View) button) {
            i = new Intent(this, IntentSampleActivity.class);
            handler.postDelayed(new Runnable() {

                public void run() {
                    startActivity(i);
                }
            }, 20000);
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        button = (Button) findViewById(R.id.screen2button);
        button.setOnClickListener(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        this.finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler = null;
        i = null;
        this.finish();
    }
}

//第二个活动

package intent.sample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class IntentSampleActivity extends Activity implements OnClickListener {

    private Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button) findViewById(R.id.screen1button);
        button.setOnClickListener(this);
    }

    public void onClick(View scr) {
        if (scr == (View) button) {
            Intent ii = new Intent(this, IntentA.class);
            startActivity(ii);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        this.finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.finish();
    }
}

我正在创建一个新线程并开始一项活动。这会创建活动类的新实例吗?

【问题讨论】:

  • 我已经在 Ondestroy 方法中将 null 分配给我的处理程序,但我不确定启动的线程是否会创建具有相同实例的新活动或创建新实例?
  • 您真的不应该从onPause()onStop()onDestroy() 呼叫finish()。它不仅破坏了标准的应用程序生命周期,而且特别是 onDestroy() 只有在活动已经完成时才会被调用。
  • 是的,这很好,但我相信它不会产生任何内存问题。如果我错了,请纠正我。
  • 你是不是泄露了runnable?
  • 我无法理解 runnable 如何启动新活动?

标签: android memory-management memory-leaks eclipse-memory-analyzer


【解决方案1】:

在这段代码中,每次我启动活动时,都会创建一个新实例。为避免这种情况,我需要在活动的 android manifest.xml 文件中使用启动模式。启动模式可以是单任务或单实例。

还是非常感谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 2014-07-02
    • 2021-12-05
    相关资源
    最近更新 更多