java - Android:绝对清除 Activity

标签 java android memory-leaks

我正在尝试处理 Android 内存。问题是我想完全释放所有内存并开始新 Activity 。我找到了一些有用的答案:

Android: Clear the back stack

Android: Clear Activity Stack

Clear the entire history stack and start a new activity on Android

之后,我试着举两个例子:

第一个例子:

    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);

我使用上面的代码在两个 Activity A 和 B 之间切换。这是内存消耗的图片: enter image description here

第二个例子:

在主 Activity 中:

    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
    startActivity(intent);

在 Main2Activity 中:

    onBackPressed();

我也在两个 Activity 之间切换。本例内存消耗: enter image description here

看起来像 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK 并不是完全释放内存的完美方式。

我真的需要知道是否像第二个示例那样释放所有内存?

提前致谢!

最佳答案

将 android:noHistory="true"属性添加到您的 <activity>在 AndroidManifest.xml 中像这样:

<activity android:name=".MyActivity"
    android:noHistory="true">
</activity>

设置标志

Intent i = new Intent(this, Splash.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(i);
    finish();

并查看此链接 Changing activity in android clears the memory needed for the previous activities?
还有这个链接 https://commonsware.com/blog/2011/10/03/activities-not-destroyed-to-free-heap-space.html

关于java - Android:绝对清除 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34713496/

相关文章:

java.lang.ClassCastException : java. lang.String 无法转换为 java.lang.Boolean,使用 Table.addRow() 和 Jackcess

java - Java生成的数字签名不会在openssl中验证

android - GridLayout 不显示

c - 无限循环中的内存泄漏

java - 用 JButton 制作棋盘

java - 如何使用 java 代码中的条件更改 mysql 数据库中的值?

c# - MonoDroid SurfaceView

java - 如何将地理围栏存储在磁盘中? (安卓)

c++ - qDeleteAll 和 new[]

c++ - 当内存分配数不总是相同时如何检测内存泄漏?