Something about GC

Source: Internet
Author: User

I wanted to sort out more text about generics and how to use them. Recently there have been so many things that I have never understood more. Post something about GC.
An important feature of virtual machine-based survival languages (Java, C #, etc.) is that they have their own garbage processing mechanisms. For programmers, The New Keyword is used to allocate objects. When releasing an object, they only need to assign null values to all references of the object so that the program cannot access this object, we call this object "inaccessible ". GC recycles the memory space of all "inaccessible" objects.
For GC, when a programmer creates an object, GC starts to monitor the address, size, and usage of the object. Generally, GC records and manages all objects in heap by Directed Graphs. This method is used to determine which objects are "reachable" and which objects are "inaccessible ". When GC determines that some objects are "inaccessible", GC has the responsibility to recycle the memory space. I collected some information and saw a lot of GC algorithms. At this stage, there seems to be no "best" algorithm. Basically, there are several GC decision algorithms:
1. reference counting collector)
The reference counting method is the only garbage collection algorithm that does not use the root set. This algorithm uses the reference counter to distinguish between a surviving object and an object that is no longer in use. Generally, each object in the heap corresponds to a reference counter. When an object is created and assigned to a variable, the reference counter is set to 1. When an object is assigned to any variable, add 1 to each reference counter. When an object is out of scope (this object is discarded and is no longer used), the reference counter minus 1. Once the reference counter is 0, the object meets the garbage collection condition.
The reference counter-based Garbage Collector runs fast and does not interrupt program execution for a long time. It is suitable for programs that must run in real time. However, the reference counter increases the overhead of program execution because each time an object is assigned to a new variable, the counter is incremented by 1, and every time an existing object has a scope, the counter is reduced by 1. This algorithm is relatively simple.
2. Tracing Algorithm (tracing collector)
The tracing algorithm is proposed to solve the problem of reference counting. It uses the concept of root set. The Garbage Collector Based on the tracing algorithm scans the root set to identify which objects are reachable and which objects are not reachable, and marks the reachable objects in some way, for example, you can set one or more places for each reachable object. In the scanning and identification process, the tracing algorithm-based garbage collection is also known as Mark-and-sweep ~) Garbage Collector.
3. compacting algorithm (compacting collector)
To solve the heap fragmentation problem, tracing-based garbage collection absorbs the compacting algorithm IDEA. In the process of clearing, the algorithm moves all objects to the end of the heap, the other end of the heap becomes an adjacent idle memory zone. The Collector updates all references of all objects it moves, so that these references can recognize the original objects at the new location. In the implementation of collectors Based on the compacting algorithm, the handle and handle tables are generally added.
4. Coping algorithm (coping collector)
This algorithm is proposed to overcome the handle overhead and solve the garbage collection of heap fragments. At the beginning, it divides the heap into one object, one plane, and multiple idle planes. The program allocates space for the object from the object plane. When the object is full, garbage Collection Based on the coping algorithm scans activity objects from the root set and copies each activity object to the idle surface (so that there is no idle hole between the memory occupied by the activity object ), in this way, the idle surface becomes the object surface, and the original object surface becomes the idle surface. The program will allocate memory in the new object surface.
A typical garbage collection Algorithm Based on the coping algorithm is the stop-and-copy algorithm, which divides the heap into the object plane and the free area plane. During the switching process between the object plane and the free area, the program is suspended.
5. Generation Algorithm (generational collector)
One defect of the stop-and-copy garbage collector is that the collector must copy all the active objects, which increases the program wait time, which is the cause of the inefficiency of the coping algorithm. In program design, there is a rule that most objects have a short time and a few objects have a long time. Therefore, the generation algorithm divides the heap into two or more sub-heaps as the generation (generation) of objects ). Because most objects have a short time, as the program discards unused objects, the garbage collector collects these objects from the youngest child heap. After the generational garbage collector is run, the objects that survived the last run are moved to the subheap of the next highest generation. Because the subheap of the old generation is not often recycled, this saves time.
6. Adaptive Algorithm (Adaptive collector)
In specific cases, some garbage collection algorithms are better than other algorithms. The Garbage Collector Based on the adaptive algorithm monitors the usage of the current heap and selects the garbage collector of the appropriate algorithm. This is relatively advanced, but it should be the best GC strategy.

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.