Study on memory arrangement principle

Source: Internet
Author: User
-- Recently, many plug-ins have been installed, and the memory is obviously in a hurry. At the same time, the operating system does not provide a memory sorting or optimization tool! (I don't know whether it's even ignorant or not. I didn't find it anyway .) As a result, I figured out the working principle of memory sorting. At the same time, I referred to the Lib of the Baidu School and recorded the following results.

I. The memory allocation method should be the main method. Most mainstream languages or runtime environments support three basic memory allocation methods:

1. Static allocation: The distribution form of static variables and global variables. We can regard static memory allocation as a durable home furniture. Generally, they do not need to be released or recycled, because no one will throw the wardrobe out of the window as garbage every day. 2. Automatic Allocation: The method for allocating memory for local variables in the stack. The memory in the stack can follow Code The outbound stack operation is automatically released when the block exits. This is similar to a visitor who visits the house. Every night, we have to go back to every house. Except for some who do not know the current business, we generally do not need to bundle the guests in a garbage bag to sweep out the house. 3. Dynamic Allocation: dynamically allocates memory space in the heap to store data. The memory block in the heap is like the napkin we use everyday. If we use it, we have to drop it into the garbage bin. Otherwise, the house will be full of space. A lazy like me dream of having a home robot to clean the house with him. In software development, if you are too reluctant to release the memory, you also need a similar robot-this is actually a specific Algorithm Implementation of the garbage collector. It is not difficult to find that the garbage collection algorithm is Program The algorithms used to collect and clean up obsolete "Napkins" during running are not static variables or local variables, but all allocated memory blocks in the heap. Ii. Garbage collection algorithm 1. REFERENCE The counting algorithm. According to the test, the first algorithm to solve the problem of garbage collection in history is to reference the counting algorithm. Taking napkin as an example, the principle of this algorithm can be roughly described:

At lunch, I took out a napkin from my napkin bag and planned to draw a blueprint for the system architecture. According to the requirements of the napkin usage statute reference counting edition, 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. The advantages and defects of the reference counting algorithm are equally 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. Using 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 itself has irreplaceable superiority. 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. 2. Mark-Sweep Algorithm
The first practical and perfect garbage collection algorithm is the Mark-clearing algorithm. It was proposed by J. McCarthy, inventor of Lisp in 1960 and was successfully applied to lisp. If the napkin is still used as an example, the process of executing the tag-Purge algorithm is as follows: during lunch, all people in the restaurant use the napkin according to their own needs. 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. As its name implies, the execution process of the tag-clearing algorithm is divided into two stages: "tag" 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) in the initial version of the tag-clearing algorithm, there are still many defects such as low efficiency (marking and clearing are two very time-consuming processes). However, almost all modern garbage collection algorithms are a continuation of the Mark-clearing idea. 3. To solve the defects of the tag-clearing algorithm in the efficiency of garbage collection. l. minsky published a famous paper in 1963, "a lisp Garbage Collector algorithm using serial secondary storage, which uses a dual-storage zone )". The algorithms described by M. L. Minsky in this paper are called Replication Algorithms.

The replication algorithm splits the heap space into two parts and uses a simple copy operation to complete garbage collection. This idea is really ingenious-innovation is the soul of technological progress, not the quotations of the Back River core. We can still use the napkin metaphor to understand the m. l. Minsky replication algorithm: The restaurant is divided into two identical parts by the garbage collection ROBOT: 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) and napkin can be used at will. 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. 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 process only 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. The invention of M. L. Minsky 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 miracle has a certain price. In the garbage collection technology, the cost of copying algorithms to improve efficiency is to artificially reduce the available memory by half. Regardless of the advantages and disadvantages, the replication algorithm has been successfully compared with the tag-clearing Algorithm in practice. So far, the three traditional algorithms of the garbage collection technology-the reference counting algorithm, the tag-clearing algorithm, and the replication algorithm-have been released around 1960. Each of these algorithms has its own strengths, both have fatal defects. Since the late 1960 s, researchers have gradually turned to improving or integrating these three traditional algorithms to foster strengths and circumvent weaknesses, adapt to the higher requirements of the programming language and runtime environment for the efficiency and timeliness of garbage collection. Iii. Significant progress since the 1970 s. With the continuous development of scientific research and application practices, people gradually realized that an ideal garbage collector should not cause application suspension at runtime, instead of occupying a large amount of memory and CPU resources, the three traditional garbage collection algorithms cannot meet these requirements. People must propose updated algorithms or ideas to solve many problems encountered in practice. At that time, the investigator's goals included:

First, improve the efficiency of garbage collection. Second, reduce the memory usage during garbage collection. This problem mainly occurs in the replication algorithm. Although the replication algorithm achieves a qualitative breakthrough in efficiency, the cost of sacrificing half of the memory space is still huge. Third, look for real-time garbage collection algorithms. Regardless of the execution efficiency, the three traditional garbage collection algorithms must interrupt the current work of the program when performing the garbage collection task. This delay caused by garbage collection is unacceptable by many programs, especially those that execute key tasks. How to improve traditional algorithms to achieve a real-time Garbage Collector for the current process that is quietly executed in the background without affecting-or at least seemingly unaffected, this is obviously a more challenging task. The determination of researchers to explore unknown fields and the progress of research work are equally surprising: in just over a decade from the 1970 s to the 1980 s, A large number of new computing methods and new ideas that excel in practical systems stand out. (unfortunately, it is a pity that foreigners can seize the opportunity to stand alone :(). It is precisely because of these increasingly mature garbage collection algorithms that today's programmers can be in Java or.. NET provides the runtime environment to allocate memory blocks as you like without worrying about the risk of Space release. 1. Mark-compact Algorithm
A tag-sorting algorithm is an organic combination of a tag-clearing algorithm and a replication algorithm. Combine the memory usage of the tag-clearing algorithm with the execution efficiency of the replication algorithm. However, the integration of the two garbage collection algorithms is not as simple as 1 plus 1 equals 2. Around 1970, G. l. steele, C. j. cheney and D. s. wise and other researchers have gradually found the correct direction, and the outline of the markup-sorting algorithm has gradually become clearer: This time in the restaurant we are familiar, garbage collection robots no longer divide restaurants 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 only needs to stand in the north of the restaurant, embrace the garbage bins, and greet the unwanted 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. 2. 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 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. 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. The basis of the 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. Analyzing the internal mechanism of Various incremental collection algorithms in detail is a very tedious task-if you are interested and need it, you can dig deep into it on holidays. It must be interesting. What people need to remember and appreciate is that H. g. the efforts of Baker and others have turned the dream of real-time garbage collection into reality, and the majority of programmers no longer have to worry about interrupting the running of programs by garbage collection. Similarly, people need to remember and appreciate things and things tomorrow should also be born today :)

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.