Have you ever wondered how Garbage Collection works under the hood in Android?
The term “under the hood” basically, boils down to these 4 simple questions:
- What is GC?
- How it works internally?
- What happens when it fails?
- Who is eligible for GC?
1. What is GC?
GC is a mechanism of collecting unused objects and freeing up (recycling ♺) the memory space for further usage.
2. How it works internally?
Till now, there were 5 versions of GC, in the Android OS, namely:
- Dalvik GC (Until KitKat): “Stop the World GC”
- ART GC (Lollipop and Marshmallow): “Generational GC”
- ART GC (Nougat): ART/Dalvik Android Team rewrites the entire allocation process in assembly code.
- ART GC (Oreo): “Concurrent Copying GC”
- ART GC (Android 10 onwards): Re-introduced “Generational GC” as an extension of powerful “Concurrent Copying GC”!
Dalvik “Stop the World GC”
This GC runs on : “Concurrent Mark-and-Sweep” (CMS) algorithm
Let’s understand this algorithm:
Step 1: Find Roots:
- The GC pauses all the threads in the system to find the roots (we will talk about the “roots” later in this course. At a very high level, for now, just think of it as a root node in a tree).
- This phase is not concurrent, so identifying roots takes time, and during this time the application cannot do anything.
- In this phase, one boolean flag “marked” is set to “false” by default for each and every object.
Step 2: Mark Reachable Objects:
- The objects which have a direct or indirect reference to the roots are marked as REACHABLE in this phase! (We’ll also talk about Object Reachability at later point of this article. Please, bear with me!
Mobile App Development Best Practices – 04.10
iOS New and Deprecated APIs in iOS 17 Abstract Class vs. Protocol-Oriented Approach in Swift Comparing the Performance of the...
New and Deprecated APIs in iOS 17
In this video, I would like to share with you some things that were either deprecated or added in iOS...
Promova helps people with dyslexia learn languages
The new Promova feature comes just in time for National Dyslexia Awareness Month and is available on the platform for...
Notify – A simple note application with modern MVVM, Compose and Material3
Notify is a simple note application that is built with Modern Android development tools. This project showcases the Good implementation...
Mobile App Development Best Practices – 03.10
iOS MetaCodable – Supercharge Swift’s Codable implementations with macros meta-programming How to build a Tuist plugin and publish it using...
How to make and use BOM (Bill of Materials) dependencies in Android projects
By using a BOM dependency, you can avoid specifying the versions of each individual library in your app, and let...