This is the 82nd unwatered original, want to get more original good articles, please search the public number to pay attention to us ~ this article first in the political cloud front blog: V8 engine garbage collection and memory allocation

Writing in the front

To do a good job, must first sharpen its device, the device of this article is not the appliance of the device, is also the container, come to the point, as a front end workers, left just const definition constant, loyalty, turn on a few new objects, play hot, is really a good JSER, coquetty operation behind, there will be day and night ceaseless QWER, Plus A walk A, without further ado, what is the browser kernel? Still don’t know what browser kernel? Let’s start with the browser kernel.

Browser kernel

Blink, Weikit, Gecko and Trident are just some of the components of each browser kernel.

The browser Rendering engine Javascript engine
Chrome Blink(used Safari Webkit 13 years ago, Blink was created by Google and Opone) V8
Safari Webkit JavaScriptCore
Firefox Gecko SpiderMonkey–OdinMonkey
IE Trident Chakra

The rendering engine and THE JS engine work together to create the page that the browser displays, as shown below:

Simple look at the line, not important, since it is about Garbage Collection (GC), it must first go to the recycling station, recycling station has a scientific name: memory, one of the computer five hardware memory core, see the following picture:

More importantly, JS is not capable of managing memory and garbage collection, everything depends on each browser’s JS engine, so in order to force a higher point, let’s not say JS garbage collection, you see, I say V8 garbage collection, is not much more powerful (touch the more and more no resistance to the head).

Memory allocation

The stack

Simply put, stack memory, small and continuous storage, easy to operate, generally automatically allocated by the system, automatic recovery, so the article mentioned garbage collection, are based on heap memory.

The heap

Heap memory, large (relative to the stack) and discontinuous.

Memory classification in V8

Before we get into memory allocation, let’s take a look at the weak generational hypothesis, on which V8’s garbage collection is based.

Concept:

  • The vast majority of objects have a short lifetime, that is, a short lifetime
  • Objects with a long life cycle are basically resident objects

Based on the above two concepts, memory is divided into two regions: ** New space and ** old space. Underline it. Write it down.

The garbage collection

The new generation

New generation (16MB for 32-bit systems, 32MB for 64-bit systems, different browsers, but not much different).

The new generation corresponds to the hypothesis that the survival time is very short, the operation of this space is very frequent, the vast majority of objects here experience a cycle of life and death, basically die out, and those who do not die will be promoted to the old generation.

Avenge the algorithm for Scavenge, typically sacrifice space for time. First he divides the generation into two equal semispace, from space and to space, and here’s how this loser works. He uses a width first algorithm, width first, remember no. Between two Spaces, only one will be working from space and the other will be resting to space at the same time.

  1. First, the garbage collector in the V8 engine detects that the from space space is running out of space, and it’s time for a garbage collection

  2. Then, starting at the root, the unreachable objects (that is, objects that cannot be traversed) will be marked, and the unmarked objects will be copied and placed in to space

  3. Finally, the data in “From Space” is cleared, and at the same time, “from space” is set to idle state, that is, it becomes “to space”, and the corresponding “to space” becomes “from space”, commonly known as “flip”

Also, you said that the space is given to him, he like how to deal with how to deal with it, it is impossible to force President Wang to open second-hand Otto, of course, for small objects, so once, the advantage of time that is leverage, although wasted half of the space, but the problem is not big, can hold.

Of course, a good V8 is impossible to tolerate, an object to bounce back and forth between form space and to space, after a form => to reversal, found some unmarked object is still in, will be thrown directly into the old generation, like after the wave to participate in the competition, to qualify, excellent.

In addition to the above situation, there is another situation where an object will be promoted. When an object is copied, it will also be promoted if it is more than 25% of the “to space” space.

The old generation

Old generation (32-bit operating systems allocate about 700MB of memory space, 64-bit double 1.4g, again, each browser may differ, but not by much).

The old generation is much more complicated than the new generation. As the saying goes, the able ones work harder, and the space is bigger, the responsibility is bigger. The old generation can be divided into the following areas:

  • ** Old object space **old object space **
  • ** Large object space ** Large object space ** Large object space ** Large object space ** Large object space ** Large object space ** Large object space ** Large object space ** Large object space ** Large object space ** Large object space It’s basically just waiting for fate to happen here and it’s impossible to accept just knowing what it is and not knowing why
  • **Map space ** this is a hidden class that stores the mapping between objects. Don’t tell you (don’t know the big guy has gone to Baidu)
  • **code space ** Code space ** Code space ** Code space

Take a look at this and take a break:

With so many basic concepts out of the way, let’s talk about the final old sweeping algorithm, which is mark-sweep/ Mark-compact.

In the process of marking, the concept of three-color marking is introduced. The three colors are:

  • White: Unmarked objects, that is, unreachable objects (objects not scanned), can be reclaimed
  • Grey: A marked object (reachable object), but the object has not been scanned and is not recyclable
  • Black: the object has been scanned (reachable object) and cannot be recycled

Of course, to mark, you need to provide the pits for the record, and a marking bitmap pit is created for each memory page allocated in V8.

The general process is as follows:

  1. Will first of all the root object all tags for white, and then use the depth-first traversal, is depth first ha, and is not the same as the new generation, according to the traverse along the depth first search, access to the object, press directly into the stack, at the same time the tag results on marking bitmap (gray), an object traversal, directly out of the stack, Meanwhile, record it as black in marking bitmap until the stack is empty. Take a picture and have a rest

  1. After the completion of the tag, the next is to wait for the garbage collector to clean up, clean up, will leave a lot of discontinuous space in the original memory area, small object is good, this time if a slightly larger object, no memory can put the next stupid, how to do? It can only trigger GC, but all the discontinuous space that was cleared can be added up again. Unfortunately, starting a GC will also drop performance. V8 allowed this to happen? It certainly doesn’t exist!

  2. So clear, new generation of object, again assigned to the old belt and insufficient memory, trigger priority tag (mark – compact), at the end of the tag, he would to object (black), moved to the other side of the memory of the other memory space is not occupied, direct release, next time another object such as a promotion, Relax.

See here each big guy may have a question, that if I finish GC, again come an object, full how to do, you say how to do, direct breakdown is good, this time need big guy when writing code, want to cherish memory, to memory like cherish your girlfriend, what? No girlfriend? Then there is nothing to be done. In principle, this problem will never be solved.

Basic memory and garbage collection is finished, there are some concepts, or to say, then look down!

Write barriers

Think of a problem. When GC wants to recycle content from the new generation, some object has only one pointer to it. Unfortunately, the pointer is pointed to by the old generation object. I want to recycle this thing, do I have to go through the old generation of objects? Isn’t this a joke? In order to recycle this one thing, I need to go through the whole old generation, which is too expensive, can’t afford to do, can’t afford to do, what then?

V8 has a concept called the write barrier, where objects are written to a cache list of all objects that the old generation points to the new generation. Of course, new objects are not recorded, only old generation objects that point to the new generation are written to the cache list.

When a new generation GC encounters an object like this, it will first read the cache list, which is much less expensive than traversing all the objects in the old generation. This is worth a wave of 666, which is excellent, of course, there is much more about the optimization inherent in the V8 engine that you can learn about slowly.

Stop-the-world

There is no need to tell the full pause alone, but I am happy and good.

In the past, both new and old bands have been included. To ensure that the logic is not consistent with the garbage collection situation, you need to stop the JS running, specifically to iterate to iterate/copy, mark/clear, and this pause is called the full pause.

This is more disgusting, the new generation even if, its memory is not large, the time is not obvious, but in the old generation, if the object traversal is too much, too big, the user at this time, it is possible to obviously feel the page card, experience gaga poor.

So in the V8 engine project called Orinoco, three things are done, of course, only for the old generation, the new generation is still ok, the efficiency is high, there is not much room for optimization. The three things are:

  • Incremental tag

When the memory usage reaches a certain amount or enters the write barrier for many times, we will temporarily stop the JS program and do marking for dozens of milliseconds at most. When the next GC, anyway, we will start to clear the previous mark

  • Parallel recovery

Parallelization literally means that in the process of a full garbage collection, the V8 engine can significantly reduce the garbage collection time by opening several helper threads to clean up the garbage together

  • Concurrent collector

Concurrency is when the main JS thread is running, and the helper thread is running at the same time, to clean up the garbage that has no logical relationship with the main thread, of course, need to write barrier to ensure

summary

V8 engines do optimization are many, and as many times (2 times) to survive in the Cenozoic object, will be recorded, at the time of the next GC, will be promoted straight to the old generation, and new promotion object, such as direct tag is black, this is because the object of the new promotion probability of survival is very high, Even if these two cases are no longer used, they will be cleared the next time, which has little impact. But in this process, the first one saves a copy cycle in the new generation, and the second one saves the process of marking. In the case of more such objects, it is still relatively advantageous.

The last sentence

Finally, finished, original thinking of writing more detail, but the space will be very big, next time, you have the chance to write about the process or V8 V8 performs to create objects are done what what what thing, actually V8 engine (or each JS engine) this thing is too huge, I know is the tip of the iceberg, Therefore, there must be some inaccuracies in the article. Welcome to make corrections and actively communicate.

Recommended reading

How do junior engineers grow quickly and seek breakthroughs

Some solutions of NPM private library from setup to data migration and finally disaster recovery backup

, recruiting

ZooTeam (ZooTeam), a young and creative team, belongs to the product RESEARCH and development department of ZooTeam, based in picturesque Hangzhou. The team now has more than 40 front end partners, the average age of 27 years old, nearly 30% are full stack engineers, no problem youth storm team. The membership consists of “old” soldiers from Alibaba and netease, as well as fresh graduates from Zhejiang University, University of Science and Technology of China, Hangzhou Electric And other universities. In addition to the daily business connection, the team also carried out technical exploration and actual practice in the direction of material system, engineering platform, building platform, performance experience, cloud application, data analysis and visualization, promoted and implemented a series of internal technical products, and continued to explore the new boundary of the front-end technology system.

If you want to change the things you’ve been doing, you want to start doing things. If you want to change, you’ve been told you need to think more, but you can’t change; If you want to change, you have the power to achieve that result, but you are not needed; If you want to change what you want to accomplish, you need a team to support you, but there is no place for you to bring people; If you want a change of pace, it’s “3 years of experience in 5 years”; If you want to change the original savvy is good, but there is always a layer of fuzzy window paper… If you believe in the power of belief, that ordinary people can achieve extraordinary things, that you can meet a better version of yourself. If you want to get involved in the growth of a front end team with a deep understanding of the business, a sound technology system, technology that creates value, and spillover impact as the business takes off, I think we should talk about it. Any time, waiting for you to write something, to [email protected]