[JS Deep Analysis] Memory

Preface

Friends who have been exposed to the C language should understand that the C language has the most primitive memory management methods at the bottom, such as the malloc() and free() methods, which are used by developers to allocate and release memory from the operating system.

However, in the language of JavaScript, when we create something (data that can be read, written, and transferred), the js engine in the browser will allocate memory based on the data type that created the thing: the basic data type is in the stack area Allocate memory. Reference data types allocate memory in the heap area. When the created thing is no longer used, the browser will "automatically" release it. This process is called "garbage collection".

Under normal circumstances, in order to minimize the memory occupied by the program when it is running, the system will find variables that are no longer used or objects that have no reference relationships, and then release the memory they occupy; this automated memory management method greatly reduces the development time. The cost of JS memory management during the process; however, this process is not real-time because its overhead is relatively large, so the garbage collector (GC) is executed periodically at fixed time intervals.

Due to the existence of the automatic garbage collection mechanism in JS, it has given many developers the illusion that during the development process in JS, we do not need to consider the issues of memory allocation and useless memory recycling. This assumption may not have been a problem in the past, but now, with the continuous complexity of business and the development of single page applications (SPA), mobile HTML5 applications and Node.js program applications, memory problems in JS are caused Phenomenons such as lags, memory overflows, and memory leaks are no longer unfamiliar, and they also make JS memory management important.

Therefore, in the process of using JavaScript for development, understanding the JavaScript memory mechanism can help developers clearly understand what happened during the execution of the code they wrote, and can also improve the code quality of the project.

1. What does good memory management look like?

  1. JS memory usage is kept low

    If the browser's memory usage is too high, it can easily cause the browser to crash. Memory leaks prevent memory from being recycled by the browser and are also an important cause of excessive memory usage.

  2. Trigger the browser's memory recycling operation infrequently

    The browser is single-threaded. Garbage collection operations will hinder the execution of normal browser programs. Therefore, frequent triggering of GC will affect the smoothness of the page. Especially during the execution of animations, the lag will be more obvious.

2. Memory life cycle

No matter which development language is used, the life cycle of memory is roughly the same: Allocate memory(allocate memory) --> Use memory(use memory) --> Release memory(release memory)

  • Allocate memory

    When we declare variables, functions, and objects, the system will automatically allocate memory for them.

    Note: In basic languages ​​such as C, this is a precise operation that the developer handles by himself; but in high-level languages ​​such as JS and Java, it has been handled for you.

  • Use memory

    Reading and writing memory, that is, using variables, functions, etc.

  • Release memory

    If the memory is no longer needed, it is released so that the memory can be used again (as with allocating memory, this operation is performed explicitly in the base language)

Example explaining the life cycle of memory :

var a = 10;  // 在内存中给数值变量分配空间
alert(a + 20);  // 使用内存
var a = null; // 使用完毕之后,释放内存空间

Note: The main concept that garbage collection algorithms rely on is references. on memory management

Guess you like

Origin blog.csdn.net/qq_39335404/article/details/132361524