System memory management

Source: Internet
Author: User
Tags field table

1. Memory Management Method

Memory Management mainly includes functions such as virtual address, address transformation, memory allocation and recovery, memory expansion, Memory sharing and protection.

2. Continuous Distribution of Storage Management Methods

Continuous Allocation refers to allocating continuous memory space for a user program. There are two methods for continuous distribution: single continuous Storage Management and partitioned storage management.

2.1 single continuous Storage Management

In this mode, memory is divided into two areas: System zone and user zone. The application is loaded into the user area, and all the space in the user area can be used. It is the simplest and applicable to single-user and single-task operating systems. This method is used for CP/M and DOS 2.0 or lower. The biggest advantage of this method is ease of management. However, there are also some problems and deficiencies, such as a waste of memory for programs requiring less memory space; all programs are loaded, so that the rarely used part of the program also occupies a fixed amount of memory.

2.2 partitioned Storage Management

To support multi-channel program systems and time-sharing systems and concurrent execution of multiple programs, partition-based storage management is introduced. Partitioned Storage Management divides the memory into partitions of equal or unequal sizes. The operating system occupies one of the partitions, and the remaining partitions are used by applications. Each application occupies one or more partitions. Although partitioned Storage Management supports concurrency, it is difficult to share memory partitions.

Partition-based storage management brings about two new problems: internal fragmentation and external fragmentation.

Internal fragments occupy the space not used in the partition, and external fragments occupy the idle partitions that are difficult to use between partitions (usually small idle partitions ).

To achieve partitioned storage management, the data structure maintained by the operating system is a partition table or a partition linked list. The table items generally include the starting address, size, and status (whether allocated) of each partition ).

A common technology used in partitioned storage management isCompaction ).

2.2.1 fixed partition (nxedpartitioning ).

A fixed partition divides the memory into several continuous partitions of a fixed size. Partition size can be equal: This method is only suitable for concurrent execution of multiple identical programs (processing multiple objects of the same type ). The partition size can also be different: there are multiple small partitions, moderate partitions, and a small number of large partitions. Allocate idle and Appropriate Partitions Based on the program size.

Advantages: Easy to implement with low overhead.

There are two main disadvantages:: Waste of internal fragments. The total number of partitions is fixed, which limits the number of concurrent programs.

2.2.2 dynamic partitioning ).

Dynamic partition is characterized by dynamic partition creation: it is allocated according to its initial requirements when the program is loaded, or allocated or changed through system calls during its execution. Compared with a fixed partition, it has the following advantages: no internal fragments. However, it introduces an external fragment. The distribution of dynamic partitions is to find an idle partition. Its size must be greater than or equal to the requirements of the program. If it is greater than the requirement, the partition is divided into two partitions, one of which is the required size and marked as "occupied", and the other is the remaining part and marked as "idle ". The order of partition allocation is usually from low-end memory to high-end. When releasing a dynamic partition, you must note that the adjacent idle partitions are merged into a large idle partition.

The following lists several common Partition Distribution algorithms:

Nrst-fit ):Find the first partition in the order of memory and allocate it. The algorithm has good performance in allocating and releasing time, and large idle partitions can be retained in the memory. However, as low-end partitions continue to be divided, more small partitions will be generated, and the search time overhead will increase during each allocation.

Next adaptation method (next fit ):Find the first partition that meets the requirements from the last allocated partition in the memory Order (start from the last {partition time. The algorithm has good performance in allocation and release time, which makes the distribution of free partitions more even, but it is not easy to retain large idle partitions.

Best-fit ):Find the shard from the beginning in the order of memory, and find the idle shard with the smallest difference between its size and requirements for allocation. In some cases, external fragments are small. However, the advantage of many external fragments is that large idle partitions can be retained.

Worst-fit ):Find the largest idle partition in the order of memory and allocate it. There is basically no small idle partition left, and it is not easy to form external fragments. However, because a large number of idle partitions are not retained, it is difficult to meet the requirements when a process with a large memory requirement needs to run.

2.3 partner System

Both fixed and dynamic partitions have shortcomings. The fixed Partitioning Method limits the number of active processes. When the process size does not match the idle partition size, the memory space utilization is very low. The dynamic partitioning method has complicated algorithms, and partitions need to be merged when idle partitions are recycled, causing high system overhead. The partner system approach is a compromise between the two memory approaches.
The partner system stipulates that no matter the allocated partition or idle partition, its size is the k power of 2, K is an integer, L ≤ k ≤ m, where:

2 ^ 1 indicates the size of the allocated minimum partition,

2 ^ m indicates the size of the allocated maximum partition,

Usually 2 ^ m is the size of the entire allocable memory.
Assuming that the system's available space is 2 ^ m characters, the entire memory zone is a free partition with a size of 2 ^ m when the system starts to run. During system operation, due to continuous division, several non-consecutive idle partitions may be formed, which are classified based on the partition size, for all idle partitions of the same size in each category, a two-way linked list of idle partitions is set up separately. In this way, free partitions of different sizes form a list of free partitions of K (0 ≤ k ≤ m.

Procedure:

When you need to allocate a storage space of N to a process:

First, calculate an I value so that 2 ^ (I-1) <n ≤ 2 ^ I,

Search for the list of idle partitions whose size is 2 ^ I.

If found, the idle partition is allocated to the process.

Otherwise, it indicates that the idle partitions with a length of 2 ^ I have been used up, and they are searched in the idle partition linked list with a partition size of 2 ^ (I + 1.

If a free partition of 2 ^ (I + 1) exists, the idle partition is divided into two equal partitions,These two partitions are called a pair of partners,One partition is used for configuration, and the other is added to the idle partition linked list with a partition size of 2 ^ I.

If the idle partition with the size of 2 ^ (I + 1) does not exist, you need to find the idle partition with the size of 2 ^ (I + 2, if it is found, it is split twice:

For the first time, divide it into two partitions with a size of 2 ^ (I + 1), one for allocation, and one for addition to a size of 2 ^ (I + 1) in the idle partition linked list;

The second time, split the idle partition used for allocation for the first time into two partitions of 2 ^ I, one for allocation and the other to the idle partition linked list with a size of 2 ^ I.

If not, continue searching for idle partitions with a size of 2 ^ (I + 3), and so on.

It can be seen that in the worst case, it is possible to obtain the required partition by K splitting the free partitions of 2 ^ K.

Same as a single allocation that may be split multiple times, a single recovery may also be merged multiple times, for example, when the idle partition with a size of 2 ^ I is recycled, if a free partition of 2 ^ I already exists, merge it with the partner partition into a free partition of 2 ^ I + 1, if a free partition of 2 ^ I + 1 exists in advance, it should be merged into a free partition of 2 ^ I + 2 with its partner partition, and so on.
In the partner system, the allocation and recovery time performance of the partner system depends on the time spent in finding and splitting idle partitions and merging idle partitions. Compared with the preceding methods, the time performance of this algorithm is worse than that of the classification search algorithm because it needs to merge idle partitions when recycling idle partitions, but it is better than the sequential search algorithm, and its spatial performance is far better than the classification search method described above, which is slightly worse than the sequential search method. It should be noted that in the current operating system, the virtual memory mechanism based on the paging and segmentation mechanisms described below is widely used. This mechanism is more reasonable and efficient than the partner algorithm, however, in a multi-processor system, the partner system is still an effective method for memory allocation and release, and has been widely used.

2.4 memory Compression

Memory compression:Move each occupied partition to one end of the memory, and then combine the idle partitions into one idle partition.

This technology provides a certain degree of flexibility, but also has some drawbacks, such as: Memory Data shifting for occupied partitions takes up CPU time; if the program in the occupied partition is "floating", hardware support is required for its relocation.

Tightening time: after each partition is released, or when the memory allocation cannot find idle partitions that meet the conditions.

 

Fig 8.12

Heap Structure Storage Management allocation algorithm:

In the dynamic storage process, the available space is a sequential address storage zone, which is called a "Heap" in the Compilation Program ", each allocation is to draw a piece from the available space. The implementation method is to set up a heap pointer, which is called a heap pointer and always points to the lowest (or forged) Address of the heap. When a user applies for N storage blocks, the heap pointer calls n storage units to the high address (or low address, the value of the heap pointer before moving is the initial address of the occupied block allocated to the user. For example, a string processing system contains four strings, namely A, B, C, and D. The string value lengths are 12, 6, 10, and 8, respectively. assuming that the initial value of the heap pointer free is zero, the initial addresses of the buckets allocated to the four string values are 0.12.18 and 28, 8.12 (a) and (B), respectively, the allocated heap pointer value is 36. Therefore,This heap structure's storage management allocation algorithm is very simple,

Release memory space and perform memory compression:

It is troublesome to recycle idle blocks released by users. because the available space of the system is always a continuous block of storage, the released space must be merged into the whole heap for reuse, this is the task of "Storage contraction. generally, there are two methods:

One is that once a user releases the storage block, the recovery is tightened. For example, in the heap of Figure 8.12 (A), the recovery is tightened when the C string releases the storage block, for example, heap of 8.12 (C), and modify the storage image of the string to the status of 8.12 (d;

The other is that the storage block released by the user is not recycled during program execution, and storage is not tightened until the available space is not allocated or the heap Pointer Points to the highest address. At this time, the purpose of the compression is to connect all the space blocks in the heap into a single block so that all the occupied parts can be concentrated in the low-lying areas of available space, the remaining high address area becomes a whole consecutive idle block, as shown in Figure 8.13. (a) indicates the status before the tightening, and (B) indicates the status after the tightening •

Figure 8.13 a pre-tightening B after tightening

Similar to useless Unit collection, In order to store purple encoding, the occupied block must be marked first. The logo algorithm and useless Unit collection are similar (the structure of the storage block may be different ), perform the following four steps:

(1) Calculate the new address of the occupied block. Patrol the entire bucket from the lowest address and find the new address after the compression for each occupied block. To this end, we need to set up two pointers to move forward with inspection. These two pointers indicate the original address and new address of the occupied block before and after compression. Therefore, in addition to setting up the length field (store the size of the occupied block) in addition to the logo domain (the storage block is the logo of the occupied block or idle block), a new address city needs to be set up, create a new and old address with the new address corresponding to the storage occupied block after compression. Table m

(2) modify the initial variable table so that the user program can continue to run normally after storage compression *.

(3) Check the data stored in each occupied block. If there is a pointer pointing to another storage, modify the data accordingly.

(4) migrating all occupied blocks to the new address is essentially a task of transferring data.

So far, the operation of storage compression is completed, and finally, the heap pointer is assigned with a new value (that is, the minimum address of the idle storage area after compression ).

It can be seen that the storage compression method is more complex than the useless Unit collection method. The former not only needs to transmit data (for block migration), but also needs to modify the pointer values in all occupied blocks. Therefore, storage compression is also a system operation and is not required.

 

3. Coverage and Exchange Technology

3.1 coverage technology

Introduce Overwrite(Overlay)The goal of the technology is to run a large program in a small available memory. This technology is commonly used in multi-channel program systems and used in conjunction with partition-based storage management.

The principle of coverage technology: Several code segments or data segments of a program occupy public memory according to time. Code and data of the necessary parts (common functions) of the program are stored in the memory; optional parts (not commonly used functions) are usually stored in the external memory (overwrite files) and loaded into the memory only when necessary. Modules that do not have a call relationship do not need to be loaded into the memory at the same time, so that they can overwrite each other.

At any time, only the required commands and data are retained in the memory. When other commands are required, they are loaded into the memory space occupied by the commands that are no longer needed;

For example, at the same time point, the CPU can only execute one of B and C. B and C can be overwritten.

 

The disadvantage of overwrite technology is that during programming, the overwrite relationship between the program module and the program module must be divided to increase programming complexity. overwrite files must be loaded from external storage to save time.

There are two methods to implement overwriting: library implementation or operating system support.

3.2 Exchange Technology

Swapping)When multiple programs are concurrently executed, you can send programs (processes) that cannot be executed temporarily to external storage to obtain free memory space for loading new programs (processes ), or the read person is saved in external storage and in the ready state. The switch unit is the address space of the entire process. Exchange technology is often used in multi-channel program systems or small time-based systems, because most of these systems use partition storage management. It is also used in conjunction with partitioned storage management, also known as roll-in/roll-out ).

Principle:Pause the processes in the memory and save the address space of the entire process to the swap zone (swap out ), read the address space of the process from blocking to ready in the external memory to the memory, and send the process to the ready queue (switch to swap in ).

ExchangeOne of the technical advantages is to increase the number of concurrent programs and provide users with appropriate response time. Another significant advantage of exchange technology compared with the coverage technology is that it does not affect the program structure. Exchange technology also has its own shortcomings, such as: Switching and switching out control increase processor overhead; the entire address space of the program is swapped, without considering the statistical characteristics of address access during execution.

3.3 coverage and exchange comparison

1) Compared with the coverage technology, the exchange does not require programmers to provide the coverage structure between program segments.

2) The exchange is mainly between processes and jobs, while the coverage is mainly within the same job or process. In addition, only the blocks irrelevant to the overwriting segments can be overwritten.

4. Page-based and segmented Storage Management

In the previous storage management methods, the space allocated to the process is continuous and the addresses used are physical addresses. If a process can be dispersed into many discontinuous spaces, memory compression can be avoided and fragments can be reduced. Based on this idea, the process address space is separated from the actual storage space by introducing the Logical Address of the process to increase the flexibility of storage management. The basic concepts of address space and storage space are defined as follows:

Address Space: The target program compiled by the source program exists in the address range defined by it. This range is called address space. An address space is a set of logical addresses.

Storage space: a set of physical units that store a series of information in the primary storage. The number of these units is called a set of physical addresses.

Based on the basic unit used for allocation, discrete allocation management can be divided into the following three methods:
Page-based storage management, segment-based storage management, and segment-based storage management. Segment-and-page storage management is the product of the first two combinations.

 

5. Page-based storage management

4.1 Basic Principles

Divide the logical address space of a program into pages of the same size, and divide the physical memory into page frames of the same size ). When a program is loaded, any page can be stored in the memory of any page box, these page boxes do not need to be consecutive, thus achieving discrete distribution. This method requires hardware support from the CPU to map logical and physical addresses. In the page storage management mode, the address structure consists of two parts: the first part is the page number, and the last part is the page address W (displacement), as shown in 4:

Webpage management has the following advantages:

1)There is no external fragment, and each inner fragment cannot exceed the page size. The biggest improvement of the several management methods discussed above is that,

2) A program does not need to be stored continuously.

3) it is easy to change the size of space occupied by the Program (mainly refers to the increase of dynamically generated data as the program runs and the required address space increases accordingly ).

DisadvantagesYes: The program must be fully loaded into the memory. If there is not enough memory, the program cannot be executed.

4.2 Data Structure of page-based Management

When the webpage system is created, the operating system assigns a page box for all pages in the process. When a process is revoked, all the page boxes allocated to it are withdrawn. During the running of the program, if the process is allowed to dynamically apply for space, the operating system also needs to allocate a physical page for the space requested by the process. To complete these functions, the operating system must record the actual page box usage in the system memory. During process switching, the operating system also correctly switches the ing between two different process address spaces to the physical memory space. This requires the operating system to record the related information of each process page table. In order to complete the above functions, the following data structure is generally used in a page-based system.

Process page table: Maps the logical page number (the address space of the current process) to the physical page number (actual memory space, also called block number.
Each process has a page table that describes the physical pages occupied by the process and the logical order,

Figure 4-1 page table

Physical page table:The entire system has a physical page table describing the allocation and usage of physical memory space. Its data structure can beUse bitmap and idle page linked list.

For the bitmap method, that is, if the page has been allocated, the corresponding bit location is 1, no 0.

Figure 4-2 page table

Request table:The entire system has a request table that describes the location and size of each process page table in the system. For address conversion, it can also be combined into the PCB (Process Control Block) of each process.

Figure 4-3 Request table

4.3 page-based management address transformation

In a page system, the address given by the command is divided into two parts: the logical page number and the on-page address.

Principle:The memory management unit (MMU) in the CPU obtains the physical page number by checking the process page table based on the Logical page number, add the box number on the physical page and the address on the page to form a physical address (see Figure 4-4 ). Logical page number, in-page offset address-> query the process page table, obtain the physical page number-> physical address: Figure 4-4 address change in page-based Management

The above process is usually completed directly by the hardware of the processor without the involvement of software. Generally, the operating system only needs to load the first address of the Process page table into the processor-specific register during process switching. Generally, page tables are stored in the primary storage. In this way, each time the processor accesses an operand in the memory, it needs to access the memory twice:

The first time used to find a page table, the logical address of the operand is changed to a physical address;

The second operation completes real read/write operations.

It takes a lot of time. To shorten the search time, you can load the page table from the memory into the associated memory (such as the quick table) in the CPU to perform Content-based search. The address conversion process is: after the CPU provides a valid address, the address translation Organization automatically sends the page number to the user table and compares the page number with all the page numbers in the table, in addition, this comparison is performed at the same time. If there is a page number that matches this, it indicates that the page table items of the page to be accessed are in the quick table. Therefore, you can directly read the physical page number corresponding to the page, so that you do not need to access the page table in the memory. Because the access speed of the associated memory is much faster than that of the memory.

 

5. segmented Storage Management

5.1 Basic Principles

In the Segment Storage Management, the address space of the program is divided into several segments, so that each process has a two-dimensional address space. In the dynamic partition allocation method described above, the system allocates a continuous memory space for the entire process. In the Segment Storage Management System, a continuous partition is allocated for each segment, and each segment in the process can be stored in different memory partitions consecutively. When a program is loaded, the operating system allocates the required memory for all segments. These segments do not need to be contiguous. Physical memory management uses the dynamic partition management method.

When allocating physical memory for a specific segment, you can use methods such as the first adaptation method, the next adaptation method, and the best adaptation method.

When recycling the space occupied by a certain segment, pay attention to merging the reclaimed space with its adjacent space.

Segment Storage management also requires hardware support to map logical addresses to physical addresses.

Programs are divided into multiple modules by segments, such as code segments, data segments, and shared segments:

-You can write and compile them separately-you can use different protection methods for different types of segments-you can share them by segment, including sharing code through dynamic links.

In this way, you can write and compile a file of the source program separately, and use different protection measures for different types of segments, or share them by segment.

In general,Advantages of segmented Storage Management:: No internal fragments. External fragments can be eliminated through memory compression to facilitate Memory Sharing. The disadvantage is the same as that of page-based storage management. All processes must be loaded into the memory.

5.2 data structure of segmented Management

To achieve segment management, the operating system needs the following data structure to map the process address space to the physical memory space and track the usage of the physical memory, so that the memory space can be allocated properly when a new segment is loaded.

·Process Field tableDescription of each segment that constitutes the process address space. It can be an index pointing to table items in the system field table. Each segment has a base address (baseaddress), that is, the intra-segment address.

Create a field ing table for each process in the system,

·System field table: All occupied segments of the system (allocated segments ).

·Idle field table: All idle segments in the memory can be combined into the system segment table.

5.3 segment-managed address transformation

Figure 4-5 address change of segment Management

In the segment management system, the address space of the entire process is two-dimensional, that is, its logical address consists of the segment number and the segment address. To map the logical address of a process to a physical address, the processor searches for the field table in the memory and adds the segment address to the first address obtained by the segment number, obtain the actual physical address (see Figure 4-5 ). This process is also directly completed by the processor's hardware. The operating system only needs to load the first address of the Process Field table into the specific register of the processor during process switching. This register is generally called a field table address register.

 

6. Differences between PAGE and segment Management

There are many similarities between a webpage and a segment system. For example, both use discrete distribution and address ing mechanism to implement address transformation. However, there are also many differences in concept between the two, mainly manifested in:

1) Requirement: it is the physical unit of information. pagination is used to achieve discrete allocation to reduce memory fragments and improve memory utilization. In other words, pagination is only required by system management, not by users. Segment is the logical unit of information. It contains a set of relatively complete information. The purpose of segmentation is to better meet user needs.

An instruction or an operand may span the boundaries of two pages rather than the boundaries of two segments.

2) size: the page size is fixed and determined by the system. The logical address is divided into two parts: the page number and the address in the page, which are implemented by the machine hardware. The length of the segment is not fixed and is determined by the program you write. It is usually divided by the compilation system based on the nature of the information when compiling the source program.

3) logical address representation: the address space of a page system is one-dimensional, that is, a single linear address space. Programmers only need to use one identifier to represent an address. The address space of a segmented job is two-dimensional. When identifying an address, the programmer must provide both the segment name and the intra-segment address.

4) because it is larger than the page, the field table is shorter than the page table, which can shorten the search time and increase the access speed.

 

 

System memory management

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.