SpiderMonkey Garbage Collector

SpiderMonkey Garbage Collector

The SpiderMonkey garbage collector is responsible for allocating memory representing JavaScript data structures and deallocating them when they are no longer in use. It aims to collect as much data as possible in as little time as possible. As well as JavaScript data it is also used to allocate some internal SpiderMonkey data structures.

The garbage collector is a hybrid tracing collector, and has the following features:

For an overview of garbage collection see: https://en.wikipedia.org/wiki/Tracing_garbage_collection

The GC is ‘precise’ in that it knows the layout of allocations (which is used to determine reachable children) and also the location of all stack roots. This means it does not need to resort to conservative techniques that may cause garbage to be retained unnecessarily.

Knowledge of the stack is achieved with C++ wrapper classes that must be used for stack roots and handles (pointers) to them. This is enforced by the SpiderMonkey API (which operates in terms of these types) and checked by a static analysis that reports places when unrooted GC pointers can be present when a GC could occur.

Source: HackerNews (https://news.ycombinator.com/item?id=45744395)