Garbage collection

JavaScript Garbage Collection (JavaScript垃圾回收)

In this chapter, we are going to see how JavaScript manages its memory. JavaScript is a unique language, it is capable of automatically allocating memory once objects are created and freeing it when they are not used anymore. In contrast, low-level languages require manual determination at what point in the program that allocated memory is not needed anymore and free it. So, let’s see what happens in a high-level language such as JavaScript when you don’t need anything anymore. How JavaScript engine finds it out and cleans it up.

The Primary Concept

The Primary Concept (主要概念)

Reachability is the primary concept of JavaScript memory management. The values that are accessible and usable are known as reachable. Below you will find the set of base reachable values:

  • Local variables and the current function parameters. (-局部变量和当前函数参数。)

  • Parameters and variables for other functions on the nested calls’ current chain. (-嵌套调用当前链上其他函数的参数和变量。)

  • Global variables. (全局变量)

The values above are called the roots. (上述值称为根。)

Other values are known as reachable once they are reachable from a root by a chain of references or a single reference. (一旦引用链或单个引用可以从根目录到达其他值,则称为可达值。)

Consider an object inside a local variable. If that object has a property that references another object, that object is called reachable. The references are also reachable. (考虑局部变量中的对象。如果该对象具有引用另一个对象的属性,则该对象称为可触及。这些参考文献也可以找到。)

In JavaScript, there exists a background process, called a garbage collector. It is capable of monitoring all objects and removing the ones that have become unreachable. (在JavaScript中,存在一个后台进程,称为垃圾收集器。它能够监控所有对象并移除无法访问的对象。)

For a better perception, check out the example below:

// book has a reference to the object
let book = {
 name: "Javascript"
};
console.log(book);

As you can see, the object reference is depicted by the arrow. The global variable “book” is referencing the object. The “name” property itself stores a primitive. (如您所见,对象引用由箭头描绘。全局变量“book”正在引用对象。“name”属性本身存储一个基元。)

In case the book value is overwritten, the reference will be lost, as shown below:

// book has a reference to the object
let book = {
 name: "Javascript"
};
book = null;
console.log(book);

So, the object becomes unreachable. The garbage collector junks the data, freeing the memory. (因此,对象变得无法访问。垃圾回收器会垃圾处理数据,释放内存。)

A Case of Two References

A Case of Two References (双引用案例)

In this section, let’ consider that the reference was copied from the book to language like this:

// book has a reference to the object
let book = {
 name: "Javascript"
};
let language = book;
console.log(language);

Now, doing the same will look like this:

// book has a reference to the object
let book = {
 name: "Javascript"
};
let language = book;
console.log(language);
book = null;
console.log(book);

The object will still be reachable via the language global variable. It’s in the memory. After overwriting the language, it can be deleted. (对象仍可通过语言全局变量访问。它在记忆中。覆盖语言后,可以将其删除。)

Internal Algorithms

Internal Algorithms (内部算法)

The main algorithm of the garbage collection is known as “mark-and-sweep”. Regularly some garbage collection steps are performed. Here they are:

  • The collector takes roots and marks, remembering them. (-收藏家根深蒂固,记住它们。)

  • Afterward, it visits and marks all the references from them. (-之后,它访问并标记他们的所有参考文献。)

  • The next step is visiting the marked objects, marking their references. It is not possible to visit the same object twice, as all the visited objects are remembered. (-下一步是访问标记的对象,标记它们的引用。不可能访问同一个对象两次,因为所有访问的对象都会被记住。)

  • The process goes on until every reachable reference is visited. (-该过程一直持续到访问了所有可访问的引用。)

  • All the objects except for the market objects are deleted. (-除市场对象之外的所有对象都将被删除。)

And, finally, the objects that couldn’t be visited during the process above, are considered unreachable and are going to be removed. (最后,在上述过程中无法访问的对象将被视为不可访问,并将被删除。)

So, the described process of the garbage collection works properly. But, JavaScript includes different optimizations for making them work even better and faster. Among those optimizations are Generational collection, Incremental collection, and Idle-time collection. (因此,所描述的垃圾收集过程可以正常工作。但是, JavaScript包含不同的优化,使其工作得更好、更快。这些优化包括代数收集、增量收集和空闲时间收集。)

Summary

Summary (概要)

Garbage collection is a process that is implemented automatically. It can’t be forced or prevented anyhow. (垃圾回收是一个自动实现的过程。无论如何,它不能被强迫或阻止。)

Objects can be retained in memory while they are reachable. (对象可以在可访问时保留在内存中。)

It’s essential to know that being referenced is not similar to being reachable. Advanced algorithms of garbage collection are performed by modern engines. (请务必了解,被引用与可访问性不同。 垃圾收集的高级算法由现代引擎执行。)



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部