Garbage collection It's all about collecting things that are already unused, cleaning them up, and releasing the space they occupy.
JavaScript has an automatic garbage collection mechanism, which means that we don't have to think about memory when we're developing it. The allocation of memory and the memory recycling that is consumed can be managed automatically. This kind of way is still very friendly to those of me who often forget the memory. The principle of the garbage collection mechanism is to find variables that are no longer in use, freeing up the space they occupy. The garbage collector will do this for garbage at a certain time interval.
Local variables exist only when the function is executed. The execution of a function also allocates a certain amount of memory space for the local variable, storing the value for use during execution until the function execution ends. After the execution of the function, these local variables are useless, but the allocated memory space is not released. At this point, you need to release the space for future use. Therefore, it is necessary for the garbage collector to keep track of which variables are not used, marking unused variables for future recycling.
1. Mark Clear
Tag cleanup is the most common way. When we declare a variable in the function, it means that the variable has entered the environment, it can be labeled "into the environment", when the variable leaves the environment, the variable is marked "Leave the environment."
It's a bit confusing at this point, and we're going to use some way to tag the variables. For example, you can flip a particular location to record when a variable enters the environment. In fact, the use of what is not important, it is important to adopt what kind of strategy.
The garbage collector adds tags to all variables in memory at run time, then removes variables from the environment and tags of variables referenced by variables in the environment, and variables that are then tagged are the variables to be deleted. Destroy those tagged values and reclaim the space they occupy.
2. Reference counting
The reference count is the number of times each value is referenced.
When you declare a variable and then assign the value of a reference type to the variable, the number of times that is worth the reference is 1. If you assign a value to another variable, the number of references is added to 1. If another value is taken for the variable that is worth referencing, the number of references to that value is reduced by 1. When the number of references changes to 0, it is not possible to access the value, freeing up the memory space it occupies. When the garbage collector runs, the number of references is 0 of the space occupied.
3. Managing Memory
Although JavaScript has a garbage disposal mechanism, there are still problems. Say that due to security factors, the space allotted to the Web browser is assigned to the desktop program less. The main fear is that JavaScript pages run out of system memory, causing the system to collapse.
Make sure that the memory you use makes the page more performance. The best way to optimize memory is to reduce the amount of data saved. Once this data is no longer useful, you can dereference it. Set the value to NULL to release the reference. Dereferencing does not release the space that is occupied. The main purpose of the dereference is to get this value out of the execution environment, making it easier for the garbage collector to recycle space when it runs.
function Createper (name) {
var localper=new Object ();
Localper.name=name;
return localper;
}
var globalper=createper ("Rose");
Globalper=null; To dismiss a globalper reference
JavaScript Garbage Collection