Memory Is About Reachability
JavaScript engines automatically manage memory using garbage collection. A simplified but powerful mental model is reachability: an object is collectible when it is no longer reachable from roots used by the runtime.
Creating an object does not guarantee it remains forever. Conversely, an object can become a memory leak if an application unintentionally keeps a reference to it.
Reachability Model
Key Takeaways
- Garbage collection is based on reachability rather than a simple time limit.
- Unreachable objects can eventually be reclaimed.
- A memory leak occurs when unwanted objects remain reachable.
Knowledge Check
When is an object generally eligible for garbage collection?