Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

A Garbage Collection Story

Glimpsing into the life of Bill — the Android garbage collector

 

 
Denis Crăciunescu
I write about things I’m learning — Android Developer @Nagarro
Published: February 02, 2021
Tweet
Share
 

Bill — the garbage collector

 

Introduction

Imagine a populated parking lot.

 

A parking lot, with both parked cars and free parking spots

The Android Parking Lot

 

Cars are coming in, cars are going out. 
Some cars might occupy one parking spot. Whereas the bigger ones might occupy the whole parking lot. 
People are generally mad here.

This is the memory.

And then there’s Recycler Bill, the garbage collector. His only purpose in life is to gather the trash from the parking lot.

When they leave, people put their trash in a sterile bag and leave it on their parking spot for Bill to collect.

Now, Bill has a problem — some people do not pack their trash in a bag. Instead, they leave a pile of trash on the parking spot.

Unfortunately, Bill cannot collect the pile of trash as he is not paid enough for this job. The trash would then occupy the parking spots indefinitely.

These are memory leaks.

 

A parking lot, having both cars and trash bags on the parking spots

One memory leak in the second row, the second parking spot

 

When a new car tries to park here, and there’s not enough space for it… the parking lot explodes, and Bill explodes as well.

This is an OutOfMemoryException.

Bill is more than a mere garbage collector. In his hands lies the fate of the parking lot.

Let’s follow Recycler Bill in his journey of becoming the best garbage collector in the parking lot. 
Oh, but don’t tell him he’s the only one around. 😁

 

Dalvik Bill (up until Android 4.4)

Things are not going that well for the parking lot.

It’s quite expensive to park here. So, it is recommended to park only if necessary.

Moreover, Bill irritates the people as everyone has to stop during some parts of the garbage collection. No cars are entering or leaving the parking lot until Bill finishes cleaning.

So how did Bill get the job with all these inconveniences?

The job of a garbage collector seems straightforward. However, the intricate technique of Bill made him stand out from the other garbage collectors. He calls his technique Mark and Sweep.

To begin with, Bill noticed that it is not that effective to collect garbage that often. Instead, he waits until the parking lot is full. Only then, he proceeds with the garbage collection.

Bill is collecting the garbage in two steps:

  1. He traverses the parking lot, marking the parking spots that the cars occupy. This is the step in which he pauses all the traffic inside the parking lot.
  2. Knowing which spots the cars occupy, he sweeps the unmarked parking spots, which contain the packed trash. He doesn’t touch the unpacked trash as it is still above his pay grade.

This seems to be working for the parking lot, but Bill remarks a new problem.

After he collects the garbage, there are a lot of gaps left in the parking lot. What if the military comes and requests a big contiguous array of parking spots for their tanks?

This problem is heap fragmentation.

Some time passes and Bill comes up with a solution.

At night time, when there is not much traffic in the parking lot, he would lift all the cars with his super-hero strength and realign them so that it creates a contiguous space of free parking spots.

This is heap compaction when the application is in the background.

 

A parking lot, having all cars on the first row, and empty parking spots on the second row

Compacted parking lot

 

But still, if the military came during day time, the parking lot would most likely explode.

 

ART Bill

Throughout the years, Bill has improved his technique.

Now, Bill has received a promotion and he can realign the parking lot during the day time as well. The military can come now with the tanks since Bill can accommodate that.

This is heap compaction even if the application is in the foreground.

The parking lot has advanced as well. The price has gone down and now people are less reticent in parking here.

The parking lot does not pause anymore when Bill marks the occupied spots. The overall garbage collection process is 3x times faster now.

Moreover, the digitalisation of the parking lot made it a lot easier for Bill to track for how much time did each car occupy their parking spot. With this knowledge, Bill now groups the cars in generations.

There are three heap generations:

  • Young generation (which is further divided into three containers — Eden, S0, S1)
  • Old generation
  • Permanent generation

When a car parks for long enough, Bill moves the car to the old generation, followed by the permanent generation.

Each generation has its own dedicated upper limit on the space the parked cars can occupy there. If a generation reaches its limit, Bill starts the garbage collection for that generation only.

 

Conclusion

You have seen how Recycler Bill has evolved from a simple garbage collector to Bill — the Garbage-Man!

However, he is yet to achieve his maximum potential.

Until then, if you wish to dig deeper into the subject, check out the following analogy to the life of Bill:

 

 

 

 

If you found the information useful, don’t forget to 👏!

Until next time! 😀

 

 

Tags: Android, Programming, Technology, Garbage Collection, Memory Management

 

View original article at: 


 

Originally published: January 28, 2021

Android News
Our Engineering Roadmap
Our Engineering Roadmap

By Mark Ng

We just completed our engineering road map for our Android apps at Australia Post. Each year we get together and try to decide on what we are going to do from an engineering perspective for the next 12 months. Each team gets to decide on what should be done now, what they want to complete by the end of the year and whats on the horizon for next year.

By ProAndroidDev -
Android News
Android Activity Lifecycle considered harmful
Android Activity Lifecycle considered harmful

By Eric Silverberg

The Android activity lifecycle is one of the first architectural concepts learned by a new Android developer. Ask any Android dev and they’ll likely all have seen this handy diagram from Google: 

By ProAndroidDev -
Android News
Our Safe Approach to Android Jetpack Navigation in a Multi-Modular App
Our Safe Approach to Android Jetpack Navigation in a Multi-Modular App

By Alejandro Weichandt

It has been a year since we started working on the Android version of the mobile app at Sync. During that year, we faced more than once that moment when we had to choose which path to follow on an Architectural decision. This story is about Navigation.

By ProAndroidDev -
Android News
Custom KotlinX Serializers
Custom KotlinX Serializers

By Jobin Lawrance

Let’s say we have a third-party class that we are using as a type in one of our data class that we want to be serialized, then we have to write a custom serializable for @Serializable to work.

 

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One