[Operating System] Chapter 3: Continuous Memory Allocation (Part2)

OS.StudyLog.Ch3.Continuous memory allocation. Continuous physical memory allocation

Memory fragmentation problem

When we allocate a space to a running program, there will be some free space that cannot be used further. That fragment, divided into two pieces (outer debris, debris inside)
outer fragments: between the memory allocation unit not to use
the fragment: Idle has been assigned to the application, but the application to use memory can not be further
two All of them are what we try to avoid.
Insert picture description here
Solve the problem of fragmentation: an effective method of memory allocation!
From the perspective of the OS, when will it provide continuous space allocation? Because the OS wants to load the APP from the hard disk into the memory, in fact, it is necessary to allocate a continuous area in the memory to allow the program to run; in another case, the application will access the data when the application is running, and the OS needs to give the data Allocate space. To this end, the OS needs to manage free and non-free space . Here, some data structures or algorithms are used to identify which ones are free and which are not.
Memory allocation algorithm:
Insert picture description here

First-fit

Insert picture description here
Yellow indicates free space, and green indicates non-free space. It looks simple and easy to understand, but its implementation also requires certain conditions.
Requirements:
1. Free blocks according to the address card sequence
2. The allocation needs to find a suitable partition
3. The redistribution needs to detect the
allocation process and the memory recycling problem needs to be considered. When recycling, it is necessary to consider whether the free blocks can be merged, because once the merge can be formed Larger space blocks can meet more application needs.
Advantages: simple. There is a large space at the high address.
Disadvantages: It is easy to form external debris and has uncertainty.

Best-fit

Insert picture description here
Free blocks are allocated according to the size that is most suitable and closest to the allocation request.
1. Free blocks according to address card sequence
2. Allocation needs to find a suitable partition
3. Redistribution needs to be detected
Advantages: relatively simple. It is very effective when most of the allocation is small size.
Disadvantages: The problem of external fragmentation is still serious. Slow reallocation. It is easy to produce a lot of useless small fragments, affecting the subsequent management

Worst-fit worst fit

Insert picture description here
According to the largest size difference, this kind of adaptation can change large blocks into small blocks, and keep small blocks as much as possible. The principle is that the outer debris is large, and there is a high probability that the debris can continue to be used.
1. Free blocks according to the address card sequence
2. Allocation needs to find a suitable partition
3. Redistribution needs to be detected
Advantages: Generally speaking, the allocation speed is relatively fast; joining allocation requests is medium and large, and the effect is better.
Disadvantages: slow redistribution; External fragmentation; splitting large free blocks causes large blocks to be allocated in the future

To sum up: The three allocation methods are not the best, only applicable according to the situation.
So we need some debris handling

Compressed defragmentation

Insert picture description here
Here are the problems:
1. The essence of the process merge and move process is actually the process of copying the memory (the overhead is relatively large). However, it cannot be moved during the execution of the program, so you must wait until the program is idle to deal with it.
2. How much does it cost to move to an ideal position. These are issues that need to be considered.

Exchange defragmentation

Insert picture description here
Purpose: Make full use of the hard disk, and treat the hard disk as a backup for memory
There are now four programs, of which P3 is being executed, which means that P1P2P4 is waiting to be executed in the memory. If P3 suddenly needs more memory during the execution, but P124 has already filled the memory space and needs to be preempted . P4 is waiting for an event to occur and the waiting time is relatively long. For this reason, we move the space occupied by P4 to the hard disk, so that the space is released in the memory. At this time, P3 continues to execute normally using the free blocks freed up.
The problem here is: 1. When to perform swap-in and swap-out operations 2. Which program is selected to swap out space
On the other hand, the granularity of swap-in and swap-out is based on the size of a single program, that is, if the program itself is compared Large, then the overhead of swapping in and out is also very large.
So how to manage and optimize? This will be explained in detail in the follow-up [Virtual Storage Management], here we only understand these two allocation methods.

Published 10 original articles · won 3 · views 289

Guess you like

Origin blog.csdn.net/Chahot/article/details/105465093