In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to understand the garbage collection and memory limitations of V8 in the front end of web. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Preface
In the third browser War, the Chrome browser from Google became the focus of the spotlight because of its excellent performance. The success of Chrome is inseparable from the JavaScript engine V8 standing behind it.
With the advent of V8, JavaScript has completely shrugged off its image of poor performance as a scripting language. The excellent performance of V8 makes JavaScript appear on the stage of high-performance background service development. It is precisely because of this opportunity that in 2009, Node founder Ryan Dahl chose V8 as Node's JavaScript scripting engine. Node is implemented under the design of event-driven and non-blocking Ithumb O model.
However, it is important to understand that although Node benefits from V8 in the implementation of JavaScript and greatly broadens the application scenario of JavaScript from the client to the server, it is also limited by v8. For performance-sensitive server programs, memory management and garbage collection will affect the composition of the service, and these have a lot to do with v8.
Memory limit for V8
In Node, if you use memory operation through JavaScript, you will find that you can only use part of the memory (about 0.7G in a 64-bit system, which is about 1.4G memory in a 32-bit system). This restriction basically does not exist for other server-side development languages.
The result of this limitation of V8 is that Node cannot directly manipulate large memory objects. In the case of a single Node process, the computer's memory resources are not fully utilized.
The reason for the problem is that Node is built on V8, so objects used in Node are allocated and managed in V8's own way.
Its memory management mechanism is not a big problem in the browser scenario, but for Node, it makes Node have such limitations.
Garbage collection algorithm of V8
The garbage collection strategy of V8 is mainly based on generational garbage collection mechanism.
In the application, the life cycle of the object varies, and different algorithms can only produce better results for specific situations. Therefore, in the modern garbage collection algorithm, the garbage collection of memory is divided into different generations according to the survival time of the object, and then a more efficient algorithm is applied to different generations of memory.
In V8, all JavaScript objects are allocated through the heap. We can see the heap usage through the heapTotal and heapUsed returned by the process.memoryUsage () method, the former in terms of the requested heap memory and the latter as the amount currently used. If the object used in the code uses more space than the requested space, the heap memory will continue to be applied for until the heap size exceeds the V8 limit.
In V8, the heap is mainly divided into two regions: the Cenozoic era and the old generation. The objects with short survival time are stored in the Cenozoic era, and the objects with long survival time are stored in the old generation.
The new zone usually only supports a capacity of 1m to 8m, while the old zone supports much larger capacity. For these two areas, V8 uses two different garbage collectors to implement garbage collection more efficiently.
Secondary garbage collector, mainly responsible for the new generation of garbage collection.
The main garbage collector is mainly responsible for garbage collection of the old generation.
No matter what type of garbage collector they are, they all have a common execution process.
1. The first step is to mark active and inactive objects in space. The so-called active object is the object still in use, and the inactive object is the object that can be garbage collected.
two。 The second step is to reclaim the memory occupied by inactive objects. In fact, after all the tags are completed, uniformly clean up all the objects in memory that are marked as recyclable.
3. The third step is to do memory consolidation. Generally speaking, after frequently recycling objects, there will be a lot of discontiguous space in memory, which we call memory fragmentation. When there are a large number of memory fragments in memory, it is possible to run out of memory if you need to allocate large contiguous memory. So the last step is to defragment the memory. This step is actually optional because some garbage collectors do not produce memory fragments.
Garbage Recycling in the New Generation
In the new generation, the Scavenge algorithm is used to divide the space of the new generation into two regions, half of which are object regions and half of which are idle regions. Newly added objects are stored in the object area, and when the object area is almost full, a garbage cleanup operation is required.
In the process of garbage collection, we should first mark the garbage in the object area; after the marking is completed, we will enter the garbage cleaning phase, and the sub-garbage collector will copy these living objects into the free area. at the same time, it will also arrange these objects in an orderly manner, so this replication process is equivalent to the completion of the memory demarcation operation, and there will be no memory fragmentation in the free area after replication.
After the copy is completed, the role of the object area is flipped with the idle area, that is, the original object area becomes the free area, and the original idle area becomes the object area. This completes the collection of garbage objects, and this role flipping operation allows these two areas to be reused indefinitely in the new generation.
In order to implement efficiency, the space of the new area is generally set to be relatively small, and it is precisely because the space of the new area is not large, so it is easy to fill the whole area with living objects. To solve this problem, the JavaScript engine uses an object promotion strategy, that is, objects that are still alive after two garbage collections are moved to the old zone.
Garbage collection in the old generation
In the old age, the mark-erase (Mark-Sweep) algorithm was used to deal with it. The first is the marking process stage, which starts with a set of root elements and recursively traverses this set of root elements (traversing the call stack). In this traversal process, the elements that can be reached are called active objects, and the elements that do not arrive can be judged as junk data. It is then marked during traversal, and the cleanup process is carried out after the tag is complete. It is completely different from the garbage removal process of the secondary garbage collector, which deletes the tag data.
After clearing the algorithm, a large number of discontiguous memory fragments are generated. Too much fragmentation will cause large objects not to be allocated enough contiguous memory, which leads to the tag-demarcation (Mark-Compact) algorithm, which is still the same as in the tag-cleanup algorithm, but the next step is not to clean up the recyclable objects directly, but to move all the surviving objects to one end, and then directly clean up the memory beyond the end boundary. This allows surviving objects to occupy continuous blocks of memory.
Incremental marking algorithm and full pause
Because JavaScript runs on the main thread, once the garbage collection algorithm is executed, you need to pause the executing JavaScript script and resume the script execution after the garbage collection is complete. We call this behavior a complete pause.
In the garbage collection of the new generation of V8, because of its small space and few living objects, the impact of full pause is not great, but the old generation is different. If the main thread takes too long in the process of garbage collection, the main thread cannot do anything else. For example, the page is performing a JavaScript animation, because the garbage collector is working, the animation will not be performed in the garbage collection process, which will cause stutters on the page.
In order to reduce the stutter caused by garbage collection in the old generation, V8 divides the marking process into sub-marking processes, and allows garbage collection marking and JavaScript application logic to proceed alternately until the marking phase is completed, we call this algorithm incremental marking (Incremental Marking) algorithm.
Using the incremental marking algorithm, a complete garbage collection task can be divided into many small tasks, which take a short time to execute and can be interspersed with other JavaScript tasks, so that when performing the above animation effects, users will not feel stuttered because of the garbage collection task.
This is how to understand the garbage collection and memory limitations of V8 in the front end of web. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.