1 reference counting garbage collection
Core: the number of times the object is referenced. The idea is that if an object A is assigned A value to the variable v, the reference count value of the object A is increased by 1. If the variable v is assigned another value, for example, if a = "str", the reference count of the object A is reduced by 1. when the reference count value is 0, it indicates that the memory space occupied by it can be recycled.
1 var A = {B: 4 };
2 var v = A; // The reference count of A is 1 at this time.
3 var vv = A; // The reference count of A is 2 at this time.
4 v = 9; // The reference count of A is 1
5 vv = "hah"; // The reference count of A is 0. When the Garbage Collector runs next time, A is recycled.
If a circular reference occurs:
1 var A = {B: 4, c: null };
2 var B = {a: 4, c: null };
3
4 A. c = B; // The reference count of B is 1.
5 B. c = A; // The reference count of A is 1. At this time, they reference each other and will not be recycled.
2 Mark Clear
Core: When a variable enters the execution environment, it is marked as "entering the environment", but when the variable leaves the environment, it is marked as "leaving the environment". When the Garbage Collector encounters a variable marked as "out of the environment", it recycles the memory space they occupy.
1 function (){
2 var a = 12; // when entering function A, the pre-resolution declares a first and marks it as "entering the environment"
3}
4 // After function A is executed, function a is marked as "leaving the environment".