Talking about the management of memory in operating system

Source: Internet
Author: User
Tags benchmark

Transfer from http://www.cnblogs.com/CareySon/archive/2012/04/25/2470063.html

Introduction

Memory is one of the most important resources in a computer, and in general, physical memory cannot accommodate all processes. Although the growth of physical memory now reaches N GB, but faster than physical memory growth is the program, so no matter how the physical memory growth, can not catch up with the speed of the program, so the operating system how to effectively manage memory is particularly important. This article describes the past and present of the operating system for memory management, and the introduction of some algorithms for page substitution.

a brief introduction to the process

Before you begin, start with a brief introduction to the process from the operating system perspective. A process is the smallest unit that occupies a resource, which of course includes memory. In modern operating systems, the memory that each process can access is independent of each other (except for some swap areas). The memory space allocated by the process is shared by the thread in the process.

From the operating system point of view, Process = program + data +PCB (Process Control block). This concept is a little bit abstract, I would say by an analogy: for example, you are cooking in the kitchen, while you watch the recipe and follow the recipe to make the ingredients, just then, your son came in to tell you that he scratched the leg, when you stop the work, the recipe back, Then find the first aid book to follow the contents of the book to paste your son's bandage, after you go back to open the recipe, and then continue to cook. In this process, you are like a CPU, recipes are like programs, and the ingredients for cooking are like data. You process the data according to the program instructions, and the first aid work is like a higher priority process, interrupts your current cooking work, then you put the recipe back (protect the scene), to deal with the high-priority process, after processing you continue to read the recipe from the page just now (Restore the scene), and then continue to do the cooking process.

After a brief introduction of the concept of the process, let's go into memory.

The age of no memory abstraction

In earlier operating systems, the concept of memory abstraction was not introduced. The program accesses and operates directly from the physical memory. For example, when the following command is executed:

MOV reg1,1000

This instruction will have no imagination to assign the contents of the physical address 1000 to the register. It is not hard to imagine that this kind of memory operation makes it impossible to have multiple processes in the operating system, such as MS-DOS, you must execute one instruction before you can proceed to the next one. If it is a multi-process, because the direct operation of the physical memory address, when a process to the memory address 1000 assigned value, the other process also assigns the memory address, then the second process of memory assignment overrides the value assigned by the first process, which causes two processes to crash at the same time.

No memory abstraction for memory management is usually very simple, except for the memory used by the operating system, all for the user program. Or leave an extra area in memory for the driver, as shown in 1.

Figure 1: Use of memory when there is no memory abstraction

In the first case, the operating system in RAM, placed in the low address of memory, the second case operating system exists in ROM, there is a high memory address, the general old-fashioned mobile phone operating system is so designed.

If this is the case, if you want the operating system to perform multiple processes, the only solution is to swap with the hard disk, when a process to a certain extent, the whole to the hard disk, to execute other processes, to the need to execute the process, and then retrieve the memory from the hard disk, as long as there is only one process in memory, This is called exchange (swapping) technology. However, this technique still has the potential to cause a process crash due to the direct manipulation of physical memory.

So, usually, this kind of memory operation often exists only in some washing machines, microwave ovens in the chip, because there is no way to have a second process to expropriate memory.

Memory Abstraction

In modern operating systems, it is normal to run multiple processes at the same time. In order to solve the various problems caused by direct operating memory, the address space is introduced, which allows each process to have its own address. This also requires that there are two registers on the hardware, a base register and a boundary register (limit register), the first register holds the start address of the process, and the second register holds an upper bound to prevent memory overflow. In the case of memory abstraction, when executing

MOV reg1,20

At this point, the physical address of the actual operation is not 20, but the actual physical address process operation based on base and offset acreage, at which point the actual address of the operation may be:

MOV reg1,16245

In this case, any operation that operates on the virtual address is converted to the physical address of the operation. Each process has a completely different memory address, which makes it possible for multiple processes to be made.

At this point, however, there is a problem, in general, that the memory size cannot accommodate all concurrently executing processes. As a result, exchange (swapping) technology emerged. This exchange is much the same as the previous one, except that the exchange is now speaking in multi-process conditions. The basic idea of the exchange is to swap idle processes out of memory, temporarily on the hard disk, and then swap back to memory when it is executed, such as the following example, when the program starts with process A, with process B and C gradually, process D is present, but there is not enough space in memory for process D, so process B swaps out memory. To process D. As shown in 2.

Figure 2: Switching technology

By Figure 2, we also found a problem where the space between process D and C is too small to be used by any other process, which is called external fragmentation . One way to do this is through the compact technology (memory compaction) solution, by moving the address of the process in memory, so that these external fragment space is filled. There are also flattering methods, such as memory grooming software, which is the principle of applying an oversized memory, displacing all processes out of memory, and then releasing the memory, allowing the new loading process to eliminate external fragments. That's why it's hard to read a disk after running memory. In addition, the use of compact technology will be very CPU-intensive, a 2G CPU does not 10ns can handle 4byte, so one more 2G of memory for a compact may take several seconds of CPU time.

The theory above is based on the assumption that the memory space occupied by the process is fixed, but in reality, the process tends to grow dynamically, so the memory allocated during the creation of the process is a problem, if the allocation is more, it will produce internal fragments , waste memory, and the allocation of less will cause memory overflow. One workaround is to allocate a bit more memory space for the process to grow than the process actually needs when the process is created. One is the direct allocation of a bit of memory space for the process to grow in memory, and the other is to divide the growth area into data segments and stacks (for storing return addresses and local variables), as shown in 3.

Figure 3: Reserve space for growth when creating a process

When the reserved space is not enough to meet the growth, the operating system will first look at the adjacent memory is idle, if idle is automatically allocated, if not idle, the entire process is moved to enough to accommodate the growth of space memory, if there is no such memory space, the idle process will be replaced.

When allowing the process to grow dynamically, the operating system must be more efficient management of memory, the operating system uses one of the following two methods to know the use of memory, respectively 1) bitmap (bitmap) 2) linked list

Using bitmaps, memory is divided into blocks of equal size, such as a 32K of memory 1K a piece can be divided into 32 blocks, you need 32 bits (4 bytes) to indicate its usage, use a bitmap to mark the already used block as 1, The bit is labeled 0. Instead of using a linked list, the memory is linked by using or not using a plurality of segments, as shown in concept 4.

Figure 4: Bitmaps and linked lists represent memory usage

Use p in the list to represent the process, from 0-2 to process, h for idle, and 3-4 for idle.

Using bitmaps to represent memory is straightforward, but one problem is that when allocating memory, you must search in memory for a large number of contiguous 0 of space, which is a very resource-intensive operation. In contrast, using a linked list would be a better place to do this. There are also some operating systems that use a doubly linked list, because when the process is destroyed, the adjacency is often empty memory or another process. Using a doubly linked list makes it easier to converge between lists.

Also, when using a linked list to manage memory, it is also an issue to allocate what free space is allocated when the process is created. In general, there are several algorithms for allocating space at process creation time.

    • Proximity adaptation algorithm (Next fit)---Search for the first memory space to meet process requirements from the current location
    • Best Fit---Search the entire list to find the memory space that meets the minimum memory required by the process
    • Maximum fit algorithm (wrost fit)---Find the largest free space in the current memory
    • First fit---from the first of the list, find the first memory space to meet the process requirements

virtual Memory (Vsan)

Virtual memory is a technology commonly used in modern operating systems. The above-mentioned abstractions meet the requirements of multiple processes, but in many cases the existing memory does not meet the memory requirements of just one large process (for example, many games are 10g+ levels). In the early operating system used to solve the problem with overlays, a program is divided into several blocks, the basic idea is to first add block 0 memory, block 0 after the execution, block 1 into memory. In turn, the biggest problem with this solution is the need for programmers to block the program, a time-consuming and laborious process. Later, the revised version of this solution is virtual memory.

The basic idea of virtual memory is that each process has a separate logical address space, and that memory is divided into blocks of equal size, called pages . Each page is a contiguous number of addresses. For the process, it seems logically a lot of memory space, some of which corresponds to a piece of physical memory (called a page box , usually page and page box size equal ), and some are not loaded in memory corresponding to the hard disk, 5 shown.

Figure 5: Mapping relationships between virtual memory and physical memory and disk

As you can see from Figure 5, virtual memory can actually be larger than physical memory. When virtual memory is accessed, the MMU (Memory Management Unit) is accessed to match the corresponding physical address (0,1,2 than 5), and if the virtual memory page does not exist in physical memory (5 of 3,4), a page fault is generated, the missing pages from the disk are put into memory, and if the memory is full, The pages in the disk are also swapped out based on an algorithm.

While the virtual memory and physical memory matching is implemented by the page table, the page table exists in the MMU, the page table of each item is usually 32-bit, both 4byte, in addition to store virtual address and page box address, but also store some flag bits, such as whether it is missing pages, modified, write protection and so on. The MMU can be imagined as a method of receiving a virtual address entry to return a physical address.

Because each entry in the page table is 4 bytes, now the 32-bit operating system virtual address space will be 2 32, even if each page is divided into 4 K, it also requires 2 20 bytes of =4m space, it is unwise to build a 4M page table for each process. Therefore, the concept of the page table to promote, generate Level two page table, level two page table each corresponding to the virtual address of 4M, and the first page table to index these two-level page table, so 32-bit system requires 1024 two page table, although the page table entries are not reduced, but in memory can only be used to use the level of the two page table and the first Level page table, Greatly reduces the use of memory.

page Replacement Algorithm

because in a computer system, it usually takes a few milliseconds to read a small amount of data hard disk, and only a few nanoseconds in memory. A CPU instruction is also usually a few nanoseconds, if in the execution of CPU instructions, the number of pages interrupt, the performance is conceivable, so as far as possible to reduce the read from the hard disk is undoubtedly greatly improved performance. As we know, physical memory is extremely limited, when the virtual memory of the page is not in physical memory, you will need to replace the physical memory pages, choose which pages to replace it is particularly important, if the algorithm is not good to replace the future use of the page, then later use will need to be replaced in, This is undoubtedly a reduction in efficiency, let's look at several page replacement algorithms.

Best permutation algorithm (Optimal Page replacement algorithm)

The best permutation algorithm is to replace the longest unused pages in the future, which sounds simple, but not achievable. But this algorithm can be used as a benchmark to measure other algorithms.

Algorithm not used recently (not recently used replacement algorithm)

This algorithm gives each page a flag bit, r indicates that it has been recently accessed, and M indicates that it has been modified. The R is zeroed periodically. The idea of this algorithm is to first eliminate those who have not been visited R=0 page, followed by the R=1, has not been modified m=0 page, and finally the R=1,m=1 page.

FIFO page replacement algorithm (First-in,first-out page replacement algorithm)

The idea of this algorithm is to retire the longest page in memory, and the performance of this algorithm is close to random elimination. And not good.

Improved FIFO algorithm (Second chance Page replacement algorithm)

This algorithm is based on FIFO, in order to avoid the replacement of frequently used pages, add a flag bit r, if recently used will R 1, when the page will be retired, if R is 1, then do not retire the page, will R 0. And those r=0 pages will be eliminated when the direct elimination. This algorithm avoids the frequently used pages being eliminated.

Clock replacement algorithm (clocks Page replacement algorithm)

Although the improved FIFO algorithm avoids the replacement of commonly used pages, it is not efficient to move pages frequently. Therefore, on the basis of the improved FIFO algorithm, the first queue is connected to form a loop, when the page fault is generated, the r=0 pages are searched from the current position, and the R=1 page is set to 0, which does not need to move the page. As shown in 6.

Figure 6: Clock replacement algorithm

Longest unused algorithm (LRU Page replacement algorithm)

The idea of the LRU algorithm is to eliminate the most recently unused pages. This algorithm performs better, but it is difficult to achieve.

The following table is a simple comparison of several algorithms:

Algorithm Describe
Optimal permutation algorithm Not implemented, most test benchmark used
Algorithms are not used very often recently Same as LRU performance
Advanced first-out algorithm It is possible to displace pages that are used frequently
Advanced first-out algorithm It's a big boost compared to FIFO.
The longest unused algorithm Very good performance, but difficult to achieve.
Clock replacement algorithm A very practical algorithm

The above several algorithms more or less have some local principle idea. Local principle is divided into temporal and spatial locality

1. In time, recently visited pages will be visited in the near future.

2. Spatially, pages around pages that are accessed in memory are also likely to be accessed.

Summary

This article simply introduces the management of memory in the operating system. These basic concepts are very helpful for many developers. There is also a segmented management in memory management, that is, a process can have multiple independent logical addresses, and later have time to fill in a post.

Talking about the management of memory in operating system

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.