Condensed Notes on Operating Systems (3) - Memory Management

 Main quotes from the article notes:

Axiu’s study notes (interviewguide.cn)

Kobayashi coding (xiaolincoding.com)

What does the operating system need to do when managing memory?

  • The operating system is responsible for allocating and reclaiming memory space.
  • The operating system needs to provide some technology to logically expand the memory space.
  • The operating system needs to provide an address translation function, which is responsible for converting the logical address and physical address of the program.
  • The operating system needs to provide memory protection capabilities. Ensure that each process runs in its own storage space without interfering with each other

Explain the basic process of converting logical addresses into physical addresses through examples

The conversion of virtual addresses into physical addresses is completed  through the Memory Management Unit ( MMU ). The MMU is a hardware component in a computer system that is responsible for converting between virtual addresses and physical addresses.

In the process of virtual address translation, a page table (Page Table) is usually used for mapping. The page table is a data structure that divides the virtual address space into fixed-size pages (Page), corresponding to the page frame (Page Frame) in physical memory . Each page table entry (Page Table Entry) records the corresponding relationship between virtual pages and physical pages.

When a program accesses a virtual address, the MMU breaks the virtual address into a page number and an offset within the page . Find the corresponding page table entry based on the page number . The page table entry contains the address or page frame number of the physical page . Finally, the MMU combines the address of the physical page with the offset within the page to obtain the corresponding physical address.

picture

Logical address VS physical address

Eg: When compiling, you only need to make sure that the relative address stored in variable x is 100 (that is, the address relative to the starting address of the process in memory). If the CPU wants to find the actual storage location of x in memory, it only needs to use the starting address of the process + 100. Relative addresses are also called logical addresses, and absolute addresses are also called physical addresses.

The relationship between swap space and virtual memory

swap space

Swap space in Linux is used when physical memory (RAM) is full. If the system requires more memory resources and physical memory is full, inactive pages in memory will be moved to swap space. Although swap space can help on machines with small amounts of memory, this approach should not be considered a replacement for memory. Swap space is on the hard drive and is slower than getting into physical memory. Swap space can be a dedicated swap partition (the recommended approach), a swap file, or a combination of both. The total size of swap space should be equal to the larger of two times your computer's memory or 32 MB, but it cannot exceed 2048MB (2 GB).

Virtual Memory

Virtual memory is an active file cross-linked with file data. It is a "WIN386.SWP" file in the WINDOWS directory. This file will continue to expand and automatically shrink. In terms of speed, the CPU's L1 and L2 caches are the fastest, followed by memory and hard disk. But virtual memory uses the space of the hard disk . Why do we use the slowest hard disk as virtual memory? Because all programs running in the computer need to be executed through memory, if the executed programs are large or many, our poor 256M/512M memory will be exhausted. The hard disk space can easily be tens of gigabytes or hundreds of gigabytes. In order to solve this problem, Windows uses virtual memory technology, which uses part of the hard disk space as memory.

Why does segmented storage management have external fragmentation but no internal fragmentation? Why do fixed partition allocations have internal fragmentation but not external fragmentation?

Memory segmentation (segmented) management can allocate memory according to actual needs , so segments will be allocated as large as there are needs, so there will be no internal memory fragmentation .

However, since the length of each segment is not fixed, multiple discontinuous small physical memories will be generated, causing new programs to be unable to be loaded, so the problem of external memory fragmentation will occur .

Memory paging (fixed allocation) Since the memory space is pre- divided , there will not be very small gaps between segments like memory segmentation. Paging is used to separate pages from each other. are packed tightly so there will be no external debris.

However, because the minimum unit of memory allocated by the memory paging mechanism is one page, even if the program is less than one page in size, we can only allocate at least one page, and the memory paging mechanism will cause internal memory fragmentation.

How to eliminate fragmented files

For external fragmentation , elimination through compaction technology means that the operating system moves and organizes the memory from time to time . However, this requires the support of dynamic relocation registers and is relatively time-consuming.

The solution to the problem of external memory fragmentation is memory swapping .

You can write the 256MB of memory occupied by the music program to the hard disk, and then read it back from the hard disk to the memory. However, when reading back, we cannot load it back to the original position, but immediately behind the 512MB of memory that has been occupied. This will free up a continuous 256MB space, so a new 200MB program can be loaded.

When reclaiming memory, merge adjacent free spaces as much as possible .

In memory swap, where are the swapped out processes stored?

Saved on disk, that is, in external memory. In operating systems with swap functions, the disk space is usually divided into two parts: the file area and the swap area. The file area is mainly used to store files, and the utilization rate of storage space is mainly pursued. Therefore, the management of the file area space adopts a discrete allocation method ; the swap area space only occupies a small part of the disk space, and the swapped out process data is stored in the swap area. . Since the speed of swapping directly affects the overall speed of the system, the management of the swap area space mainly pursues the speed of swapping in and out. Therefore, the swap zone is usually allocated continuously (you can understand it after studying the file management chapter ) . In short, the I/O speed of the swap area is faster than that of the file area.

What is the difference between memory swapping and overwriting?

Swapping technology is mainly performed between different processes (or jobs), while overwriting is used within the same program or process.

What is memory coverage? What are the characteristics?

Since the program does not need to access all parts of the program and data at all times (especially large programs), the user space can be divided into a fixed area and several coverage areas. Put the frequently active parts in the fixed area , and segment the remaining parts according to the calling relationship. First, put the segments that are about to be accessed into the coverage area, and put the other segments in the external memory . Before they need to be called, the system will transfer them into the coverage area. , replace the original segments in the coverage area.

The characteristics of overlay technology: it breaks the restriction that all the information of a process must be loaded into the memory before it can be run. However, when the amount of code running the program at the same time is larger than the main memory, it still cannot run. Furthermore, everyone should note that Only the segments in the coverage area can be updated , and the segments not in the coverage area will remain in memory.

What is memory swapping? What are the characteristics?

The design idea of ​​swap (swap) technology : When the memory space is tight, the system temporarily swaps out some processes in the memory and into the external memory, and swaps out some processes in the external memory that have running conditions into the memory (processes dynamically move between the memory and the disk). Scheduling)

Swap in: Move the ready program from auxiliary storage to memory. Swap out: Move the program in the waiting state (or deprived of the right to run under the CPU scheduling principle) from the memory to the auxiliary storage to free up the memory space.

When will the memory swap be performed?

Memory swapping is usually performed when many processes are running and memory is tight, and is paused when the system load decreases. For example: If you find that page faults often occur when many processes are running , it means that the memory is tight. At this time, you can swap out some processes; if the page fault rate drops significantly, you can pause the swap out.

Do you know the key points that need to be paid attention to when swapping memory?

  1. Swapping requires backup storage , usually a fast disk, which must be large enough and provide direct access to these memory images .
  2. In order to effectively use the CPU, the execution time of each process needs to be longer than the swap time , and the main factor affecting the swap time is the transfer time, which is proportional to the space memory being swapped.
  3. If you swap out a process, for example, make sure it's proportional to the memory space of that process .
  4. Swap space is usually presented as an entire block of disk and is independent of the file system , so it can be used very quickly.
  5. Swapping usually starts when there are many processes running and memory space is tight , and is paused when the system load decreases .
  6. Ordinary exchange is not used much, but some variants of the exchange strategy still play a role in many systems (such as UNIX systems).

What is the purpose of virtual memory?

The purpose of virtual memory is to expand physical memory into larger logical memory , so that the program can obtain more available memory.

In order to better manage memory, the operating system abstracts memory into an address space. Each program has its own address space , which is divided into multiple blocks, each block is called a page.

The pages are mapped into physical memory, but do not need to be mapped into contiguous physical memory, nor do all pages need to be in physical memory. When a program references a page that is not in physical memory, the hardware performs the necessary mapping, loads the missing portion into physical memory, and re-executes the failed instruction.

As can be seen from the above description, virtual memory allows the program to not map every page in the address space to physical memory , which means that a program does not need to be loaded into the memory to run, which makes the operation of limited memory large. program possible.

For example, if a computer can generate 16-bit addresses, then the address space range of a program is 0~64K. The computer has only 32KB of physical memory, and virtual memory technology allows the computer to run a 64K program.

When a program dynamically allocates memory from the heap, how does it operate on virtual memory?

Page table: It is a data structure stored in physical memory. It records the mapping relationship between virtual pages and physical pages.

When performing dynamic memory allocation, such as the malloc() function or the new keyword in other high-level languages, the operating system will create or apply for a virtual memory space in the hard disk and update it to the page table ( allocate a page table entry (PTE) , so that the PTE points to the newly created virtual page on the hard disk), and the mapping relationship between the virtual page and the physical page is established through the PTE .

If there is a fast table in the system, what will happen to the address translation process?

①The CPU gives the logical address , and a piece of hardware calculates the page number and offset within the page , and compares the page number with all page numbers in the fast table .

② If a matching page number is found , it means that the page table entry to be accessed has a copy in the fast table, then the memory block number corresponding to the page is directly taken out , and then the memory block number and the page offset are spliced ​​to form a physical address . Finally, access the memory unit corresponding to the physical address. Therefore, if the fast table hits, accessing a certain logical address only requires one memory access.

③If no matching page number is found , you need to access the page table in the memory , find the corresponding page table entry , get the memory block number where the page is stored, and then concatenate the memory block number and the page offset to form a physical address . Finally, Access the memory unit corresponding to the physical address. Therefore, if the fast table misses, accessing a certain logical address requires two memory accesses (note: after finding the page table entry, it should be stored in the fast table at the same time for possible access again later. But if the fast table has been If it is full, the old page table entries must be replaced according to a certain algorithm)

Memory distribution

  • Code segments, including binary executable code;
  • Data segment, including initialized static constants and global variables;
  • BSS section, including uninitialized static variables and global variables;
  • The heap segment, including dynamically allocated memory, grows upward starting from low addresses;
  • File mapping segments, including dynamic libraries, shared memory, etc., start from low addresses and grow upward ( depending on the hardware and kernel version (opens new window) );
  • Stack segment, including local variables and function call context, etc. The size of the stack is fixed, generally  8 MB. Of course, the system also provides parameters so that we can customize the size;

As you can see from the memory layout in the picture above, there is a section of memory space (gray part) below the code segment. This area is the "reserved area". The reason why there is a reserved area is because in most systems, we It is considered that an address with a relatively small value is not a legal address . For example, we usually assign invalid pointers to NULL in C code . Therefore, there will be an inaccessible memory reserved area here to prevent the program from reading or writing data at small memory addresses due to bugs, causing the program to run away.

Which is faster to create objects from the heap or the stack? (Examine the allocation efficiency comparison of heap and stack)

Consider it from two aspects:

  • Allocating and releasing, the heap must call functions (malloc, free) when allocating and releasing. For example, when allocating, it will go to the heap space to find a space of sufficient size (because multiple allocations and releases will cause memory fragmentation), which will cost a certain amount of time . For details, you can look at the source code of malloc and free. The functions do a lot of extra work, but the stack does not need this.

  • Access time: accessing a specific unit of the heap requires two accesses to the memory . The first time is to obtain the pointer, and the second time is the real data, while the stack only needs to be accessed once. In addition, the probability that the contents of the heap will be swapped to external memory by the operating system is greater than that of the stack, and the stack will generally not be swapped out.

What are the parts of the memory occupied by a program compiled by C/C++?

1. Stack area (stack) - the address grows downward, is automatically allocated and released by the compiler , and stores function parameter values, local variable values, etc. It operates like a stack in a data structure, first in, last out.

2. Heap area (heap) - the address grows upward and is generally allocated and released by the programmer . If the programmer does not release it, it may be recycled by the OS when the program ends. Note that it is different from the heap in the data structure, and the allocation method is similar to a linked list .

3. Global area (static area) (static) - global variables and static variables are stored together. Initialized global variables and static variables are in one area, and uninitialized global variables and uninitialized static variables are in adjacent areas. of another area. - System release after the program ends

4. Literal constant area —constant strings are placed here. Released by the system after the program ends

5. Program code area (text)—stores the binary code of the function body.

What are the common memory allocation methods?

Memory allocation method

(1) Allocated from static storage area . Memory is allocated when the program is compiled, and this memory exists throughout the entire runtime of the program . For example, global variables and static variables.

(2) Create on the stack. When executing a function, storage units for local variables within the function can be created on the stack, and these storage units are automatically released when the function ends. The stack memory allocation operation is built into the processor's instruction set and is very efficient, but the allocated memory capacity is limited.

(3) Allocation from the heap, also known as dynamic memory allocation. When the program is running, you can use malloc or new to apply for any amount of memory . The programmer is responsible for when to use free or delete to release the memory . The lifetime of dynamic memory is determined by us, and its use is very flexible, but it also has the most problems.

Under normal circumstances, the size of the stack space under Linux/windows platform

In the Linux environment, it is determined by the operating system, usually 8MB, 8192KB, and can be viewed and modified through the ulimit command.

In Windows environment, it is determined by the compiler. VC++6.0 is generally 1M.

What are the dynamic partition allocation algorithms? Can you talk about them separately?

1. First adaptation algorithm

Algorithm idea: Start searching from the low address every time and find the -th free partition that can meet the size.

How to implement: Free partitions are arranged in increasing order of address . Each time memory is allocated, the free partition chain (or free partition table) is searched sequentially and the first free partition whose size meets the requirements is found.

2. Best adaptation algorithm

Algorithm idea: Since dynamic partition allocation is a continuous allocation method, the space allocated to each process must be a continuous entire area. Therefore, in order to ensure that there is a continuous large space when the "big process" arrives, you can leave as many large free areas as possible, that is, give priority to using smaller free areas .

How to implement: Free partitions are linked in order of increasing capacity . Each time memory is allocated, the free partition chain (or free partition table) is searched sequentially and the first free partition whose size meets the requirements is found. 

3. Worst adaptation algorithm

Also known as Largest Fit algorithm

Algorithm idea: In order to solve the problem of the best adaptation algorithm - that is, leaving too many small fragments that are difficult to use , the largest continuous free area can be used first in each allocation, so that the remaining free area after allocation will not be too small. More convenient to use.

How to implement: Free partitions are linked in descending order of capacity . Each time memory is allocated, the free partition chain (or free partition table) is searched sequentially and the first free partition whose size meets the requirements is found. 

4. Neighbor Adaptation Algorithm

Algorithm idea: The first adaptation algorithm searches from the head of the chain every time. This may lead to many small free partitions in the low address part, and each allocation search must go through these partitions, thus increasing the search overhead. If you start searching from the end of the last search every time , you can solve the above problem.

How to implement: Free partitions are arranged in increasing order of addresses (can be arranged in a circular linked list). Each time memory is allocated, the free partition chain (or free partition table) is searched starting from the end of the last search, and the first free partition whose size meets the requirements is found. 

5. Summary

First adaptation is not only the simplest, but also usually the best and fastest. However, the first adaptation algorithm will cause many small free partitions to appear in the low address part of the memory, and each search must go through these partitions, thus increasing the search overhead. The proximity algorithm attempts to solve this problem, but in practice it often results in the allocated space being split into small fragments at the end of the memory, which usually results in worse results than the first fit algorithm.

At best it results in a lot of fragmentation, at worst it results in no big space.

In experiments, the first adaptation is better than the best adaptation, and they are all better than the worst.

algorithm Algorithmic thinking Partition sort order advantage shortcoming
first adaptation Find the right partition from beginning to end Free partitions are arranged in increasing order of address Overall performance is the best. The algorithm has low overhead , and there is generally no need to reorder the free partition queue after recycling the partitions.
best fit Prioritize smaller partitions to retain more large partitions Free partitions are arranged in increasing order of capacity. More large partitions will be retained to better meet the needs of large processes. It will generate a lot of fragments that are too small and difficult to use; the algorithm is expensive and it may be necessary to reorder the free partition queue after recycling the partitions.
worst adaptation Prioritize larger partitions to prevent fragmentation that is too small to be usable Free partitions are arranged in descending order of capacity Can reduce small fragments that are difficult to use Large partitions are easily used up, which is not conducive to large processes; the algorithm is expensive (the same reason as above)
proximity adaptation Evolved from the first adaptation, each search starts from the end of the last search. Free partitions are arranged in increasing order of addresses (can be arranged into a circular linked list) There is no need to start retrieval from the small partition with a low address every time. The algorithm has low overhead (the reason is the same as the first adaptation algorithm) This will cause large partitions with high addresses to be used up.

Do you know what dither is? It is also called bumping phenomenon

The page just swapped out will be swapped out of memory immediately , and the page just swapped in will be swapped out of external memory immediately. This frequent page scheduling behavior is called jitter, or thrashing. The main reason for thrashing is that the number of pages frequently accessed by the process is higher than the number of available physical blocks (not enough physical blocks are allocated to the process)

Allocating too few physical blocks to a process can cause thrashing in the process. Allocating too many physical blocks to a process will reduce the overall concurrency of the system and reduce the utilization of certain resources. In order to study how many physical blocks should be allocated to each process, Denning proposed the concept of "process working set"

process working set

For each process, the virtual memory manager keeps a certain number of its . And track the performance indicators of its execution and dynamically adjust this number . The memory pages that reside in physical memory in Win32 are called the "working set" of the process. The working set of the process can be viewed through the "Task Manager", where the "Memory Usage" column is the working set size.

At the beginning of the process, only a few code pages and data pages are loaded into memory. When code that has not been executed or data that has not been transferred into memory is accessed, these code pages or data pages will be transferred into physical memory, and the working set will also grow. However, the working set cannot grow infinitely. The system defines a default minimum working set (depending on the system physical memory size, this value may be 20~50 MB) and a maximum working set (depending on the system physical memory size, this value) for each process. May be 45~345 MB). When the working set reaches the maximum working set, that is, when the process needs to transfer new pages into physical memory again, the virtual memory manager will first replace some pages in its original working set and then transfer the new pages that need to be transferred in. Load into memory.
 

Guess you like

Origin blog.csdn.net/shisniend/article/details/131863568