Hello everyone, here is the public number: Java xiaojie to refueling, this week to share an operating system related knowledge – memory management

  • Don’t say a word. Just drive

Physical address VS virtual address

  • Physical address: Logically, we can think of physical memory as a large array in which each byte can be accessed by a corresponding address, called a physical address

  • Virtual address: The address that the application uses at runtime

The CPU translation process of the virtual address is roughly shown in the figure below

Their inclusion relationship is as follows: CPU contains MMU, and MMU contains TLB

  • CPU
    • MMU(Memory Management Unit):Converts virtual addresses to physical addresses
      • TLB(Address bypass cache Translation Lookaside Buffer): Speeds up the process of address Translation

The usual loading order is

  1. The operating system loads programs from disk (where they were originally stored) into memory
  2. The CPU executes the first instruction of the program but the instruction is now in physical memory
  3. The CPU fetch instruction retrieves the virtual address of the instruction, which is translated by the MMU into a physical address
  4. The request to read the physical address is sent across the bus to the corresponding physical memory, which then sends the instruction to the CPU

piecewise

The MMU has two main mechanisms for translating virtual addresses into physical addresses: segmentation and paging

Segmented mechanism

  • The operating system manages/allocates physical memory in the form of “segments” (contiguous segments of physical memory)
  • The virtual address space of an application consists of several segments of varying sizes: code segments, data segments, and so on
  • When the CPU accesses a segment of the virtual address, the MMU queries the segment table to obtain the physical address of the segment

Virtual address:

  • Segment number: indicates the segment in the virtual address space to which the virtual address belongs
  • Intra-segment Address: Indicates the offset relative to the start address of the segment

When the CPU reads the instruction and finds that the address of the instruction is a virtual address, then the MMU in the CPU first determines whether the segment number is valid. If it is, the POSITION of the segment table can be found through the segment table base address register. The starting address of the segment can be found through the segment number in the virtual address, plus the in-segment address (in-segment offset). You can get the final physical address

  • Under the segmentation mechanism, virtual memory and physical memory are divided into different segments

Segmented faults

  • In the virtual address space, the physical memory space corresponding to adjacent segments may not be adjacent, and the operating system can implement discrete allocation of physical memory resources. However, this segmented allocation mode is prone to external fragmentation of physical memory

The one that doesn’t fit in the picture isExternal fragmentation

The paging mechanism

  • Basic idea:
    • Divide the application’s virtual address space into contiguous, equal-length virtual pages (4K)
    • Physical addresses are also divided into contiguous, equal-length physical pages
    • Physical and virtual pages have the same page length

This is done because it makes it easy for the operating system to construct page tables for each application, namely, virtual and physical page mapping tables

  • With paging, any virtual page in an application’s virtual address space can be mapped to any physical page in physical memory, avoiding the problem of external fragmentation

  • The paging virtual address also consists of two parts: virtual page number: intra-page offset:

The specific process of translation is:

  1. MMU first resolves the virtual page number in the virtual address, checks whether the virtual page number is valid, and finds the corresponding entry in the virtual page table of the application through the virtual page number (the start address of the page table is placed in the page table base address register).
  2. Then pull out the physical page number in that entry
  3. Finally, the physical start address corresponding to the physical page number plus the in-page offset in the virtual address is used to obtain the final physical address

TLB

First, let’s talk about the locality principle

  • Time locality: If an instruction in a program is executed, it is likely to be executed again shortly thereafter, and if some data has been accessed, it is likely to be accessed again shortly thereafter (because of the large number of loops in the program)
  • Spatial locality: Once a program accesses a storage unit, it is likely that nearby storage units will be accessed shortly thereafter (because much of the data is stored contiguously in memory)

So, what if we could create a cache that would cache data that might be accessed more often, and reduce the number of visits to the page table?

In order to reduce the number of visits to address Translation, MMU introduces TLB (Translation bypass cache)

  • TLB hardware uses a layered architecture,It is divided into L1 and L2.
    • LI is divided into data TLB and instruction TLB, which cache the address translation of data and instruction respectively
    • L2 does not distinguish between data and instructions
  • TLBThe cacheThe mapping between virtual page numbers and physical page numbers is similar to map, where key is the virtual page number and value is the physical page number.
    • If found in TLB, it is called a TLB hit
    • If you don’t find it, you call it a TLB miss

With TLB, the query becomes

    1. The MMU first parses the virtual page number in the virtual address and checks whether the virtual page number is valid
    • Check the TLB. If a match is found, the initial physical address is directly obtained and the intra-page offset is added to obtain the final physical address. Otherwise, continue to query the page table
    • If a physical initial address exists in the page table, the physical initial address is cached in the TLB

Use this virtual page number to find the corresponding entry in the application’s virtual page table (the start address of the page table is placed in the page table base address register) 2. Then fetch the physical page number in the entry. 3. Add the physical start address corresponding to the physical page number and the in-page offset in the virtual address to obtain the final physical address

Multistage page table

  • What if the page table is too large, and the page table has to be stored consecutively, which takes up a lot of memory, so you split a large table into many smaller tables

The access order after splitting is shown in the figure

  • Find the physical page number according to the primary page number, which contains the address of the secondary page table, after finding this address, according to the secondary page number

Finds the physical address, which is the final physical address plus the in-page offset

Page break and missing exceptions

Change the page

Paging in virtual memory: When physical memory is insufficient, the operating system should write the contents of several physical pages to a large space such as disk, then reclaim the physical pages and continue to use them

For example, if the virtual page K of application A corresponds to physical page V, the operating system wants to reclaim physical page V. What should be done?

    1. The operating system writes V to disk
    1. In addition, delete the mapping between virtual page K and physical page V in the page table of A, and record the corresponding location of physical page V on the disk

These two are called the physical page V swaps

Missing page exception

Missing page exceptions are a prerequisite for the paging mechanism to work. They are triggered when an application accesses a virtual page that has been allocated but not mapped to physical memory

  • How to solve: passin
      1. The CPU runs a page missing exception handler preconfigured by the operating system, which finds a free physical page,
      1. Reloads the content previously written to disk into the free physical page
      1. The virtual address is then mapped to the physical address

After all this is done, the CPU goes back to where the page missing exception occurred and continues running

Segment-page memory management

  • Piecewise management
    • Advantages: It is very convenient to realize information sharing and protection according to logical modules
    • Disadvantages: Prone to external fragmentation
  • Paging management
    • Advantages High memory space utilization, no external fragmentation, only a small amount of in-page fragmentation
    • Disadvantages: It is not convenient to share and protect information according to logical modules
  • Segment-page memory management
    • The address space is divided into several layers according to the logical relationship of the program itself, and each section is divided into equal pages
    • Physical memory and virtual memory are divided into equal memory blocks. The system allocates memory for processes in blocks
    • Logical address/virtual address (segment number, page number, in-page offset)

The procedure for translating virtual addresses into physical addresses becomes

    1. Obtain the segment number based on the logical address and check whether the segment number is normal
    1. If yes, the initial page table address corresponding to the segment number is found
    1. If the page number is normal, find the physical initial address according to the page number, and add the page offset to find the real physical address

Phase to recommend

  • How is the network connected from the four – layer model
  • I’m confused by the various locks in mysql
  • Five thousand words, yes, we do have an HTTP.
  • The interviewer of JINGdong asked me: “Talk about MySql affairs,MVCC?”