GC 是否保证释放未使用活动的内存

Is GC guaranteed to free memory of unused activities

我正在尝试分析 Android 应用程序的内存使用情况。我这样做使用

adb shell dumpsys meminfo <package name>

我的应用只有一个Activity,我重复以下步骤大概十几次:

  1. 打开应用程序;
  2. 使用后退按钮退出

这样做了十几次后 dumpsys 显示我的 Activities 的大约 1-2 个实例仍在内存中。在我点击 adb dumpsys 几次后,Activity 计数降为零。这是正常的吗?如果是泄漏,我不希望 Activity 计数下降到零。这是否意味着 GC 缓慢地占用 Activity 对象的内存?

Does that mean GC claims the memory of the activity objects slowly?

通过按后退按钮完成 Activity 并不意味着它的实例将立即被杀死并且内存 GC-ed。 ActivityonDestroy() 方法不是 "finalizer"。可以遇到这样一种情况,当启动一个新的 Activity 实例时,"old" 实例仍然位于内存中(作为 reference)。

If it is a leak I don't expect the activity count to go down to zero.

如果您看到应用程序的进程 运行 这不是泄漏,因为 "the activity count goes down to zero".

After I hit adb dumpsys for a couple of times the activity count goes down to zero. Is this normal?

是的,由于上述原因。