1 Reference count Garbage collection
Core: The number of times the object is referenced by a trace record. The idea is that if an object A is assigned a variable V, the reference count value of the object A is plus 1, and if the variable v is given another value, such as a= "str", the reference count value of the object A is reduced by 1. When this reference count becomes 0 o'clock, it indicates that the memory space it occupies can be recycled.
1 var a={b:4};
2 Var v=a;//at this time a reference count of 1
3 Var vv=a; At this point A's reference count value is 2
4 v=9;//a Reference count value is 1
5 vv= "hah"; the reference count value of//a is 0, and when the garbage collector next runs, A is recycled
If a circular reference appears:
1 var a={b:4,c:null};
2 var b={a:4,c:null};
3
4 a.c=b; B's Reference count value is 1
The reference count of 5 b.c=a;//a is 1, at which point they reference each other and are not recycled
2 Mark Purge
Core: When the variable enters the execution environment, it is marked as "Enter the environment", but when the variable leaves the environment, it is marked "Leave the environment". When the garbage collector encounters variables marked "Leave the environment", it reclaims the memory space they occupy.
1 function A () {
2 Var a=12; When you enter function A, the pre-resolution declares a first and is marked as "entering the environment"
5;
4//After function A has been executed, a is marked "Leave the Environment"