Android / Memory Management

A Garbage Collection Story

Glimpsing into the life of Bill — the Android garbage collector

Denis Crăciunescu
ProAndroidDev
Published in
4 min readJan 28, 2021

--

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.

--

--