Posts

Showing posts from September, 2018

Garbage collection

Image
Garbage collection Memory management in JavaScript is performed automatically and invisibly to us. We create primitives, objects, functions… All that takes memory. What happens when something is not needed any more? How does the JavaScript engine discover it and clean it up? Reachability The main concept of memory management in JavaScript is  reachability . Simply put, “reachable” values are those that are accessible or usable somehow. They are guaranteed to be stored in memory. There’s a base set of inherently reachable values, that cannot be deleted for obvious reasons. For instance: Local variables and parameters of the current function. Variables and parameters for other functions on the current chain of nested calls. Global variables. (there are some other, internal ones as well) These values are called  roots . Any other value is considered reachable if it’s reachable from a root by a reference or by a chain of references. For instance, if ther