Virtual Memory Learning Record

Source: Internet
Author: User

Important points of knowledge

Virtual memory

A modern system provides an abstract concept of main memory. Virtual memory is the perfect interaction between exceptions, hardware address translation, main memory, disk files, and kernel software, which provides a large, consistent, and private address space for each process. It has three important capabilities:

    1. It regards main memory as a cache of address space stored on disk, storing only active areas in main memory and transmitting data back and forth between the main memory of the disk as needed, in this way, the main memory is used efficiently.
    2. It simplifies storage management by providing a consistent address space for each process.
    3. It protects the storage space of each process from being destroyed by other processes.

Virtual addressing

The CPU accesses the main memory by generating a virtual address, which is converted to the appropriate physical address before being sent to the memory. The task of replacing a physical address with a virtual address is called address translation. the dedicated hardware of the Interleaved Memory management unit on the CPU chip uses the query table stored in main memory to dynamically translate the virtual address, which is managed by the operating system.

Scheduling of pages

This part of the operating system in the course of the teacher has done a detailed explanation, here is not more than repeat.

Virtual Storage for Linux

Linux organizes virtual storage into a collection of areas (also called segments). A region is a contiguous slice of the already existing (allocated) virtual memory, which is associated in some way.

Linux pages exception handling

When the MMU tries to translate a virtual address A, it triggers a missing pages. This exception causes control to be transferred to the kernel's fault-handling program, and the handler then performs the following steps:

    1. Is virtual address a legal?
    2. Is the attempted memory access legal?
    3. After processing the above two problems, the kernel knows that the fault is due to legitimate operation of the virtual address. Then process: Select a Sacrifice page, if the Sacrifice page has been modified, then swap it out, swap in a new page and update the page table. When the fault-message handler returns, the CPU restarts the instruction that caused the missing pages, and this instruction sends a to the MMU again. Then a will be able to translate a normally.

Memory mapping

Linux initializes the contents of this virtual memory area by associating a virtual memory area with an object on a disk, a process known as a memory map .

The virtual memory area can be mapped to two types of objects:

    • Ordinary files in the Unix file system.
    • anonymous files.

Memory mapping provides us with a clear mapping mechanism for controlling multiple processes to share objects.

    • If a process is to map a storage object to a region of its virtual address space, then any write to this zone by the process is visible to other processes that also map the shared object to its virtual address space, and these changes are reflected in the original file object on disk.
    • Changes made to a region that is mapped to a private object are not visible to other processes, and any writes that the process makes to the zone do not reflect on the disk's objects.

Dynamic memory allocation

The dynamic memory allocator maintains a virtual memory area of a process, called a heap. The allocator maintains the heap as a collection of blocks of different sizes. Each block is a contiguous virtual memory slice, either allocated or idle.

    • The allocated block is explicitly reserved for use by the application.
    • Free blocks are available for allocation. The free block remains idle until it is explicitly assigned by the application.

There are two basic styles of dispensers

    • Explicit allocator: Requires an application to explicitly release any allocated block.
      • Working conditions: Processes any request sequence, responds to requests immediately, uses only heaps, aligns blocks, and does not modify allocated blocks.
      • Goal: Maximize throughput and maximize memory utilization.
    • Implicit allocator: The allocator is required to detect when an allocated block is no longer in use by the program, then release the block. So an implicit allocator is called a garbage collector , and the process of automatically releasing unused allocated blocks is called garbage collection .

The important reason for using dynamic memory allocation is that until the program actually runs, they know the size of some data structures and can effectively utilize the memory resources.

Fragments

The phenomenon of having unused memory but not satisfying the allocation request is called fragmentation. Fragments are made up of two forms:

    • Internal fragmentation.
    • External fragments.

Allocator Common placement Strategy

    • First fit: Search the list from the beginning and select the first appropriate free block.
    • Next adaptation: Search the list from where it last ended, and select the first appropriate free block.
    • Best fit: Check each free block and select the minimum free block for the desired request size.

Data structure of the allocator

    • An implicit idle list.
    • An explicit idle list.

Detach Storage

One way to reduce allocation time is to maintain multiple idle lists, where the blocks in each list are roughly equal in size.

Basic methods for separating storage:

    • Simple separation of storage.
    • Separation adaptation.
    • Partner system.

Garbage collection

A garbage collector is a dynamic storage allocator that automatically frees programs from the allocated blocks that are not needed. These blocks are called garbage. The process of automatically reclaiming heap storage is called garbage collection. The garbage collector treats the memory as a forward-to-reach graph. The nodes of the graph are divided into a set of root nodes and a set of heap nodes. Each heap node corresponds to an allocated block in the heap.

Memory-related errors common in the C language

    • Indirectly referencing bad pointers.
    • Read uninitialized memory.
    • Allow stack buffer overflow.
    • Suppose the pointers are the same size as the objects they point to.
    • Cause dislocation errors.
    • References a pointer, not the object it points to.
    • Misunderstood pointer arithmetic.
    • References a variable that does not exist.
    • Cause a memory leak.

Resources

"In-depth understanding of computer systems" 9 Chapter Virtual Memory

Virtual Memory Learning Record

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.