javascript garbage collection [revisit the basic JavaScript (c)]

Preface:

JavaScript has automatic garbage collection, garbage collection mechanism of this principle is very simple: to identify those variables no longer continue to use, and free up the memory. For this purpose, the garbage collector will follow a fixed time interval (or executed in a predetermined collection time), periodically performing this operation.

generally

Now we have to analyze the life cycle of the normal function of local variables. Local variables exist only during the execution of the function. In this process, the local variable will assign the corresponding space on the stack (or heap) memory to store their value. Then use these variables in a function until the end of the function execution.
In this case, the local variable is no longer necessary, so that they can release memory for future use. In this case, it is easy to judge whether the necessary variables still exist

But not all cases are so easy to reach a conclusion.
The garbage collector must keep track of which variables which variables useful to useless, for no longer useful variables marked mark, for future to recover its occupied memory. Strategies for identifying unwanted variables may be implementation-specific, but specific to the realization of the browser, usually there are two strategies.

Clear labeling

The most commonly used JavaScript garbage collection is labeled Clear (mark-and-sweep). When the variable into the environment (e.g., a variable declared in a function), this variable will be marked into the environment . Logically, never be released into the environment variable memory occupied, as long as the execution flow into the corresponding environment, you might use them. When leaving the environment variable, it was marked to leave the environment (the function return).

You can use any way to label variables. For example, can be recorded by flipping a particular bit when a variable into the environment, or by using a "into the environment," the list of variables and a variable "Leaving Environment" list to keep track of which variables have changed. After all, how to mark the variable is not important, the key is to take what strategy .

Look at an example

function marry(man, woman) {
  woman.husband = man;
  man.wife = woman;

  return {
    father: man,
    mother: woman,
  }
}

let family = marry({
  name: "Onion"
}, {
  name: "Garlic"
});

As shown, at this stage all objects are reachable.

Now remove some references

delete family.father
delete family.mother.husband

As shown, although there are references to Garlic Onion, but it itself is not reachable, it will soon be discovered and recovered GC.

Reference count

Another less common garbage collection strategy called the reference count (reference counting). Meaning the number of times each of the reference count value is referenced track record. When declaring a variable and a reference citations when the value is assigned to the variable type, then this value is 1. If a value the same reference number has been assigned to another variable, the value is incremented. Reference Conversely, if the value comprises a reference variable has made another value, this value is decremented by one. When this number becomes the reference value of 0, then no way to access this value, and thus can be recycled back to the memory space occupied. In this way, when the next time the garbage collector runs, it will release those citations zero value memory occupied.

This has a serious problem: circular references

function problem(){ 
 var objectA = new Object(); 
 var objectB = new Object(); 
 objectA.someOtherObject = objectB; 
 objectB.anotherObject = objectA; 
}

In this example, objectB ObjectA and refer to each other by respective attributes; that is, the number of reference objects are both 2. In the implementation of indicia clear strategy, because after the function is executed, these two objects are out of scope, so this is not a problem refer to each other. However, achieve using reference counting strategy, after the function completes, objectA and objectB will continue to exist, because their citations never be zero. If this function is repeated several times to call, it will lead to a lot of memory can not be recovered.
So this approach is no longer used in JavaScript

Memory Management

使用具备垃圾收集机制的语言编写程序,开发人员一般不必操心内存管理的问题。但是,JavaScript在进行内存管理及垃圾收集时面临的问题还是有点与众不同。其中最主要的一个问题,就是分配给 Web浏览器的可用内存数量通常要比分配给桌面应用程序的少。这样做的目的主要是出于安全方面的考虑,目的是防止运行 JavaScript 的网页耗尽全部系统内存而导致系统崩溃。内存限制问题不仅会影响给变量分配内存,同时还会影响调用栈以及在一个线程中能够同时执行的语句数量。因此,确保占用最少的内存可以让页面获得更好的性能。而优化内存占用的最佳方式,就是为执行中的代码只保存必要的数据。一旦数据不再有用,最好通过将其值设置为 null 来释放其引用——这个做法叫做解除引用(dereferencing)。

function createPerson(name){ 
 var localPerson = new Object(); 
 localPerson.name = name; 
 return localPerson; 
} 
var globalPerson = createPerson("Nicholas"); 
// 手工解除 globalPerson 的引用
globalPerson = null;

解除引用让值脱离执行环境,以便垃圾收集器下次运行时将其回收。

重温JavaScript基础(一) 基本类型和引用类型的值
重温JavaScript基础(二) 执行环境以及作用域链
重温JavaScript基础(三) 执行环境以及作用域链

参考 JavaScript高级程序设计
Javascript垃圾回收机制

Guess you like

Origin www.cnblogs.com/riwang/p/12390558.html