32

One of the touted features of the ART runtime in Android 5.0+ is heap compaction, to reduce heap fragmentation. A fragmented heap can get OutOfMemoryErrors a lot easier, as there may not be a single contiguous free block of memory big enough for your needs, even if the heap overall has enough free space.

I understand that this occurs when the app moves to the background, based on Google conference presentations and the like. However, the only statement that I can find on it in the documentation says:

Homogeneous space compaction is free-list space to free-list space compaction which usually occurs when an app is moved to a pause imperceptible process state. The main reasons for doing this are reducing RAM usage and defragmenting the heap.

It's unclear exactly what a "pause imperceptible process state" means, technically.

Suppose an app does not have any foreground activities at the moment. Is there anything that the developer might have done that might prevent heap compaction for that app's process? For example, does having a foreground service block heap compaction?

4
  • 6
    Just verifying that you've read source.android.com/devices/tech/dalvik/gc-debug.html where it talks about 'heap compaction' Currently, the event that triggers heap compaction is ActivityManager process-state changes. When an app goes to background, it notifies ART the process state is no longer jank “perceptible.” Sep 19, 2016 at 22:39
  • @MorrisonChang: No, I missed that page -- thanks! That helps, but I don't know how to interpret "an app goes to background". If that literally refers to IMPORTANCE_BACKGROUND, then even having a running service might block heap compaction. Sep 19, 2016 at 22:47
  • seems like you just wanted to be assured. have you faced any problem with your background tasks related services ? it would be great if you can come up with an actual performance related problem. and about services they have low priority compared to foreground tasks and it was there since from beginning like your service will be killed or blocked when phone is short on memory. to dig deep you can read about sticky-CMS and object graph , will help Sep 22, 2016 at 4:43
  • 2
    "seems like you just wanted to be assured" -- I just want to be able to tell developers what to expect, since this behavior is undocumented. "your service will be killed or blocked when phone is short on memory" -- no, it will not. Android terminates processes to free up system RAM; Android does not destroy individual components. Moreover, that is not strictly related to the question. Sep 22, 2016 at 11:56

3 Answers 3

9
+250

Putting the pieces of the puzzle together.

From what I can determine, ART will compact anything that is paused for 2-3 seconds and by paused it means not currently running in background, so activities, but not running services. It will also compact on the fly, or concurrently while the app is in the foreground.

Currently, the event that triggers heap compaction is ActivityManager process-state changes. When an app goes to background, it notifies ART the process state is no longer jank “perceptible.” This enables ART do things that cause long application thread pauses, such as compaction and monitor deflation.

Chet Hasse states:

Garbage Collection
ART brought improved garbage collection dynamics. For one thing, ART is a moving collector; it is able to compact the heap when a long pause in the application won’t impact user experience (for example, when the app is in the background and is not playing audio). Also, there is a separate heap for large objects like bitmaps, making it faster to find memory for these large objects without wading through the potentially fragmented regular heap. Pauses in ART are regularly in the realm of 2–3ms.

From what I can see any pause in the app is fair game for the ART GC.

I suspect the app needs to be paused completely of all services, etc for the compact to occur, as it's reallocating the memory addresses of the heap, and for this to occur, it cannot be changing. As this larger compact that is taken during the app pause and not on the fly is a dynamic rearrangement of the heap. The only changes that can be made in the smaller pauses is to re-route some addresses on processes no longer being used.

Though this is an educated guess, not definitive and I will endeavour to get more info.

The source code here should have the answer. They're using naming like InJankPerceptibleProcessState() and trying to wade through this, as you probably already have yourself.

Reading it, will update answer when/if I find the definite answer.

4
  • "I suspect the app needs to be paused completely of all services, etc for the compact to occur" -- I'm hoping that compaction would be blocked by foreground services but not regular non-foreground services. But this gets at the crux of the issue. We as developers really ought to know what the things are that we might do (or not do) that impacts whether ART can compact the heap. If the answer is what you suspect, that having any service blocks heap compaction, that would be important to know. Sep 28, 2016 at 12:31
  • 1
    Oh, please do not take my comment as a complaint! I was merely clarifying the issue, that's all. Many thanks for your help! Sep 28, 2016 at 12:56
  • @CommonsWare just my 2 cent when app is background means not closed completely i think ART can still compact heap , i have seen it happen when i was analyzing heap dumps in my app
    – JAAD
    Sep 28, 2016 at 13:35
  • 1
    @Yvette i havent checked that need to check that, but my foreground app is multi process so i guess it will kill my UI process when it is not needed anymore
    – JAAD
    Sep 28, 2016 at 13:39
0

Homogeneous space compaction is free-list space to free-list space compaction which usually occurs when an app is moved to a pause imperceptible process state. The main reasons for doing this are reducing RAM usage and defragmenting the heap.

Source : https://developer.android.com/studio/profile/investigate-ram.html#LogMessages

4
  • 4
    That is what I have in my question. You can tell that by reading the question and looking at the quoted passage. Sep 27, 2016 at 11:27
  • 2
    @Jamil Hasnine Tamim . Glad for the daring answer. Dear do you even know who CommonsWare is.. !! He is a legend ... And if he asks some question (which itself is very rare !) you should think not twice but more than 100 times before answering! Anyways kudos for the attempt !... And one more thing, I am not the one who downvoted you... Probably you might think , thats why I am mentioning here
    – Sreehari
    Sep 28, 2016 at 6:46
  • @CommonsWare sry bro...I am new in stack but I try to give some info not exact answer! I apologize..and waiting for correct answer. Sep 28, 2016 at 14:18
  • @Stallion try to know...if he is legend, than um honored to get reply from him... thanks Sep 28, 2016 at 14:20
-1

Actually you can measure the idle time of an app by. Start the idle timer and stop if if there is any event captured in TextWatcher/OnKeylistner, if you app is in background and none of these events are called, it is good to be collected by GC.

Also this heap contraction is event based and priority based. Eg if there is never a scenario when user need memory, OS will not even do it.

As far as priority is concerned, for garbage collection, it looks for background apps with no background service, then app with background service and at last the foreground apps.

1
  • This question has nothing to do with idle time of an app. Please provide any evidence that heap compaction is "priority based". Also, provide any evidence that heap compaction is performed on foreground apps, since the documentation clearly states otherwise. Sep 28, 2016 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.