4. Languages and runtimesGarbage collection
Garbage collection
Garbage collection is the language runtime automatically finding memory your program can no longer reach and freeing it, so you don’t have to. A tracing collector starts from the roots, meaning globals and whatever is on each thread’s stack, follows every pointer to mark what’s still reachable, and reclaims the rest. Many collectors, such as Java’s and .NET’s, are generational: since most objects die young, they sweep a small nursery of new objects often and cheaply, and move survivors to an older area checked less often. The price is pauses and extra CPU, which modern collectors shrink by working alongside your program. Java, Go, C# and JavaScript use one; C++ and Rust don’t.
In this lesson: Mark-and-sweep over an object graph. A generational heap with promotion.