introduction
This article is the eighth chapter, memory allocation and recovery of storage management . Early computer programming did not require too much storage management. As computers and programs become more and more complex, storage management becomes necessary. This article is mainly to understand the process of memory allocation and memory recovery
Significance of storage management
- Ensure that the computer has enough memory to process data
- Ensure that the program can obtain a portion of memory usage from available memory
- Ensure that the program can return the used memory for use by other programs
The process of memory allocation
Single continuous allocation
- Single continuous allocation is the simplest memory allocation method
- Can only be used in a single-user , single-process operating system
Its allocation process is to divide the memory into system area and user area
The system area refers to that all the memory in the system area is used by the operating system area , and the user area refers to that all the memory in the user area is used by programs in the user area (this is an outdated method)
Fixed partition allocation
- Fixed partition allocation is the simplest storage allocation method that supports multiple programs
- The memory space is divided into several fixed-size areas
- Each partition is only provided for use by one program and does not interfere with each other
The main memory is divided into several partitions, and each partition is provided for use by a certain process. This is the allocation method of fixed partitions
Dynamic partition allocation
- According to the actual needs of the process, dynamically allocate memory space
- Involving related data structures and specific algorithms
Dynamic partition free table data structure
Assuming that there are several partitions in main memory, some are used and some are not used, so a data structure is needed to store whether a certain partition is used, and a free table data structure is needed at this time
There are several partitions in the table, and each partition has a mark, 0 or 1, 0 means not used, 1 means used. This is the dynamic partition free table data structure
Dynamic partition free chain data structure
This is to use a doubly linked list to save the free area in the dynamic partition. All scattered free areas are connected end to end through a linked list to form a free linked list . However, there will be free areas 2 and 3 that are continuous as shown in the figure below. Therefore, node 2 and node 3 can be combined, so that you can Reduce the number of nodes in the free linked list. Therefore, the size of each node is different, so it is necessary to store and record the storage capacity of this node in each node. This is the data structure of the dynamic partition free chain
Dynamic partition allocation algorithm
- First adaptation algorithm (FF algorithm)
- Best Fit Algorithm (BF Algorithm)
- Fast adaptive algorithm (QF algorithm)
These algorithms are actually used for dynamic memory allocation
First adaptation algorithm (FF algorithm)
For each memory allocation, search for a suitable memory area from the beginning (mainly using the data structure of the free chain).
If there is no suitable free area, the allocation will fail. If it is found, the free area will be allocated to this process. . Start from the head each time, making the head address space continuously divided
Best Fit Algorithm (BF Algorithm)
- The best adaptation algorithm requires the free area linked list to be sorted by capacity
- Traverse the free area linked list to find the best free area
Sort the free area according to the size. When the memory is needed, traverse from the head of the node to find the best free area node. The advantage of this algorithm is that it can avoid a situation of overkill, because it traverses the free list from small to large, so the first suitable free area it matches must be the best.
Fast adaptive algorithm (QF algorithm)
- Fast adaptation algorithm requires multiple free area linked lists
- Each free area linked list stores a free area of one capacity
Divide the node with one free zone and the node with two free zones into two linked lists. When memory allocation is required, a memory area suitable for a certain process can be quickly found.
The process of memory reclamation
The memory reclamation process may have the following situations:
- The recovery area is adjacent to a free area, and the recovery area is below the free area
- The recovery area is adjacent to a free area, and the free area is below the recovery area
- The recycling area is in the middle of two free areas
- Separate recycling area
Recycling process in each case
The recycling area is below the free area
Use the data structure of the free linked list to save the free area, no need to create a new free linked list node, only need to increase the capacity of the free area 1 to the free area (that is, include the reclaimed area)
The recycling area is above the free area
- Combine the recycle area with the free area
- The new free area uses the address of the reclaimed area
The recycling area is located between the two free areas
- Combine free area 1, free area 2 and reclaim area
- The new free area uses the address of free area 1
Separate recycling area
- Create a new free node for the recycle area
- Insert into the corresponding free area linked list
It is the core competitiveness of a technical person to find the constant in the rapidly changing technology. Unity of knowledge and action, combining theory with practice