Binders In Android (Part II): Reference Counting & Death Recipients
Photo by Crissy Jarvis on Unsplash
This is the second part of multi-part series about Binders in Android. It is highly recommended to read Part I (if you haven’t already) otherwise this part probably won’t make much sense.
Other articles in this series:
IntroductionIn the first part, we saw that the binder is used for everything that happens across processes in the core platform. They are the driving force behind interaction in the framework and used extensively in the system for security. In this part, we’ll talk about some very important features of binders which make it suitable for hostile environments. But why do we even need to know about death recipients & reference counting mechanism? Since the very beginning, Android’s central focus has been the ability to multitask. In order to achieve it, Android takes a unique approach by allowing multiple applications to run at the same time. Users usually never explicitly close the applications but instead, leave them running in the background. When memory is low, the Android system kills the low priority apps which are there in the background. This ability to keep processes waiting in the background speeds up app switching later down the line. Android doesn’t rely on applications being well-written and responsive to polite requests to exit. Rather, it brutally force-kills them without warning, allowing the kernel to immediately reclaim resources associated with the process. This helps prevent serious out of memory situations and gives Android total control over misbehaving apps that are negatively impacting the system. For this reason, there is no guarantee that any user-space code (such as an Activity’s Considering the limited memory available in mobile environments, this approach seems promising. However, there is still one issue that needs to be addressed: how should the system detect an application’s death so that it can quickly clean up its state? When an application dies, its state will be spread over dozens of system services (the Activity Manager, Window Manager, Power Manager, etc.) and several different processes. These system services need to be notified immediately when an application dies so that they can clean up their state and maintain an accurate snapshot of the system. Here enters the death recipient & reference counting. Now we have good enough reasons to know about these features, so let’s dive into it.
Reference CountingReference counting guarantees that Binder objects are automatically freed when no one references them and is implemented in the kernel driver. The binder kernel driver does not know anything about binder objects that have been shared with a remote process instead what actually happens is:
Death RecipientBinder’s “link-to-death” facility, allows a process to get a callback when another process hosting a binder object goes away. In Android, any process can receive a notification when another process dies by taking the following steps:
Looking at the LocationManagerService source code helps us understand what’s happening under the hood (note that some of the source code has been cleaned up for the sake of brevity):
Let’s walk through the code step-by-step:
Wrapping UpThe Binder’s link-to-death & reference counting features are incredibly useful and are used extensively by the framework’s system services. Many other system services depend on these facilities in order to ensure that expensive resources aren’t leaked when an application process is forcefully killed. Some other examples are the
Android News
Compose CameraX on Android
By Peng Jiang Android new UI toolkit Jetpack compose is in beta now, which has all the features you need to build production-ready apps. CameraX is another Jetpack support library, which let you control the camera easier. As compose is still under development, lots of the views are still not available the compose way.
By
ProAndroidDev -
droidcon News
Tech Showcases, Developer Resources & Partners
EmployerBrandingHeader
jobs.droidcon.com
![]() Latest Android Jobs
Kotlin Weekly
![]() Your weekly dose of Kotlin
ProAndroidDev
![]() Android Tech Blogs, Case Studies and Step-by-Step Coding
Zalando
![]() Meet one of Berlin's top employers
Academy for App Success
![]() Google Play resources tailored for the global droidcon community |
Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.