简体   繁体   English

Android垃圾收集器释放内存

[英]Android Garbage Collector Freed Memory

I'm working on an app that handles a lot of allocations (on the order of 4 million doubles and a million classes). 我正在开发一个处理大量分配的应用程序(大约400万个双打和100万个类)。 I was looking through garbage collector logs and I'm seeing different amounts of memory total being freed across different devices. 我正在查看垃圾收集器日志,我看到不同设备上释放的内存总量不同。

For example, I have a Moto X (2014) that ends up freeing just over 312 MB. 例如,我有一个Moto X(2014)最终释放超过312 MB。 I also have a Droid Bionic running the same code on the same data that frees 616 MB on average. 我还有一个Droid Bionic在相同的数据上运行相同的代码,平均释放616 MB。 Both devices end up with a heap size of about 50 MB. 两个设备最终的堆大小约为50 MB。

Why is so much more memory freed by the GC on the Bionic than the Moto X? 为什么Bionic上的GC释放的内存比Moto X多? They should generate the same amount of garbage each. 他们应该生成相同数量的垃圾。 What's going on behind the scenes in the garbage collector? 垃圾收集器幕后发生了什么? The Moto X is on Android 5.1 and the Bionic is on 4.1.2. Moto X在Android 5.1上,Bionic在4.1.2上。

Edit: I have four devices that are freeing about 300 MB RAM: the Moto X (2014), Nexus 7 2013, Nexus 7 2012, and Razr i. 编辑:我有四个可以释放大约300 MB RAM的设备:Moto X(2014),Nexus 7 2013,Nexus 7 2012和Razr i。 All four of these use ART. 所有这四个都使用ART。 The Bionic is running the Dalvik runtime. Bionic正在运行Dalvik运行时。 Is this why there's less freeing up? 这是为什么没有那么放松了? I noticed GC_FOR_ALLOC doesn't happen in ART but is getting called all the time on Dalvik. 我注意到GC_FOR_ALLOC在ART中没有发生,但在Dalvik上一直被调用。

Quoting from this post: 引用这篇文章:

Next, the ART team worked to optimize the garbage collector (GC). 接下来,ART团队致力于优化垃圾收集器(GC)。 Instead of two pauses totaling about 10ms for each GC in Dalvik, you'll see just one, usually under 2ms. 而不是Dalvik中每个GC总共大约10ms的两次暂停,你只会看到一次,通常不到2ms。 They've also parallelized portions of the GC runs and optimized collection strategies to be aware of device states. 他们还并行化了部分GC运行和优化的收集策略,以了解设备状态。 For example, a full GC will run only when the phone is locked and user interaction responsiveness is no longer important. 例如,只有在手机被锁定且用户交互响应不再重要时,才会运行完整的GC。 This is a huge improvement for applications sensitive to dropped frames. 对于对丢帧敏感的应用程序,这是一个巨大的改进。

What the author says here is that ART powered devices will be much more efficient in context of GC - both with respect to the time GC "wastes" and the amount of memory freed at runtime. 作者在这里所说的是,在GC的上下文中,ART驱动的设备将更加高效 - 无论是在GC“浪费时间”还是在运行时释放的内存量。

Additional contribution to the lower memory usage can be attributed to this (this is just a guess): 对较低内存使用量的额外贡献可归因于此(这只是猜测):

In perhaps the most important improvement, ART now compiles your application to native machine code when installed on a user's device. 在最重要的改进中,ART现在可以在安装到用户设备上时将应用程序编译为本机机器代码。 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). 被称为提前编译,随着编译器针对特定体系结构(例如ARM,x86或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. 因此,您的应用程序安装时间会稍长,但在启动时会更快启动,因为在Dalvik VM上运行时执行的许多任务(例如类和方法验证)已经发生。

Since ART compiles your app ahead of time, the time of compilation can be extended, thus allowing the compiler to perform a better job optimizing your code. 由于ART会提前编译您的应用程序,因此可以扩展编译时间,从而使编译器能够更好地优化代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM