Garbage collection
How a language runtime finds the objects a program can no longer reach and takes their memory back. Eight objects, three roots, one mark-and-sweep pass, then generations and pauses.
In 30 seconds
A garbage collector frees memory for you. Instead of asking the programmer to delete each object, it works out which objects the program can no longer reach and reclaims their space.
It starts from the roots, the variables in use right now, and follows every reference. Everything it reaches is marked as live; everything else is swept away.
Because most objects are short-lived, many collectors check new objects often and old ones rarely. The price of all this is that the program is sometimes paused while the collector works.
Garbage collection, step by step
This is plain mark and sweep. Real collectors (in Java, Go, JavaScript, C#) add refinements such as moving objects to close gaps. Some languages use reference counting instead, or alongside.
- Step 1: Objects fill the heap. A running program keeps creating objects, and each one takes a piece of memory called the heap. Variables hold references (arrows) to objects, and objects refer to each other. Memory is finite, so space that is no longer needed has to be given back somehow.
- Step 2: Roots and reachability. The roots are the places a program can touch directly: local variables of running functions, and globals. An object is live if you can get to it from a root by following references. Right now every object is: G, for example, is reached by user → A → F → G.
- Step 3: Garbage appears. Two ordinary things happen. The function that owned tmp returns, so that root vanishes and H is orphaned. And the program sets user.draft = null, cutting the arrow from A to F. No path from any root leads to F, G or H now. The program can never use them again: they are garbage.
- Step 4: Mark. A garbage collector reclaims memory automatically, and mark and sweep is the classic way. In the mark phase it starts at every root and follows each reference, setting a mark on every object it reaches. Here it marks A, B, C, then D, E. It never arrives at F, G or H.
- Step 5: Sweep. In the sweep phase the collector goes through the whole heap. Any object without a mark is freed, and its space becomes available for new objects. F and G go even though they refer to each other: what counts is reachability from a root, not whether something points at you.
- Step 6: Generational collection. In most programs the bulk of objects are short-lived: a temporary string, an intermediate result. Generational collectors exploit this. New objects go in a small young area that is collected often and quickly, because few survive. Objects that keep surviving are moved to an old area, which is collected far less often.
- Step 7: Pauses. Collection is not free. The simplest collectors stop the program completely while they mark and sweep: a “stop the world” pause, which can show up as a stutter. Generational collectors replace one long pause with many short ones, and concurrent collectors do most of their marking while the program keeps running.
Try it yourself
Learn this first
Spotted a mistake on this page?