项目作者: sureshalagarsamy

项目描述 :
JavaScript Garbage Collection
高级语言:
项目地址: git://github.com/sureshalagarsamy/javascript-garbage-collection.git


JavaScript 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 JavaScript engine discovers that and cleans up?

Reachability

The main concept of memory management in JavaScript is reachability.

“reachable” values are those that are accessible or useable somehow. They are guaranteed to be stored in memory.

reachable values cannot be deleted for obvious reasons.

  • Local variables and parameters of the current function.
  • Global variables.

These values are called roots.

There’s a background process in the JavaScript engine that is called garbage collector. It monitors all objects and removes those that became unreachable.

A simple example

  1. // user has a reference to the object
  2. var user = {
  3. name: "John"
  4. };

image

Here the arrow shows an object reference. The global variable “user” references the object {name: "John"}

The “name” property of John stores a primitive, so it’s painted inside the object.

If the value of user is overwritten, the reference is lost:

  1. user = null;

image

Now John becomes unreachable. There’s no way to access it, no references to it. Garbage collector will junk the data and free the memory.

Summary

  • Garbage collection is performed automatically. We cannot force or prevent it.
  • Objects are retained in memory while they are reachable.

Modern engines implement advanced algorithms of garbage collection.