Android 垃圾收集器释放内存

标签 android memory garbage-collection android-runtime

我正在开发一个处理大量分配的应用程序(大约 400 万个 double 和一百万个类)。我查看了垃圾收集器日志,发现不同设备释放的内存总量不同。

例如,我有一个 Moto X (2014),它最终释放了超过 312 MB。我还有一个 Droid Bionic 在相同的数据上运行相同的代码,平均释放 616 MB。两种设备最终的堆大小约为 50 MB。

为什么 Bionic 上的 GC 释放的内存比 Moto X 多得多?它们应该各自产生相同数量的垃圾。垃圾收集器的幕后发生了什么? Moto X 运行的是 Android 5.1,而 Bionic 运行的是 4.1.2。

编辑:我有四台设备正在释放大约 300 MB RAM:Moto X (2014)、Nexus 7 2013、Nexus 7 2012 和 Razr i。所有这四个都使用ART。 Bionic 正在运行 Dalvik 运行时。这就是释放较少的原因吗?我注意到 GC_FOR_ALLOC 不会在 ART 中发生,但在 Dalvik 上一直被调用。

最佳答案

引自 this帖子:

Next, the ART team worked to optimize the garbage collector (GC). Instead of two pauses totaling about 10ms for each GC in Dalvik, you’ll see just one, usually under 2ms. They’ve also parallelized portions of the GC runs and optimized collection strategies to be aware of device states. For example, a full GC will run only when the phone is locked and user interaction responsiveness is no longer important. This is a huge improvement for applications sensitive to dropped frames.

作者在这里所说的是,ART 驱动的设备在 GC 环境中将更加高效 - 就 GC“浪费”的时间和运行时释放的内存量而言。

降低内存使用量的额外贡献可归因于此(这只是一个猜测):

In perhaps the most important improvement, ART now compiles your application to native machine code when installed on a user’s device. Known as ahead-of-time compilation, you can expect to see large performance gains as the compilers are tuned for specific architectures (such as ARM, x86, or MIPS). This eliminates the need for just-in-time compilation each time an application is run. Thus your application will take slightly longer to install but will boot faster upon launch as many tasks executed at runtime on the Dalvik VM, such as class and method verification, have already taken place.

由于 ART 会提前编译您的应用程序,因此可以延长编译时间,从而使编译器能够更好地优化您的代码。

关于Android 垃圾收集器释放内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31731726/

相关文章:

android - Flutter dart 调试器断点停止工作

java - Android 支持设计库中 TabLayout 中的 IndexOutOfBoundsException

memory - 在什么情况下静态分配比动态分配好?

c - 在堆数组上使用数组初始化符号

java - Java 和 PHP 应用程序的 GC 和内存行为?

android - 第三方支付。这是什么?

php - 调用特定 php 函数后出现 Android Volley null 错误,其他函数工作正常

c - 如何让进程共享内存(c,linux)?

python - 返回后是否可以删除该对象?

java - 在 full GC 之前获得预先警告