为什么在Android中退出后,应用程序及其资源会保留在内存中?

时间:2011-11-16 09:01:36

标签: android memory-leaks ddms heap-dump

我有一个简单的Hello World应用程序

public class TestLeaksOnFinish extends Activity  
{
    static int ctr = 0;
    protected void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    TextView t = new TextView(this);
    t.setText("Hello World! "+ctr++);
    setContentView(t);          
    }   
}

当我多次运行时,每次按下BACK,我看到ctr每次都会增加,表示在BACK之后Activity没有被完全杀死。

按下BACK后,通过在DDMS中转储HPROF文件也可以确认这一点 该文件仍包含我的TestLeaksOnFinish活动类。

有人可以解释为什么在按BACK后,此活动仍然存在于堆转储中吗?

当我列出传入的引用时,我得到以下内容 screenshot from DDMS

1 个答案:

答案 0 :(得分:11)

在Android活动中不要被杀死,他们只会被移到后台。这就是Android的工作方式,你不能杀死一个应用程序,或者基本上没有像你所知的那样退出。它只会留在后台和内存中。 / p>

当内存不足时,系统会根据系统为每个应用程序本身提供的优先级开始终止应用程序进程。