This is the sixth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
preface
Any program needs to allocate memory to run, such as we run a web page. If some of the allocated memory can’t timely release, is called a memory leak, when there is a lot of memory leaks, is out of memory, memory is simply the memory we use is greater than the system can provide space for memory, the memory of direct phenomenon is page card, more serious is the collapse of the page. This results in poor user experience and user loss.
In this article you will learn:
- Js memory allocation
- Garbage collection mechanism
- Garbage Collection Policy
- V8 memory management
- V8’s recycling strategy
Life cycle of memory
Regardless of the programming language, the memory lifecycle is basically the same:
JavaScript memory allocation
JS data types are divided into two types, one is the original data type, including string, number, Boolean, NULL, undefined, sybmol, bigint. The other is the reference data type Object. Different data types are stored differently in memory. Memory is divided into three types: stack memory, heap memory and code space.
The bottom of the bucket is equivalent to the bottom of the stack. The water in the bucket is equivalent to the top of the stack. The water in the bucket enters from the top of the stack and goes out from the top of the stack. Let’s create a variable, for example var a = 10; In memory, you put 10 in memory, you put a in memory, and then you associate a with 10. The reference data type, the size is not fixed, stored in the heap, such as an object var obj = {}, first in the heap will open up a piece of memory to store the value of the object, and then generate a hexadecimal address code, finally put the address code in the stack, and put obj, so that the address code and obj associated.
Garbage collection mechanism
Find the variables that are no longer in use, and reclaim the memory they occupy. The garbage collector will collect the variables once in a while, and the collection time varies between engines. Js uses garbage collection to automatically manage memory, but garbage collection is a double-edged sword.
Garbage collection Policy
V8 memory management
The first thing to know about V8 is that it has a memory limit, and you might be wondering why does V8 have a memory limit?
What about the V8 recycling test? In fact, V8 adopts the generational recycling strategy, which divides the memory into the new generation and old generation. V8 uses different garbage collection algorithms for new and old generations to improve efficiency.
conclusion
Memory management storage is more abstract, but is closely related to our daily research and development, understand the management of memory, the daily processing of memory problems will be more handy. I hope it helps you.
If you think it’s good, give it a like.
reference
-
Developer.mozilla.org/zh-CN/docs/…
-
note.youdao.com/s/X8tnIW5R