What Are mainstream Garbage Collector systems?

Source: Internet
Author: User
Tags high cpu usage
Inadvertently, JavaScriptV8 and Python use reference counting and Mark clearing as the basic method for garbage collection. Would you like to ask if there are other garbage collection algorithms? Inadvertently, JavaScript V8 and Python use reference counting and Mark clearing as the basic method for garbage collection. Would you like to ask if there are other garbage collection algorithms? Reply content: I have answered a lot before, and some of them are reliable. I feel it is quite troublesome to add them. I just want to open a recommended book list:
[Garbage Collection] [Garbage Collection] [automatic Collection of useless memory units] related books
The Garbage Collection Handbook is very eye-opening and can perfectly answer The subject's questions.

If you don't want to buy books, you can start learning from this website: Introduction to memory management.

Also welcome to the HLLVM group forum, virtual machines in advanced languages
I have posted some posts before, for example, introducing the basic implementation of copying GC: a problem with HotSpot VM Serial GC
And G1 GC implementation: [HotSpot VM] Ask about the principle of the G1 Algorithm
Comparison of several GC Types: Why is the concurrent garbage collector (CMS) not labeled?

In the discussion of reference counting and tracing GC, let's just put a portal: In the garbage collection mechanism, how does reference counting maintain all object references? -Redna xelafx

========================================================== ====================

In addition, the problem description is as follows:
Inadvertently, JavaScript V8 and Python use reference counting and Mark clearing as the basic method for garbage collection. Would you like to ask if there are other garbage collection algorithms?
(C) Python uses reference count as the master and mark-sweep as the backup.

However, V8 GC is not based solely on mark-sweep.

Initially released, V8 was relatively simple, with two generations of GC, where new space uses copying GC, and then global GC selectively uses mark-sweep or mark-compact based on fragmentation.
The abstract idea can look at this entry function: v8/mark-compact.cc at 0.1-v8/v8-GitHub

void MarkCompactCollector::CollectGarbage() {  Prepare();  MarkLiveObjects();  SweepLargeObjectSpace();  if (compacting_collection_) {    EncodeForwardingAddresses();    UpdatePointers();    RelocateObjects();    RebuildRSets();  } else {    SweepSpaces();  }  Finish();}
It feels like the Reference counting on Wikipedia And Tracing garbage collection The entry is quite good. GC can be classified in many ways:

Some GC components must traverse the graph of the objects to be GC, so as to obtain precise information about which object is alive and which object is dead. We call this GC tracing GC, which is called non-tracing GC without traversing (for example, reference counting, which can only obtain an approximate information, so it cannot process the tracing ring ).

Some GC require coworking by programmers/compilers to precisely know which pointer is an object (the object that requires GC ). Some GC programs are not required. It is difficult to guess! If you cannot guess it, you can only use it as a pointer.) GC can also be achieved. The former is called precise GC, and the latter is called conservative GC (such as Boehm GC ). We will mainly discuss precise GC below.

After some GC allocates memory, this memory may be moved to another place to prevent memory fragmentation and improve the cache locality ..), this GC is called moving GC, and a GC that does not do so is called non-moving GC. Moving GC is a tracing GC, because they must know how to traverse the graph of the objects that require GC, or they cannot be moved (after all, when moving an object, you also need to modify the value of the place where the object is stored and point to a new location ).

Some GC processes the entire object graph at a time, while some GC is optimized, and only new objects are processed in part. This optimization is based on a phenomenon: the new object is easier to point to the old object, while the old object is less to point to the new object; the new object is easier to die, objects that have lived for a long time are likely to live longer. Many programming methods will cause this phenomenon, such as immutable data structures. Then, GC can differentiate the age of objects, and divide the memory allocated areas into (larger) old areas and (smaller, for cache locality) new areas, then, based on whether the old zone is full or not, each GC determines whether GC is required only in the new GC area or all GC is required. If you only need the GC new area, when traversing the object graph, as long as you encounter an object in the old area, it will be treated as this object is live, and the subsequent figure will not need to be looked at and cut it off directly. If you encounter a new area object, you can see if you want to move it to the old area based on the number of times the object has survived GC. Of course, this is not an absolute phenomenon. It is still the case that the old object points to the new object. What should we do? This requires you to check whether the modified object is an old object every time you modify the object (this method is called GC write barrier, the modified value is not a new object. If so, you can use a method (such as remembered set and card marking) to remember the exception. In this case, GC is called generational GC.

The most common non-tracing GC method is reference counting, which can be seen in Python, Objective-C, C ++, and Rust. One of the easier-to-read implementations is (MADD libstdc ++'s shared_ptr is really ugly. If you really like C ++, protobuf/shared_ptr.h at master · google/protobuf · GitHub in protobuf Good readability) rust/rc. rs at master · rust-lang/rust · GitHub

Naive mark-and-sweep is a simple tracing GC. It needs to traverse two object graphs, the first mark and the second sweep. I used it in Scheme interpreter: overminder/sanya-c · GitHub. For details, see sgc. c. (The code still has a lot of noise .. because the stack is not completely a root set and needs to be avoided)

Cheney's semi-space copying GC It is a relatively simple kind of moving GC, that is, allocating two memory blocks of the same size. When the first block is used up, GC starts to move the living object to the second block, if you die, you will not be able to continue. I have used it in Scheme JIT: overminder/sanya-native-GitHub. For details, see gc. cpp.

I have also implemented a simple generational GC in Scheme compiler: YAC/scm_generational_gc.c at master-overminder/YAC-GitHub. . There are only two generations, and the remembered set is used. This is indeed more complex than other GC implementations. At that time, it was also a variety of segfault. It took a long time to debug Valgrind...

----------
After a modification in the evening, it should be called precise GC instead of exact GC. Added a shared_ptr implementation that can be viewed. Garbage collection algorithms can be divided into two types: one is reference counting-based reference counting method and the other is tracing-based. This type of expansion involves copy collection, Mark cleaning, and Mark compression, generation collection and later division of the entire heap in G1, and a remember set for each block for management. The specific expansion and advantages and disadvantages of these methods and The corresponding memory allocation methods can be found in The Garbage Collection Handbook In Version 2 (2011. G1 needs to search sun's papers. I have answered a similar question before. For details, refer to: Which garbage collection algorithms are used in various programming languages? What are the advantages and disadvantages of these algorithms? -Xie Zhiyi's reference count is used to determine which objects need to be recycled, while marking, copying, marking, and sorting are garbage collection methods. Mark-and-Sweep Garbage Collection
I mentioned a little on CSAPP, but I personally don't remember it... Reference count GC processes what is reference count

Reference counting is a form of garbage collection. Every object has a count to record how many references point to it. The reference count is changed in the following scenarios:

  • When an object is added with a reference, such as assigning a value to a variable, attribute, or passing in a method, the reference count is added with the 1 operation.
  • When an object reduces one reference, for example, if the variable leaves the scope, the attribute is assigned as another object reference, the object where the attribute is located is recycled, or the method of the previously passed parameter is returned, the reference count is reduced by 1.
  • When the reference count is 0, it indicates that the object is not referenced and can be marked as garbage for collection.

Reference traversal GC processing what is reference object Traversal

The garbage collector traverses the object from the point called GC Roots. All the points that can be reached are marked as alive. The unreachable objects in the heap are marked as garbage and cleared. What are GC Roots?

  • Class, which is loaded by the system class loader. These classes are never uninstalled. They can hold object references through static attributes. Note: Generally, classes loaded by custom class loaders cannot become GC Roots.
  • Thread, a surviving thread
  • Java method StackLocal variables or parameters in
  • JNI method StackLocal variables or parameters in
  • JNI global reference
  • Objects used for Synchronous monitoring
  • Objects held by JVM, which are not recycled by GC for special purposes. These objects may be system class loaders, some important exception handling classes, some objects reserved for exception handling, and some custom class loaders that are executing class loading. However, the specific objects mentioned above depend on the specific JVM implementation.

Learn more about how the garbage collector can be accessed to handle circular references There are four main GC (Garage Collection) Types

1. Reference Counting: each object has a counter that records the number of times of Reference and is GC when the counter is 0.
2. Mark and Sweep: traverse (the traversal indicates that there is a reference). Each object can be found and marked, and then all objects without tags are GC.
3. copy Collection: Revision 2. During the sweep operation, all objects in heap need to be scanned to ensure high CPU usage. in CC mode, replace the mark with replication to directly copy the retrieved object to another heap area, and then clear the old heap to complete GC.
4. generational Collection: this is also the revision of version 2, because the replication in version 3 is also slow. in short, heap is divided into four areas: Eden, elastic vor1, elastic vor2, And Tenured. the GC operation is triggered when Eden is full. This GC occurs in Eden, Region vor1, and Zone 2. objects in the Eden area that survive in GC are moved to region vor1 and Region 2, while objects in the same vor area are still alive after multiple GC operations, and moved to the Tenured area. GC occurs in Tenured, but the frequency is very low. in a word: If I check you several times, it will reduce the frequency of checking you.

There are many strange GC improvements in these aspects.
For example, the current JVM is generally
Generational Collection + Parallel Collector + Concurrent Mark and Sweep Collector

I do not like to say that the answer is too long and too detailed. so I will not elaborate on the advantages of their shortcomings. reference counting is not a collection mechanism. It is used with Root Reachability Analysis to determine whether an object is alive.

Next is the garbage collection algorithm:
  1. Mark-Sweep)
  2. Mark-Compact)
  3. Generational Collection)
  4. Copy)
Read the relevant chapters in the three books
1CLR via C #, which only introduces the best
2. a deep understanding of Java Virtual Machine (JVM), the advanced features and best practices of JVM, the more variable types of algorithms, and more detailed evolution and optimization processes.
The 3cocos2d-x authoritative guide can be used as an introduction to oc, that is, garbage collection developed by ios. It introduces the count-based and more convenient usage.

There are not many pages in total.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.