简体   繁体   English

Android GC行为

[英]Android GC Behaviour

I am trying to understand why android GC behaves the way it does, 我试图了解为什么Android GC会表现出这种方式,

  • VM Heap is set to 512 MB VM堆设置为512 MB
  • Runtime.getRuntime maxMemory is 512 MB Runtime.getRuntime maxMemory为512 MB
  • Runtime.getRuntime totalMemory is 13 MB Runtime.getRuntime totalMemory为13 MB
  • Runtime.getRuntime freeMemory is 3 MB Runtime.getRuntime freeMemory为3 MB

I am always within +/- 2-3 MB of these values. 我总是在这些值的+/- 2-3 MB之内。

My code is functional so I am using a lot objects but I am no where near 512 MB yet I am constantly seeing GC calls in my logcat especially in tight loops. 我的代码可以正常运行,所以我使用了很多对象,但是我离512 MB不远了,但是我一直在我的logcat中不断看到GC调用,尤其是在紧密循环中。 Why doesn't it just let the heap grow? 为什么不让堆增长呢? and GC in small increments instead of pausing my app every time for 300 ms. 和GC的小幅增加,而不是每次都暂停我的应用300毫秒。

Is there any way for me to have a little more control over the garbage collection process (this won't be a market app I have no problem with installing a custom rom.) other than stock answer being change your coding style do not create a lot of objects, since I see a similar behavour on a real device (a quad 1,2 ghz machine with 2 gb of ram when no other app is running it is kind of annoying to have a machine this powerfull and not be able to code the way I like.) 除了股票答案正在更改您的编码样式之外,我是否可以通过其他任何方式对垃圾收集过程进行更多控制(这不会是市场应用程序,我在安装自定义rom方面没有问题。)很多对象,因为我在真实设备上看到类似的行为(当没有其他应用程序运行时,带有2 gb ram的Quad 1,2 ghz机器,因此拥有如此强大且无法编码的机器有点烦人我喜欢的方式。)

The main problem is that an app runs in a VM and each VM has its limit. 主要问题是应用程序在VM中运行,并且每个VM都有其限制。 As long as you are not going native (NDK) you will have to live with the little RAM your VM gets. 只要您不使用本机(NDK),您就必须忍受VM获得的少量RAM。

I have no problem with installing a custom rom. 我没有安装自定义rom的问题。

If you want to build your own ROM (or search for one with configurable settings), increase the size per VM and be happy with it. 如果要构建自己的ROM(或使用可配置的设置搜索ROM),请增加每个VM的大小并对此感到满意。

My code is functional so I am using a lot objects 我的代码可以正常工作,所以我使用了很多对象

Well there is a difference between "using a lot of objects" and "using a lot of objects with damn short life time". 好吧,“使用大量对象”与“使用大量寿命短的对象”之间是有区别的。 But as you don't wanna hear about it... 但是,因为您不想听到它...

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

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