Free memory management, bitmap, free linked list

When dynamically allocating memory , the operating system must manage it. There are two ways to track memory usage: bitmaps and free lists .

1. Storage management using bitmaps

When using the bitmap approach, memory may be divided into allocation units as small as a few words or as large as a few kilobytes . Each allocation unit corresponds to a bit in the bitmap, with 0 for free and 1 for occupied (or vice versa). A memory area and its corresponding bitmap are shown in Figure 3-6.

 

The size of the allocation unit is an important design factor. The smaller the allocation unit, the larger the bitmap . However, even if there is only a 4-byte allocation unit, 32-bit (4-byte) memory requires only 1 bit in the bitmap; 32n-bit memory requires n-bit bitmap, so the bitmap only occupies 1/ 32 of memory. If a larger allocation unit is selected, the bitmap is smaller. But if the size of the process is not an integer multiple of the allocation unit, then a certain amount of memory will be wasted in the last allocation unit .

Because the size of the memory and the size of the allocation unit determine the size of the bitmap, it provides a simple way to keep track of memory usage using a fixed-size memory area. The main problem with this approach is that, when deciding to load a process with k allocation units into memory, the storage manager must search the bitmap to find a string of k consecutive zeros in the bitmap . Finding a continuous string of 0s of a specified length in a bitmap is a time-consuming operation (because the string may cross word boundaries in a bitmap), which is a disadvantage of bitmaps .

2. Storage management using linked lists

Maintains a linked list of allocated and free memory segments. A node in the linked list either contains a process, or an empty free area between two processes. The memory layout shown in Figure 3-6a can be represented by the segment linked list shown in Figure 3-6c. Each node in the linked list contains the following fields: indication of free area (H) or process (P) , starting address , length and a pointer to the next node .

In this example, the segment list is sorted by address , which has the advantage that the update of the list when the process terminates or is swapped out is straightforward . A process to be terminated generally has two neighbors (unless it is at the bottom or top of memory), which may be processes or free areas, resulting in the four combinations shown in Figure 3-7. Updating the linked list in Figure 3-7a requires replacing P with H; in Figure 3-7b and Figure 3-7c, two nodes are merged into one, and the linked list is missing one node; in Figure 3-7d, three Nodes are merged into one, removing two nodes from the linked list.

  

Because the node representing the terminated process in the process table usually contains a pointer to the node corresponding to its segment list, it may be more convenient to use a double-linked list for a segment-linked list than the singly-linked list shown in Figure 3-6c. Such a structure makes it easier to find the previous node and check if it can be merged .

When storing processes and free areas in a linked list in address order , there are several algorithms that can be used to allocate memory for a created process (or an existing process swapped in from disk) . Here, it is assumed that the storage manager knows how much memory to allocate for the process .

The simplest algorithm is the first fit algorithm. The storage manager searches along the segment list until it finds a large enough free area. Unless the size of the free area is exactly the same as the size of the space to be allocated, the free area is divided into two parts, one part is used by the process, and the other part forms a new part. free area. The first fit algorithm is a fast algorithm because it searches as few linked list nodes as possible.

A small modification to the first fit algorithm results in the next fit algorithm. It works in the same way as the first fit algorithm, except that every time a suitable free area is found, the current position is recorded. So that the search starts from where it ended last time when looking for a free area next time, instead of starting from the beginning every time like the first-fit algorithm. The simulation program of Bays (1977) proves that the performance of the next-fit algorithm is slightly lower than that of the first-fit algorithm.

Best fit algorithm. Search the entire linked list (start to end) to find the smallest free area that can hold the process. Try to find the free area that is closest to the actual need, and match the requested and available free area to the best area, rather than splitting a large free area that may be used later.

Take Figure 3-6 as an example to examine the first-fit algorithm and the best-fit algorithm. If a block of size 2 is required, the first fit algorithm will allocate the free area at position 5, and the best fit algorithm will allocate the free area at position 18.

因为每次调用最佳适配算法时都要搜索整个链表,所以它要比首次适配算法慢。它比首次适配算法或下次适配算法浪费更多的内存,因为它会产生大量无用的小空闲区。一般情况下,首次适配算法生成的空闲区更大一些。

最佳适配的空闲区会分裂出很多非常小的空闲区,为了避免这一问题,可以考虑最差适配(worst fit)算法,即总是分配最大的可用空闲区,使新的空闲区比较大从而可以继续使用。仿真程序表明最差适配算法也不是一个好主意。

如果为进程和空闲区维护各自独立的链表,那么这四个算法的速度都能得到提高。这样就能集中精力只检查空闲区而不是进程。但这种分配速度的提高的一个不可避免的代价就是增加复杂度和内存释放速度变慢,因为必须将一个回收的段从进程链表中删除并插入空闲区链表。

如果进程和空闲区使用不同的链表,则可以按照大小对空闲区链表排序,以便提高最佳适配算法的速度。在使用最佳适配算法搜索由小到大排列的空闲区链表时,只要找到一个合适的空闲区,则这个空闲区就是能容纳这个作业的最小的空闲区,因此是最佳适配。因为空闲区链表以单链表形式组织,所以不需要进一步搜索。空闲区链表按大小排序时,首次适配算法与最佳适配算法一样快,而下次适配算法在这里则毫无意义

在与进程段分离的单独链表中保存空闲区时,可以做一个小小的优化。不必像图3-6c那样用单独的数据结构存放空闲区链表,而可以利用空闲区存储这些信息。每个空闲区的第一个字可以是空闲区大小,第二个字指向下一个空闲区。于是就不再需要图3-6c中所示的那些三个字加一位(P/H)的链表结点了。

另一种分配算法称为快速适配(quick fit)算法,它为那些常用大小的空闲区维护单独的链表。例如,有一个n项的表,该表的第一项是指向大小为4KB的空闲区链表表头的指针,第二项是指向大小为8KB的空闲区链表表头的指针,第三项是指向大小为12KB的空闲区链表表头的指针,以此类推。像21KB这样的空闲区既可以放在20KB的链表中,也可以放在一个专门存放大小比较特别的空闲区的链表中。

快速适配算法寻找一个指定大小的空闲区是十分快速的,但它和所有将空闲区按大小排序的方案一样都有一个共同的缺点,即在一个进程终止或被换出时,寻找它的相邻块,查看是否可以合并的过程是非常费时的。如果不进行合并,内存将会很快分裂出大量的进程无法利用的小空闲区。


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325775157&siteId=291194637