Home > Common Problem > Is virtual memory part of computer memory?

Is virtual memory part of computer memory?

青灯夜游
Release: 2020-08-19 15:19:14
Original
6921 people have browsed it

Virtual memory is a portion of the computer's physical memory. Virtual memory is a technology for computer system memory management. It is usually divided into multiple physical memory fragments, and some are temporarily stored on external disk storage for data exchange when needed.

Is virtual memory part of computer memory?

#The operating system has the concepts of virtual memory and physical memory. A long time ago, when there was no concept of virtual memory, programs used physical addresses for addressing. The range that a program can address is limited, depending on the number of address lines of the CPU. For example, on a 32-bit platform, the addressing range is 2^32, which is 4G. And this is fixed. If there is no virtual memory and 4G of physical memory is given every time a process is started, many problems may occur:

  • Because my physical memory is limited. Yes, when there are multiple processes to be executed, 4G of memory must be allocated. Obviously if your memory is smaller, it will be allocated quickly, so the processes that have not been allocated resources can only wait. When a process has finished executing, the waiting process is loaded into memory. This frequent operation of loading memory is very inefficient

  • Since the instructions directly access the physical memory, then my process can modify the data of other processes, and even Modifying the data in the kernel address space is something we don’t want to see

  • Because the memory is randomly allocated, the address where the program runs is also incorrect.

So in response to the various problems that will arise above, virtual memory came out.

When a process is running, it will get 4G of virtual memory. You can think of this virtual memory. Each process thinks it has 4G of space. This is only what each process thinks. But in fact, the physical memory corresponding to the virtual memory may only correspond to a small amount of physical memory. In fact, How much memory is used will correspond to how much physical memory is used.

The 4G virtual memory obtained by the process is a continuous address space (this is only what the process thinks), but in fact, it is usually divided into multiple physical memory fragments, and some are stored on external disks. On the memory, data is exchanged when needed.

When a process starts to access an address, it may go through the following process

  • Every time I want to access an address in the address space, I need to translate the address For the actual physical memory address

  • all processes share this entire physical memory, and each process only maps the virtual address space it currently needs to physical memory

  • The process needs to know which data in the address space is in the physical memory, which is not (maybe this part is stored on the disk), and where it is in the physical memory, which needs to be recorded through the page table

  • Each entry in the page table is divided into two parts. The first part records whether the page is in physical memory, and the second part records the address of the physical memory page (if it is)

  • When a process accesses a virtual address, it will first look at the page table. If it is found that the corresponding data is not in the physical memory, a page fault exception will occur

  • In the handling process of page missing exception, the operating system immediately blocks the process, swaps the corresponding page from the hard disk into the memory, and then makes the process ready. If the memory is full and there is no free space, then find a Page coverage, as for which page is specifically covered, it depends on how the operating system's page replacement algorithm is designed.

Regarding the connection between virtual memory and physical memory, the following picture can help us consolidate.

Is virtual memory part of computer memory?

The working principle of the page table is as follows

Is virtual memory part of computer memory?

  • Our CPU wants to access the virtual address For the virtual page (VP3) where it is located, according to the page table, find the value of the third entry in the page table and determine the valid bit. If the valid bit is 1, the DRMA cache hits. According to the physical page number, the content in the physical page is found and returned.

  • If the valid bit is 0, the parameter page fault exception occurs, and the kernel page fault exception handler is called. The kernel selects a page as the overwritten page through the page replacement algorithm and refreshes the content of the page to the disk space. Then cache the VP3 mapped disk file to the physical page. Then in the third entry in the page table, the valid bit becomes 1, and the second part stores the content that can correspond to the address of the physical memory page.

  • After the page fault exception is processed, return to the instruction before the interruption and re-execute it. At this time, the cache hits and execute 1.

  • Map the found content to the notification cache, and the CPU obtains the value from the notification cache and ends.

Let’s summarize how virtual memory works

When each process is created, the kernel will allocate 4G of virtual memory to the process. When the process has not started running, this is just a memory layout. In fact, the program data and code (such as .text.data segment) at the corresponding location in the virtual memory are not immediately copied to the physical memory. It is just a mapping between the virtual memory and the disk file (called memory mapping). At this time, the data and code are still on the disk. When the corresponding program is run, the process looks for the page table and finds that the address in the page table is not stored in physical memory, but on the disk, so a page fault exception occurs, so the data on the disk is copied to physical memory.

In addition, when the process is running, when memory is dynamically allocated through malloc, only virtual memory is allocated, that is, the page table entry corresponding to this virtual memory is set accordingly. When the process actually accesses this A page fault exception is triggered when the data is retrieved.

It can be considered that virtual space is mapped to disk space (in fact, it is also mapped to disk space as needed, through mmap, which is used to establish the mapping relationship between virtual space and disk space)

Advantages of utilizing the virtual memory mechanism

  • #Since the memory space of each process is consistent and fixed (4G under 32-bit platforms), Therefore, when linking an executable file, the linker can set the memory address without caring about the final actual memory address of the data. This is left to the kernel to complete the mapping relationship

  • When different When a process uses the same piece of code, such as the code of a library file, only one copy of such code can be stored in physical memory. Different processes only need to map their own virtual memory to it, which can save physical memory

  • When the program needs to allocate continuous space, it only needs to allocate continuous space in virtual memory, and does not need continuous physical memory. In fact, physical memory is often intermittent memory fragments. In this way, we can effectively utilize our physical memory

For more related knowledge, please visit: PHP Chinese website!

The above is the detailed content of Is virtual memory part of computer memory?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template