I Am a Happy Janitor – Part 1: Finding garbage


Indeed what I do is very much like the job of the janitors – like the ones who clean your building, or janitors you see at a food court, or yourself when you are taking care of garbage at your house. Doubtful you say? Let me prove it to you.

Finding garbage

What do you do when you are having lunch with your friends at a food court and suddenly you need to go somewhere for a short while in the middle of it (like, you asked for hot sauce but were given not-so-hot-sauce and you went back to the cashier to tell him how it’s so not up to your standard)? Then the nice janitor guy comes along and he sees your plate and tries to take it (you were so hungry you already ate most of your food before your remembered to use the sauce…). What happens now?

One of your friends says to the janitor guy, “my friend is not finished yet.”. The janitor then goes away without taking your plate.

Same principle for the GC, the difference is GC’s friends tell it what’s not garbage, ie, what’s live (seriously..who wants garbage…). GC’s friends include:


· EE stack walker

· Handle table

· Finalize queue

What happens is the following:

GC says: I need to start a collection on gen X…My super friends please report to me what is live now for gen X.

JIT says: Roger that. I will take care of the live roots on managed stack frames. When I jitted methods I had already built up a table that tracked which locals were alive at which point so I will report the ones that are currently live to you. I am quite aggressive at determining if something should be dead or not in retail builds – locals could be treated as dead as soon as I determine it’s not in use anymore (not guaranteed). However in debuggable builds I am much more lenient – I will let all locals live till the end of the scope to make debugging more convenient.

EE stack walker says: I can take care of the locals on the unmanaged frames when managed frames call into the EE (for lock implementation and whatnot).

Finalize queue says: Indeed, the almighty me brought back some dead objects back to life so I am telling you about these objects.

Handle table says: Reporting handles in use in gen X and all its youngest generations now.

GC waits patiently while the super friends are reporting what’s live.

The only time GC needs to participate in finding what’s live is when GC needs to find out if there are objects in older generations that are referring to objects in younger generations. For example if GC is collecting gen 1, it will need to go find out if there are any gen2 objects that are referring to gen1 and gen0.


Discussion is closed.

Feedback usabilla icon