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

Mar*_*ele 3 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);          
    }   
}
Run Code Online (Sandbox Code Playgroud)

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

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

有人可以解释为什么在按BACK后这个Activity仍然存在于堆转储中?

当我列出传入的引用时,我得到以下内容 来自DDMS的截图

Ser*_*yol 11

在Android活动中不要被杀死,他们只会被转移到后台.这就是Android如何工作,你不能杀死一个应用程序或基本上没有退出你知道.它只会留在后台和内存中.

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