当前位置:网站首页>Garbage collection mechanism

Garbage collection mechanism

2022-06-25 10:35:00 m0_ forty-nine million four hundred and seventy-one thousand si

The memory life cycle is basically consistent :

  1. Allocate the memory you need
  2. Use the allocated memory ( read 、 Write )
  3. Release it when it is not needed / The return

Reference resources :JS Memory management - Simple books  

JS Summary of garbage collection and memory leaks in - Play chess at leisure - Blog Garden

Scoring point Stack garbage collection 、 Garbage collection 、 New area old area 、Scavenge Algorithm 、 Mark - Clear algorithm 、 Mark - Sorting algorithm 、 A complete pause 、 Increment mark Standard answer

The browser garbage collection mechanism is divided into stack garbage collection and heap garbage collection according to the storage mode of data .

Stack garbage collection is very simple , When a function is finished ,JavaScript The engine will move down ESP To destroy the execution context stored in the stack , Follow the principle of first in, then out .

Garbage collection , When the function ends directly , Stack space processing is complete , But the data in the heap space is not referenced , But it is still stored in heap space , The garbage collector is required to recycle the garbage data in the heap space . In order to make garbage collection achieve better results , Depending on the life cycle of the object , Use different garbage collection algorithms . stay V8 It will be divided into two parts: the new generation and the old generation , In the new generation, objects with short survival time are stored , A long-lived object stored in an old generation . Used in New Area Scavenge Algorithm , Use the mark in the aged area - Clear algorithms and tags - Sorting algorithm .

The bonus answer is

Scavenge Algorithm : 1. Mark : Mark the garbage in the object area 2. Clean up garbage data and defragment memory : The secondary garbage collector copies these surviving objects into the free area , And arranged in an orderly way , After copying, there will be no memory fragmentation in the free area 3. Role flip : Once the copy is done , Object area and free area for role flipping , That is, the original object area becomes a free area , The original free area becomes the object area , This completes the garbage object recycling operation , At the same time, this kind of role reversal operation can make these two areas in the new generation infinitely reused Mark - Clear algorithm : 1. Mark : The tagging phase starts with a set of root elements , Recursively traverses this set of root elements , In this traversal , Elements that can be reached are called active objects , Elements that do not arrive can be judged as garbage data . 2. eliminate : Clean up the garbage data . 3. Generate memory fragmentation : Mark a block of memory multiple times - After clearing the algorithm , A large number of discontinuous memory fragments will be generated . Too much fragmentation will cause large objects to not be allocated enough contiguous memory . Mark - Sorting algorithm 1. Mark : And tags - The marking process is the same , Start with a set of root elements , Recursively traverses this set of root elements , In this traversal , Accessible elements are marked as active objects . 2. Arrangement : Let all live objects move to the end of memory 3. eliminate : Clean up memory outside the end boundary V8 The secondary garbage collector and the main garbage collector are used to deal with garbage collection , But due to the JavaScript It runs on the main thread , Once the garbage collection algorithm is executed , We need to JavaScript The script pauses , After garbage collection, resume script execution . We call this behavior total pause . In order to reduce the old generation of garbage recycling caused by the carton ,V8 The tagging process is divided into sub tagging processes , At the same time, let the garbage collection mark and JavaScript Apply logic alternately , Until the marking phase is complete , We call this algorithm incremental tagging (Incremental Marking) Algorithm

原网站

版权声明
本文为[m0_ forty-nine million four hundred and seventy-one thousand si]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251014391464.html