Javascript basics: garbage collection mechanism

Javascript garbage collection mechanism

Insert picture description here

1. What is a memory leak?

The operation of the program requires memory. When the program requests it, the operating system will provide the memory. If the memory that is no longer used is not released in time, it is called a memory leak .

For the continuously running service process, the memory must be released in time, otherwise, the memory usage will become higher and higher, which will affect the performance of the system, and cause the process to crash.

2. What is the garbage collection mechanism?

JavaScript has a garbage collector, and the garbage collector will be executed periodically at fixed intervals.

There are two most common garbage collection methods:

  • Mark clear
  • Reference count

1. Mark removal:

Principle: When a variable enters the environment, mark this variable as "enter environment". When a variable leaves the environment, it is marked as "out of the environment". The memory marked "Leave the environment" is reclaimed.

  1. The garbage collector will mark all variables stored in memory when it is running.
  2. Remove the tags of the variables in the environment and the variables referenced by the variables in the environment.
  3. In addition, the marked variables will be regarded as variables to be deleted.
  4. The garbage collector completes the memory cleanup, destroys those marked values ​​and reclaims the memory space they occupy.

2. Reference counting

Principle: Keep track of the number of times each value is referenced.

  1. A variable is declared and a value of a reference type is assigned to this variable. The number of references to the value of this reference type is 1.
  2. The same value is assigned to another variable, and the number of references to this reference type value is increased by 1.
  3. When the variable containing the value of this reference type is assigned to another value, the number of references to the value of this reference type is reduced by one.
  4. When the number of references becomes 0, it means that there is no way to access this value.
  5. When the garbage collector runs next time, it will release the memory occupied by the value of 0 references.

(Note: The memory cannot be released when it is circularly referenced.)

3. How to observe memory leaks?

If after five consecutive garbage collections, the memory usage becomes larger than once, there will be a memory leak. This requires real-time viewing of memory usage.

  1. You can check the memory usage in the browser.
  2. Through the command line, the command line can use process.memoryUsagethe methods provided by Node . process.memoryUsageReturn an object that contains the memory usage information of the Node process. (To judge the memory leak, the heapUsedfield shall prevail.)

Four, summary

The memory is not released or released in time will cause memory leaks.

The common methods of garbage collection are mark sweeping and reference counting.

You can view memory leaks through the browser and the command line.

Guess you like

Origin blog.csdn.net/imagine_tion/article/details/112464720