Javascript garbage collection and memory management

Garbage Collection

javascript different from c, c ++ is a feature: automatic garbage collection mechanism, which means that developers can focus on business without having to put too much focus on memory management, improve development efficiency.
The so-called garbage collection is 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.
Currently in the browser, to achieve waste recycling There are two main ways:
1. Mark Clear
is the most common way garbage collection javascript.
There are two concepts in a clear manner marked: "into the environment" and "leave the environment." "Into the environment" refers to the execution of variables into the environment. "Leave the environment" refers to the variable to complete the task, leaving the execution environment.
All variables garbage collector running time will be stored in memory are marked. Then, it will remove the tag and environment variables referenced variable environment variables. And then after that variable is marked will be considered ready to delete the variable, because the environment variables have been unable to access these variables. Finally, the garbage collector memory to complete the cleanup, the destruction of the value of those tagged and recovery of memory space they occupy.
2. The reference count
meanings reference count the number of times each track record value is referenced. 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.
In fact, this mechanism is not commonly used in the js, because this mechanism can cause problems with circular references, "circular reference" refers to the object A contains a pointer to a pointer to the object B, and object B also contains a pointer to the object A references. Automatic recovery mechanisms for language like js class, the need for additional manual to release the memory, is not friendly. For example, in the following examples:

function garbage(){
    var A = new Object();
    var B = new Object();
    A.property = B; 
    B.property = A;
}

Since the objects A, B refer to each other, all citations is 2, up the memory are not to be automatically released.
In IE, the DOM and BOM reference count on the use of this class, after IE9, Microsoft turned to the mark clear way to manage memory, thus avoiding two ways to coexist.

Memory Management

Because browsers and other desktop programs compared to the system assigned to the browser's memory is relatively small, the main purpose of this is for security reasons, to prevent run JavaScript web exhausted all system memory and cause the system to crash. Memory limitations will not only affect memory allocated to a variable, but will also affect the call stack and the number of statements in one thread can be executed simultaneously.
Therefore, to ensure minimal memory footprint can make the page better performance. The best way to optimize memory usage, that is for the implementation of the code to save only the necessary data. Once the data is no longer useful, preferably by setting its value to release its reference to null - this procedure is called dereferencing (dereferencing). This approach applies to most attribute global variables and objects. Local variables are automatically dereferenced when they leave the execution environment.

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

However, the lifting of a reference value does not mean automatic recovery of the value of the memory occupied. Dereference true role is to get the value from the execution environment for the next run of the garbage collector when it is recovered.

summary

JavaScript 是一门具有自动垃圾收集机制的编程语言,开发人员不必关心内存分配和回收问题。可 以对 JavaScript 的垃圾收集例程作如下总结。

 value out of scope will be automatically marked as recyclable, and therefore will be deleted during garbage collection.
 "Clear mark" is the mainstream garbage collection algorithm, the idea of this algorithm is not currently used to value plus the mark, then
again reclaim their memory.
 Another garbage collection algorithm is "reference counting" idea of this algorithm is to keep track of the number of all values referenced. JavaScript
engine is currently no longer use this algorithm; but when accessing non-native JavaScript object (such as a DOM element) in IE, this
algorithm may still cause problems.
 When the code in the presence of a circular reference phenomenon, "reference counting" algorithm can cause problems.
 lift variable references not only help to eliminate the phenomenon of circular references, but also good for garbage collection. In order to ensure effective return to
collect memory, should be lifted global objects no longer in use, references the global object properties and variables of a circular reference.

--- Reference "JavaScript Advanced Programming 3rd Edition"

Guess you like

Origin www.cnblogs.com/jlfw/p/11962093.html