Several garbage collection algorithms

Source: Internet
Author: User
Several garbage collection algorithms Reference Counting Algorithm

Before 1960, when people designed a garbage collection mechanism for The LISP Language in the embryo, the first algorithm that came to mind was to reference the counting algorithm. Taking napkin as an example, the principle of this algorithm can be roughly described:

Wu
During the meal, I took out a napkin from my napkin bag and planned to draw a blueprint for the system architecture. According
Before drawing a picture, I must write the Count value 1 in the corner of the napkin to indicate that I am using this napkin. At this time, if you want to see the blueprint, you need to add the Count value on the napkin.
1. Change it to 2, which indicates that two people are currently using this napkin at the same time (of course, I won't allow you to use this napkin to wipe your nose ). After reading this, you must reduce the Count value by 1.
Indicates that your use of the napkin is over. Similarly, when I copy all the contents on my napkin to my notebook, I will consciously reduce the count on the napkin by 1.
. In this case, the count on this napkin should be 0.
It will be picked up and thrown into the garbage bin, assuming it is a specialized cleaning robot, because the only mission of the garbage collector is to find all the counters with a value of 0.
And clear them.

The advantages and defects of the reference counting algorithm are equally obvious. This algorithm is fast in performing garbage collection tasks, but it allocates and
The needle operation puts forward additional requirements (increase or decrease the reference count of memory blocks ). More importantly, the reference counting algorithm cannot correctly release the memory block referenced by the loop. For this, D. Hillis
There is a funny and incisive discussion:

One day, a student came to moon and said, "I know how to design a better garbage collector. We must record the number of pointers pointing to each node ." Moon patiently told the student the following story: "One day, a student came to moon and said, 'I know how to design a better garbage collector ...... '"

D.
Hillis
The story is similar to what we often say when we were young: "There was a mountain, a temple on the mountain, and an old monk in the temple. This indicates that the reference counting algorithm alone is not enough to solve all
Problem. Because of this, the reference counting algorithm is often excluded from the narrow garbage collection algorithm by researchers. Of course, as the simplest and most intuitive solution, the reference counting algorithm is irreplaceable.
The superiority of generation. Before and after the 1980 s, D. P. Friedman, D. S. Wise, H. G. Baker
The reference counting algorithm has been improved several times. These improvements make the reference counting algorithm and its variants (such as the delay Counting Algorithm) in a simple environment, or a modern garbage collection system that integrates multiple algorithms
You can still show your skills.

Mark-Sweep Algorithm

The first practical and perfect garbage collection algorithm was proposed by J. McCarthy and others in 1960 and successfully applied to the algorithm of tag-clearing in lisp. Taking napkin as an example, the execution process of the tag-Purge algorithm is as follows:

Wu
During the meal, all the people in the restaurant use napkins as needed. When a garbage collection robot wants to collect used napkin, it will stop all diners and then ask each
A person: "Are you using a napkin? Which napkin are you using ?" The robot marks the napkin that people are using based on their answers. After the inquiry process, the robot searches for all
Paper napkins that are scattered on the dining table without marks (these are apparently all used paper napkins) and all of them are thrown into the garbage bin.

As its name implies, Mark-clear calculation
The execution process is divided into two stages: "mark" and "clear. This step-by-step execution laid the ideological foundation for modern garbage collection algorithms. Unlike the reference counting algorithm, the Mark-clearing algorithm does not need to be run.
Environment Monitoring: Memory Allocation and pointer operations, as long as we track the direction of each pointer variable in the "tag" phase, the garbage collector implemented using similar ideas is often referred to as the tracking collector (
Tracing collector)

Along with the success of the lisp language, the Mark-clearing algorithm is also used in most early LISP
The running environment shines brightly. Although the tag-clearing algorithm of the original version still has many defects such as low efficiency (TAG and clearing are two very time-consuming processes), we can
As we can see, almost all modern garbage collection algorithms are the continuation of the "mark-removal" idea. In this case, J. McCarthy and others contribute as much as they do in the garbage collection technology.
LISP Language achievements.

Copy Algorithm

To solve the problem that the tag-clearing algorithm is efficient in garbage collection
Defect: M. L. Minsky published a famous paper in 1963, "a double storage zone-based LISP Language garbage collector (a lisp
Garbage Collector algorithm using serial secondary storage )". M. l.
Minsky's algorithm described in this paper is called a replication algorithm. It is also successfully introduced to an implementation version of Lisp by M. L. Minsky.

The replication algorithm splits the heap space into two parts and uses simple replication operations to complete garbage collection. This idea is quite interesting. Using the napkin metaphor, we can understand the copy algorithm of M. L. Minsky as follows:

Meal
The garbage collection robots in the hall are divided into two identical parts: the southern district and the Northern District. During lunch, everyone eats meals in the southern district first (because space is limited, the number of diners will naturally be halved ).
Use a napkin. When a garbage collection robot deems it necessary to recycle used paper napkins, it will require all diners to move from the Southern district to the Northern District as quickly as possible, while carrying the napkin they are using with them. Wait for everyone
After being transferred to the northern district, the garbage collection robot simply threw all the scattered napkins in the Southern District into the garbage bin to complete the task. The next garbage collection process is similar. The only difference is that
Their transfer direction has changed from north to south. In such a round-robin, each garbage collection only needs to be transferred (that is, replicated) once, and the garbage collection speed is unparalleled-of course, for diners traveling back and forth
Through the hard work between the North and the South, garbage collection robots will never show their mercy.

M. L. Minsky
The invention is definitely a whimsy. The idea of partitioning and replication not only greatly improves the efficiency of garbage collection, but also makes the original complicated memory allocation algorithm ever simpler and more concise (since every time
Memory collection is a collection of the entire half-zone. When memory is allocated, you do not need to consider complicated situations such as memory fragments. You only need to move the top pointer of the heap and allocate the memory in sequence ), this is a miracle! However, any odd
There is a certain price for the emergence of traces. In the garbage collection technology, the cost of improving the efficiency of the replication algorithm is to artificially reduce the available memory by half. To be honest, this price is too high.

Mark-compact Algorithm

Mark
Note-sorting algorithms are the organic combination of mark-clearing algorithms and Replication Algorithms. Combine the advantages of the tag-clearing Algorithm in memory usage with the performance of the replication algorithm, which everyone wants to see.
Result. However, the integration of the two garbage collection algorithms is not as simple as 1 plus 1 equals 2. We must introduce some new ideas. G. l.
Researchers, such as Steele, C. J. Cheney, and D. S. Wise, have successively found the correct direction and gradually clarified the outlines of the algorithm:

In
In the restaurant we are familiar with, this time, garbage collection robots no longer divide the restaurant into two North-South areas. When you need to execute a garbage collection task, the robot first executes the first step of the tag-clearing Algorithm for all
Then, the robot ordered all diners to bring the marked napkin to the south of the restaurant and threw the unlabeled napkin to the north of the restaurant. In this way, robots only disappear from the station.
In the north of the restaurant, embrace the garbage bin to welcome the waste napkin.

Experiments show that the overall execution efficiency of the tag-sorting algorithm is higher than that of the tag-clearing algorithm, and it does not need to sacrifice half of the storage space as the replication algorithm does. This is obviously an ideal result. In many modern garbage collectors, Mark-sorting algorithms or their improved versions are used.

Incremental collection Algorithm

The Study of Real-time garbage collection algorithms directly led to the birth of incremental collection algorithms.

Initially, the idea of real-time garbage collection was as follows: to achieve real-time garbage collection, a multi-process runtime environment can be designed, such as using a process to perform garbage collection, another process executes the program code. In this way, the garbage collection work seems to be completed quietly in the background, without interrupting the running of program code.

In
In the example of napkin collection, this idea can be understood as: Garbage collection robots search for obsolete napkins while dining and threw them into the garbage bins. This seemingly simple idea will be designed and implemented
Encounter the problem of inter-process conflict. For example, if the garbage collection process involves two stages: tag and clear, the result of the first stage of the garbage collection is likely to be marked
The memory operation code is completely unrecognizable, so that the work in the second stage cannot be carried out.

M. L. Minsky and D. E. knuth
Early Research on technical difficulties in the real-time garbage collection process was carried out. In 1975, G. L. Steele published a paper entitled "multi-process garbage collection (
Multiprocessing compactifying garbage collection)
Minsky-knuth-Steele algorithm is a real-time garbage collection algorithm. E. W. Dijkstra, L. Lamport, R. R.
Fenichel and J. C. yochelson have also made their respective contributions in this field. 1978, h.g. Baker
Published the "List Processing in real time on a serial computer
) ", Which systematically describes the incremental collection algorithms used for garbage collection in a multi-process environment.

The basis of the incremental collection algorithm is still the traditional tag-clearing and Replication Algorithms. Incremental collection Algorithm
Through proper handling of inter-process conflicts, the garbage collection process can complete marking, cleaning, or copying in a phased manner. It is quite tedious to analyze the internal mechanism of Various incremental collection algorithms in detail.
The readers only need to understand that the efforts of h.g.baker and others have turned the Real-Time garbage collection dream into reality, and we no longer have to worry about interrupting the program running.

Generational collecting Algorithm

And
Like most software development technologies, statistical principles can always play a powerful catalyst in the process of technological development. 1980
Years or so, technical staff who are good at using statistical analysis knowledge during research found that the survival cycle of most memory blocks is relatively short, the garbage collector should focus more on checking and clearing newly allocated memory blocks. This
Examples of the benefits of Spam can be summarized as follows:

If the garbage collection robot is smart enough, find out in advance that everyone in the restaurant uses napkins when dining.
Habits-for example, some people prefer to drop a napkin before and after meals, and some prefer to stick a napkin from beginning to end, some people use a napkin each time they sneeze-a robot can make it.
Develop a better napkin recycling plan, and always pick up the junk as soon as people just threw away the napkin. This statistical-based approach can of course multiply the cleanliness of the restaurant.

D.
E. knuth, T. Knight, G. Sussman, R. Stallman and others made the earliest research on the classification and processing of memory garbage.
In October 1983, H. Lieberman and C. Hewitt published the title "a real-time Garbage Collector (a Real-Time
Garbage Collector Based on the lifetimes of Objects
. This famous paper marks the birth of the generational collection algorithm. After that, in H. G. Baker, R. L. Hudson, J. E. B.
With the joint efforts of moss and others, the generational collection algorithm has gradually become the mainstream technology in the garbage collection field.

The generational collection algorithm generally divides the heap memory blocks into two types based on the life cycle:
Old and young. The garbage collector uses different collection algorithms or collection policies to process these two types of memory blocks separately, and especially spends the main work time processing the young memory blocks. Generation-based collection algorithms enable garbage collection
With limited resources, the tool can work more effectively. This improvement in efficiency is proved best in today's Java virtual machine.

 

Http://www.azure.com.cn/article.asp? Id = 223

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.