Ten years of JAVA moving bricks - operating system memory management

Computer Memory Management Concepts

Computer memory management refers to the management process of the operating system to allocate, use and release the memory resources in the computer system . Memory is an important resource for storing programs and data in a computer system. The goal of memory management is to maximize memory utilization and improve system performance and efficiency.
Memory management includes:
1. Memory allocation : Memory management is responsible for allocating available memory space to processes or applications. It keeps track of which blocks of memory are free and which are allocated, and allocates them as needed. Common memory allocation algorithms include first-fit, best-fit, and worst-fit.
2. Memory mapping : Memory management maps the virtual address space of a process to actual addresses in physical memory. This way, each process can independently access its own memory space without interfering with the memory of other processes.
3. Memory protection : Memory management protects the memory of a process from being illegally accessed or tampered with by other processes by setting access rights, address space isolation and other mechanisms. This ensures system security and stability.
4. Memory recovery : When the process terminates or the allocated memory is no longer needed, the memory management is responsible for reclaiming the memory space and marking it as free for subsequent memory allocation.

physical address logical address concept

The physical address refers to the actual memory address in the computer system, which corresponds to the storage unit in the computer hardware. The physical address is directly managed and used by the hardware, and the operating system cannot directly access or control the physical address. Because the physical address is handled and managed by the memory management unit (MMU) or memory controller in the hardware .
An MMU is a hardware component that sits between a computer's central processing unit (CPU) and physical memory. It is responsible for converting logical addresses (used by the operating system and applications) to corresponding physical addresses and controlling memory access.
When the CPU issues a memory access request, the MMU converts the logical address into a physical address according to the mapping relationship between the logical address and the page table (or segment table). The MMU then passes the physical address to the memory controller for the actual memory access.
The memory controller is the hardware component responsible for communicating with the physical memory modules. It selects the memory location to read or write based on the physical address, and handles details related to memory transfer, such as data reading and writing, data caching, and so on.
Therefore, the physical address is directly controlled and accessed by the MMU and memory controller in the hardware, and the operating system manages and uses the physical address space through cooperation with the hardware.

Logical address space refers to the virtual memory address space used by a process or program. The logical address is relative to the process, and it is the address used by the program at runtime. The logical address space is assigned to each process by the operating system and is mapped to actual physical memory on the physical address space at runtime.

The operating system uses a memory management unit (MMU) to map logical addresses to physical addresses. The MMU is responsible for converting the logical address of the process into the corresponding physical address. This mapping can be realized through data structures such as page tables and segment tables, and these data structures record the correspondence between logical addresses and physical addresses.

The logical address space can be much larger than the actual physical memory space, which is achieved through virtual memory technology. Virtual memory technology allows an operating system to store part of a process's data and code on disk and only load it into physical memory when needed. This provides a larger address space, allows the system to run more processes at the same time, and can better manage memory resources.
Memory relocation, memory protection concept

Relocation refers to the process of converting the address used in the program from a relative address (such as an offset) to an actual physical address when the program is executed or loaded. In computer systems, programs often use relative addresses to refer to data or instructions in memory. The relocation mechanism converts it into an actual physical address by calculating the sum of the relative address and the base address, so that the program can correctly access the data and instructions in the memory.

Memory Protection (Memory Protection) refers to the mechanism of hardware and operating system to protect the memory of the process from being illegally accessed or tampered with. Memory protection can be divided into the following aspects:

  1. Access permissions: The operating system can set access permissions for each process or memory region, such as read-only, read-write, or execute permissions. This can limit the process's access to memory and prevent illegal operations.

  2. Address space isolation: Each process has its own independent virtual address space, so that each process can only access its own memory, but not the memory of other processes. This isolation protects the security of data and code between processes.

  3. Exception handling: The operating system can detect and handle exceptions such as accessing illegal memory or out-of-bounds access. When a memory access error occurs, the operating system can terminate the process or take appropriate measures to protect the stability and security of the system.

Through relocation and memory protection mechanisms, the operating system can ensure that programs can access memory correctly and protect memory from illegal access and damage. This is critical to the proper functioning of the system and the execution of applications.

**

Partition management for memory management

**
Partition management of memory management refers to dividing computer memory into different areas, and each area is used to store different programs or data. This division can improve memory utilization and management efficiency.
The mainstream partition management methods for memory management include:

  1. Fixed Partitioning: In this approach, the memory is divided into partitions or regions of fixed size. Each partition is assigned to a specific process or program. This is a simple and efficient approach, but can lead to internal fragmentation if the partition size is larger than the required memory.

  2. Variable partitions: Unlike fixed partitions, variable partitions allow dynamic allocation of memory based on the size of the program or process. Memory is divided into variable-sized partitions, and each partition is assigned to a process as needed. This approach reduces internal fragmentation, but can lead to external fragmentation.

  3. Dynamic partitioning: Dynamic partitioning is an extension of mutable partitioning. It allows memory to be divided into variable-sized partitions at runtime. When a process requests memory, the operating system searches for a suitable free partition and assigns it to the process. This approach helps optimize memory utilization, but requires more complex memory management algorithms.

  4. Buddy system: Buddy system is a memory allocation technique that divides memory into fixed-size blocks using a binary tree data structure. The size of each block is a power of 2. When a process requests memory, the system finds the smallest available block that can accommodate the requested size. If the chunk is larger than the required size, it is split into two equal-sized buddy chunks. This approach reduces external fragmentation, but can lead to internal fragmentation.

Swap technology is a method of swapping parts of programs or data from memory to disk or other external storage media when memory is insufficient. Through the swapping technology, the memory space can be released for use by other programs, while the state of the swapped out program or data is preserved so that it can be reloaded into the memory when needed.

Overlay technology is a method of dividing a program or data into several modules or segments when memory is limited, and loading different modules or segments into memory when needed. Through the overlay technology, the limited memory space can be effectively used and the utilization rate of the memory can be improved. Every small block will be overwritten. The application of overlay technology is relatively less, and more advanced memory management techniques such as paging and segmentation are used more.

Segment management method:

Segmented memory management is a memory management technique that divides the logical structure of a program into multiple logical segments and allocates an independent memory space for each segment. Each segment represents a logical unit in the program, such as code segment, data segment, heap segment, and stack segment.

In segmented management, each segment has its own start address and length. These segments can be allocated and freed dynamically according to the needs of the program. The size of each segment can vary according to the requirements of the program, thus better adapting to different types of programs.

Compared with traditional fixed partition management and variable partition management, segmented management provides a more flexible memory allocation method. It allows programs to allocate and free memory segments as needed without requiring the entire program to be in memory. This improves memory utilization and supports larger programs.

However, segmented management also introduces some challenges. One of these is external fragmentation, when multiple segments are allocated and deallocated, discontinuous chunks of free memory may be left behind. This can lead to fragmentation of memory, thereby reducing memory utilization efficiency.

In order to solve the problem of external fragmentation, more advanced memory management techniques such as paging and segment page management are usually used. These techniques combine the advantages of segmentation and paging to provide better memory management and utilization efficiency.

Paging management method:

Paging of memory is a memory management technique in which physical memory and logical memory are divided into pages of fixed size. Each page has the same size and is numbered sequentially.

In paging management, the logical address space of a program is divided into logical pages, and the physical memory is divided into physical pages. Mapping between logical pages and physical pages is done through page tables. A page table is a data structure used to record the correspondence between logical pages and physical pages.

When a program accesses a logical address, the operating system converts the page table entry of the logical page into the physical address of the corresponding physical page. This translation process is called address translation or page table lookup. Through paging management, programs can use a contiguous logical address space without caring about the actual layout of physical memory.

Paging management helps to improve memory utilization and flexibility. It allows multiple programs to share physical memory, and pages can be dynamically allocated and deallocated. In addition, paging management also provides a virtual memory abstraction layer, allowing programs to access a logical address space larger than physical memory.

However, paging management also introduces some overhead, such as page table maintenance and address translation time overhead. In order to optimize the performance of paging management, technologies such as advanced page table structure, cache mechanism and page replacement algorithm can be used. These techniques can reduce the time of page table lookup and improve the efficiency of memory access.

**

Segment page management method:

**
Segment paging management is a memory management method that combines the segmentation and paging technologies of memory management. It divides the program's logical address space into segments, and each segment is divided into fixed-size pages. Through segment page management, you can enjoy the advantages of segment management and paging management at the same time.

In segment page management, the logical address of the program consists of two parts: segment number and page number. The corresponding segment table entry can be found through the segment number, and then the corresponding page table entry can be found through the page number. Finally, through the combination of segment table entries and page table entries, logical addresses can be converted into physical addresses.

The main advantage of segment paging management is that it can provide more flexible memory allocation and higher memory utilization. It can dynamically allocate and release segments and pages according to the needs of the program, so as to adapt to programs and data structures of different sizes. In addition, segment page management can also provide better memory protection and security. By setting different permission bits, you can control access to different segments and pages.

Although segment paging has many advantages, it also adds complexity and overhead to memory management. It is necessary to maintain segment tables and page tables, and perform address translation of segment pages. Therefore, in practical applications, segment page management is mainly used in large-scale operating systems and scenarios that require more advanced memory management functions, rather than the commonly used memory management methods.

Personal laptops typically use paged memory management. Paged memory management divides memory into fixed-size pages rather than segments according to the logical structure of the program. The size of each page is usually 4KB or larger. When a program needs to access memory, the operating system translates the logical address into a physical address and loads the required page into physical memory.

The advantage of paged memory management is that it is simple and efficient. It allows the operating system to allocate and reclaim memory in units of pages, providing better memory utilization and flexibility. In addition, paged memory management also provides support for virtual memory, allowing each process to have an independent virtual address space.

It should be noted that personal notebook computers may also use other memory management technologies, such as virtual memory management and memory caching. These techniques can further improve memory utilization and performance. The specific memory management method used may vary depending on the operating system and hardware

Basic concepts of virtual memory


Virtual memory is a technology in the computer operating system, which combines the physical memory and disk space of the computer to provide a virtual memory space for each running program . The concept of virtual memory allows an operating system to temporarily store part of a program's memory on disk instead of loading it all into physical memory. This can effectively expand the available memory space, allowing the computer to run more programs at the same time

The locality principle of virtual memory means that when a program accesses memory, there are characteristics of temporal locality and spatial locality, that is, programs tend to access recently used memory locations or nearby memory locations.

  1. Temporal Locality: This principle states that if a program accesses a memory location, it is likely to access the same memory location again in the near future. This is because programs are often executed in a cyclic or iterative manner, repeatedly accessing the same memory area.

  2. Spatial Locality: This principle states that if a program accesses a certain memory location, it is likely to access nearby memory locations in the next period of time. This is because a program's data is usually stored in memory in a contiguous fashion, and programs tend to process data in chunks.

The principle of locality of virtual memory has an important impact on system performance, because it allows the operating system to take some optimization measures in memory management, such as:

  1. Caching: The operating system can utilize caching mechanisms to store recently used memory data in a cache for faster access.

  2. Page replacement: When the physical memory is insufficient, the operating system can choose to replace some infrequently accessed memory pages to the disk according to the principle of locality, thereby making room for more frequently accessed pages.

  3. Prefetching: The operating system can load data that may be needed into the memory in advance according to the memory access mode of the program to reduce access delays.

Understanding the principle of locality of virtual memory helps to optimize memory management strategies and improve system performance and resource utilization.

page fault

Page fault (Page Fault) means that in the virtual memory system, when the page (Page) accessed by the program is not in the physical memory, an interrupt will be triggered, called page fault interrupt.

When a program accesses a virtual memory address, the operating system will first check whether the page corresponding to the address has been loaded into physical memory. If the page is already in physical memory, it is called a page hit (Page Hit), and the program can directly access the page. But if the page is not in physical memory, it is called Page Fault.

When a page miss occurs, the operating system will perform the following steps:
1. Save the context information of the current program, including register status and program counter, etc.
2. Find the data for the page stored on disk.
3. Load the page from disk into a free page frame (Page Frame) in physical memory.
4. Update the mapping relationship corresponding to the virtual memory address in the Page Table.
5. Restore the context information of the program and re-execute the interrupted instruction.

Through the page fault interrupt mechanism, the operating system can load the pages in the virtual memory into the physical memory on demand, thereby achieving a larger memory space and higher operating efficiency. When a page miss occurs, some delay may be incurred as the page data needs to be loaded from disk. However, through a reasonable page replacement algorithm and prefetching mechanism, the number of missing pages can be minimized and system performance improved.

address translation process

The address translation process of virtual memory can be described by the following steps:

  1. Program accesses virtual memory address: When a program accesses a virtual memory address, it uses the virtual address to address the desired data or instruction.

  2. Mapping of virtual addresses to physical addresses: The Memory Management Unit (MMU) in the operating system is responsible for converting virtual addresses to physical addresses. It uses page table (Page Table) for address mapping.

  3. Page table lookup: MMU looks up the corresponding page table entry (Page Table Entry) in the page table according to the page number part in the virtual address.

  4. Check the valid bit (Valid Bit): In the page table entry, there is a valid bit used to indicate whether the page is in physical memory. If the valid bit is valid, it means that the page is in physical memory and can be accessed directly. If the valid bit is invalid (invalid), it means that the page is not in the physical memory, and paging (Page Replacement) is required.

  5. Page fault interrupt processing: If the valid bit of the page table entry is invalid, it means that a page fault interrupt (Page Fault) has occurred. The operating system handles the interrupt, loads the missing page from disk into physical memory, and updates the valid bits and physical address of the page table entry.

  6. Physical address generation: Once the page table lookup is successful, the MMU will use the physical page number in the page table entry and the page offset of the virtual address to generate the final physical address.

  7. Access to physical memory: Using the generated physical address, the CPU can directly access data or instructions in physical memory.

Through the address translation process, the virtual memory system can convert the virtual address used by the program into a physical address, so as to realize transparent access and management of memory. This address mapping mechanism enables virtual memory to provide a larger memory space, allows multiple programs to run simultaneously, and provides better memory isolation and protection mechanisms.

Guess you like

Origin blog.csdn.net/weixin_43485737/article/details/132607363