gpt4 book ai didi

Android 垃圾收集器释放内存

转载 作者:IT王子 更新时间:2023-10-28 23:32:53 25 4
gpt4 key购买 nike

我正在开发一个处理大量分配的应用程序(大约 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com