Common solutions for various garbage collection algorithms

Source: Internet
Author: User
Http://bbs.ss.pku.edu.cn/ss/index.php/5770/action_viewspace_itemid_5084.html

Reference Counting AlgorithmWhat is the Super blog of the institute of software and microelectronics of Peking University ?) F'j; g o ,?

) S! Rm8a # y g "^) w | I0 before 1960, people designed for the lisp language in the embryoJunkWhen collecting data, the first algorithm that comes to mind is to reference the counting algorithm. Taking napkin as an example, the principle of this algorithm can be roughly described:
%/@ Dnvjd v'vp0
In order to write down the design inspiration suddenly popped out of my mind during the lunch at the top of the page, I drew a napkin from my napkin bag and planned to draw a blueprint for the system architecture. According to the requirements of the reference counting version for napkin Use conventions, before drawing a picture, I must first write the counting 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 I have drawn, you should add 1 to the Count value on the napkin and change it to 2, this 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 the paper, you must reduce the Count value by 1, indicating that your use of the napkin has ended. Similarly, when I write all the contents on my napkin to my notebook, I will consciously reduce the count on my napkin by 1. In this case, the count on the napkin should be 0, and it will be collected by the garbage collector-assuming it is a robot responsible for cleaning-and picked it up and threw it into the garbage bin, because the only mission of the garbage collector is to find and clean all the napkins whose count is 0. 9c' & AJP/ry8w, super blog of the institute of software and microelectronics, Peking University
Peking University Institute of software and microelectronics super blog "t [c3p; XD? R_q
The advantages and disadvantages of the counting algorithm are also obvious. This algorithm is fast in performing garbage collection tasks, but it puts forward additional requirements for every memory allocation and pointer operation in the Program (increase or decrease the reference count of memory blocks ). More importantly, the reference counting algorithm cannot correctly release the memory blocks referenced by the loop. For this, D. Hillis has a funny and incisive discussion:
{Ymz8hf? /Qg2z0
5 | _ z1b1a0 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 ...... '"0nu [" f6r * ovq6a
Peking University software and microelectronics Institute super blog qs + ykb2cz
D. The story of Hillis is similar to the story we used to say when we were a child: "There was a mountain, a temple, and an old monk in the temple. This indicates that the reference counting algorithm alone is not enough to solve all the problems in garbage collection. 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 has its own superiority that cannot be substituted. Before and after the 1980 s, D. p. friedman, D. s. wise, H. g. baker and others have made several improvements to the reference counting algorithm, which makes the reference counting algorithm and its variants (such as the delay Counting Algorithm) in a simple environment, or in some modern garbage collection systems that integrate multiple algorithms, you can still show your skills.
) AA "P?!] D0E) O [0 Beijing University software and microelectronics Institute super blog 9n8xq "d5x" NK
Mark-Sweep Algorithm
;~ Y4by, X {0

% KC 'C $ tde0g0 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 Language. Taking napkin as an example, the execution process of the Mark-clearing algorithm is as follows: Peking University software and microelectronics Institute super blog-M k8y9w ~ Ey

; @ 8j $ ijm1tk; Z % n} 0 during lunch, 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 person in the restaurant in sequence: "Are you using napkin? Which napkin are you using ?" The robot marks the napkin that people are using based on their answers. After the inquiry, the robot finds all the paper napkins (apparently all used paper napkins) scattered on the dining table and threw them into the garbage bin. The super blog of the institute of software and microelectronics of Peking University, Xo! D * PG '(J

6hy + JM $ fd4d0 as its name implies, the execution process of the Mark-clearing algorithm 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 monitor every memory allocation and pointer operation in the runtime environment, as long as you 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)
/N0s3yh @"? O. i0k0 super blog of the institute of software and microelectronics, Peking University, be1v "C + C % {(ZG "/
With the success of the lisp language, the tag-clearing algorithm also shines in most early lisp runtime environments. 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 see that almost all modern garbage collection algorithms are the continuation of the Mark-clearing idea. mcCarthy and others have contributed as much as they did in the lisp language.
; AJ. fhj (y0
['G; Jmo + w7q H "W0Copy Algorithm
L $ fy0o % s3v0
Peking University software and microelectronics Institute super blog 5 ~ U} x9iu. Ha}
To solve the defects of the tag-clearing algorithm in the efficiency of garbage collection, M. l. minsky published a famous paper in 1963, "a lisp Garbage Collector algorithm using serial secondary storage, which uses a dual-storage zone )". The algorithm described by M. L. Minsky in this paper is called a replication algorithm. It is also successfully introduced to an implementation version of Lisp by M. L. Minsky.
O9g! Gp0 _) A A0
. E] n) The kc6av0 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:
:@~ J.! C7m9m0 ssaid, super blog of the institute of software and microelectronics, Peking University
The restaurant is divided into two identical parts by garbage collection robots: the southern district and the Northern District. During lunch, everyone eats in the southern district first (because of limited space, the number of diners will naturally be halved ).CasualUse napkin. When garbage collection robots think it is necessaryReclaimWhen using an old napkin, it requires all diners to move from the Southern district to the Northern District as quickly as possible and carry the napkin they are using with them. After everyone is moved 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 people's transfer direction has changed from North District to South District. In this way, each garbage collection simply needs to be transferred (that is, replicated) once, and the garbage collection speed is unparalleled-of course, it is hard for diners to travel between the North and South areas, garbage collection robots will never show mercy. Peking University software and microelectronics Institute super blog {; e9pv g

3 T: Ws @ I * |) s? 0 m. l. Minsky's invention is definitely a whimsy. The idea of partitioning and replication not only greatly improves the efficiency of garbage collection, in addition, the original complex memory allocation algorithms have been simplified and summarized as never before (since each memory collection is a collection of the entire half-zone, during memory allocation, you don't need to consider complicated situations such as memory fragments. You just need to move the heap top pointer and allocate the memory in order.) This is a miracle! However, the emergence of any odd trace has a certain price. 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.
(R. R @ (V; [_ c px + C0
W8_eo, K (om0Mark-compact algorithm: b5'uc (@ _ us; o

: Sv_f? 4CO) ^ K0 tagging-sorting algorithm is an organic combination of the tag-clearing algorithm and the replication algorithm. Combining the advantages of the tag-clearing Algorithm in memory usage and the execution efficiency of the replication algorithm, this is what everyone wants to see. 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. Around 1970, G. l. steele, C. j. cheney and D. s. wise and other researchers gradually found the correct direction, and marked-the outline of the Sorting Algorithm gradually became clear: 0k 'yii ~, super blog of the institute of software and microelectronics, Peking University ~ 1ibg

8? V! Q $ PQ & O, w3n8m0 are in a 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 performs the first step of the tag-clear algorithm, and then marks all the napkins in use, the robot ordered all diners to bring tagged napkins to the south of the restaurant, and threw unlabeled disposable napkins to the north of the restaurant. In this way, the robot will only stand in the north of the restaurant and embrace the garbage bins to welcome the unwanted napkin. Peking University software and microelectronics Institute super blog v U/& yh2p/_ + k

P blo; K (ue0 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. Beijing University software and microelectronics Institute super blog r q! K. ws @/u
X. AK [# '$ SC
Incremental collection AlgorithmBeijing University software and microelectronics Institute super blog p! | 6j KF * JP ^] "V
Super blog of the institute of software and microelectronics, Peking University (h; G % MZ] & P 'I
The Study of Real-time garbage collection algorithms directly led to the birth of incremental collection algorithms. Peking University Institute of software and microelectronics super blog % @ "z d) hwb} 0d'm

BS) yp1/xzn0 initially, people thought about real-time garbage collection as follows: to achieve real-time garbage collection, we can design a multi-process runtime environment, for example, you can use one process to execute garbage collection and another process to execute program code. In this way, the garbage collection work seems to be completed quietly in the background, without interrupting the running of program code.
2/'t] SCU; K_1 ~ 0 Beijing University software and microelectronics Institute super blog tsg6i # J; I
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 encounter a conflict between processes during design and implementation. For example, if the garbage collection process involves two stages: tag and clear, the results that the garbage collector has worked hard to mark in the first stage are likely to be completely modified by the memory operation code in the other process, so that the second stage of work cannot be carried out.
0x9i .? "Nv0nb0 hyuke @
M. l. minsky and D. e. knuth made an early research on the technical difficulties in the real-time garbage collection process. l. steele published a paper entitled "multiprocessing compactifying garbage collection" in 1975, this paper describes a Real-Time garbage collection algorithm called "Minsky-knuth-Steele 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 article "list processing in real time on a serial computer", which describes the incremental collection algorithm used for garbage collection in a multi-process environment. Peking University software and microelectronics Institute super blog 3o8n/V1 ^ g (~ : S

S7ja8c * g (the basis of the nih0 incremental collection algorithm is still the traditional tag-clearing and Replication Algorithms. By properly handling inter-process conflicts, the incremental collection algorithm allows the garbage collection process to 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. Here, readers only need to understand: H. g. the efforts of Baker and others have turned the dream of real-time garbage collection into reality. We no longer have to worry about the spam interruption program running. The super blog jqeo6 @ of the institute of software and microelectronics of Peking University @

0 w % H (_ L? CS0Generational collecting algorithm: Super blog of the institute of software and microelectronics, Peking University @ fij (|, B M + vt
Super blog C4, School of software and microelectronics, Peking University [3pcd
Like most software development technologies, statistical principles can always play a powerful catalyst in technological development. Before and after 1980, technical staff who are good at using statistical analysis knowledge in the study 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. The value of this discovery for garbage collection technology can be summarized by using napkin as follows: Peking University software and microelectronics Institute super Blog B? 0f $ @ 'y' n0b
Peking University software and microelectronics Institute super blog 5O j8vs7 [C
If the garbage collection robot is smart enough, the habit of using a napkin in a restaurant is first known. For example, some people prefer to use a napkin before and after meals, some people like to stick a napkin from start to end, and some people use a napkin every time they sneeze-a robot can develop a better napkin recycling plan, it's not long before people just threw away napkins and picked up the rubbish. This approach based on statistical principles can of course multiply the cleanliness of the restaurant.
! [~ K: WY H0
T p # Y-BP/fi0d. E. knuth, T. Knight, G. Sussman, R. Stallman and others made the earliest research on the classification and processing of memory garbage. In 1983, H. Lieberman and C. Hewitt published a paper entitled "A real-time Garbage Collector Based on the lifetimes of objects. This famous paper marks the birth of the generational collection algorithm. After that. 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. What is the Super blog PBE of the institute of software and microelectronics of Peking University? _ N
Peking University Institute of software and microelectronics super blog 7zng ^ V * No-V
The generational collection algorithm generally divides memory blocks in the heap into two types: 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. The generational collection algorithm enables the Garbage Collector to work more effectively under limited resource conditions.JavaThe best proof is obtained from the virtual machine.

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.