What is virtual memory?

What is virtual memory? First check Wikipedia:

Virtual memory is a computer system memory management technology. It makes the application thinks it has a memory (a continuous full address space) continuously available, in fact, it is usually separated into a plurality of pieces of physical memory, and temporarily stored on an external part of the disk storage, when necessary for data exchange. Compared with the system does not use virtual memory technology, systems written using this technology allows large programs easier, for real physical memory (such as RAM use) are also more efficient.

The definition of virtual memory is based on the address space redefined, that the address space is defined as "continuous virtual memory address" to take "cheat" program, to make them think they are using a large piece of "continuous" address.

That can provide a chunk of virtual memory address space is continuous, it is a program for continuous, complete, in fact, virtual memory is mapped on a plurality of physical memory fragmentation, there are some maps to the external disk storage . Virtual memory has two advantages:

1. virtual memory address space is continuous, free of debris

2. The maximum virtual memory space is the biggest cup of address space, unlimited memory size can provide larger address space than memory

 

How is virtual memory work?

 

When a process is created, the operating system (32-bit systems) will be allocated a 4GB size of virtual memory for the process. The reason is 4GB, because the 32-bit operating system, a pointer length of 4 bytes (32-bit, 32-th power of 2 addresses addressability from 0x00000000 ~ 0xFFFFFFFF) is the size of the capacity of 4GB.

A process virtual address is used by the memory area management table, and does not take actual 4G. The use of the memory area is mapped to physical memory by page table. Therefore, each process can use the same virtual memory address without conflict, because their physical addresses are actually different. For example, virtual address 0x12345678 0x12345678 physical address mapping of virtual addresses and the process B Process A is different of.

64-bit system we use now, the largest virtual memory be? 64 is 2 to the power it? That would have 16EB virtual address space, 1EB be? = 1,000 1EB  PB = 1,000,000 TB = 1,000,000,000 GB There is no doubt that this is a very huge numbers, our system does not require such a huge virtual memory. Modern operating systems, such as, Windows on AMD64 implement only the application of maximum 256TB of virtual memory.

cup To access the virtual memory address, the address need to go through the physical address to be translated into accessible. For example the view (from the "in-depth understanding of computer systems") and, cpu to access a virtual address 4100 is required (memory management unit) via dedicated hardware memory management unit memory physical address MMU be translated to the corresponding 4, then cpu address in memory 4 to fetch the data on the return position.

 

Virtual memory is divided into blocks of a fixed size, the virtual page becomes (Virtual Page) referred to VP, the corresponding physical memory is also divided into blocks of a same size, it becomes a physical page (Physical Page) abbreviation PP. It is a page between disk and memory units for data exchange.

cpu how to know if a particular virtual page cached data? Cached in memory or on disk it?

This requires a form to record, this is the page table.

The first column of the page table is valid if 0 indicates that the virtual page is not cached, 1 has been cached data.

The second column represents the blue data cached in memory, the white represents the data cached in the disk.

 

Page Hit

 

When the cpu (the position indicated by the arrow) of the data to be accessed PTE2, found valid flag is 1, it indicates that the virtual page has been cached, since the page table address points VP2 data block in memory, so the direct cpu VP2 read data from memory, this is called a page hit.

 

Missing pages

 

当cpu要访问PTE3(箭头所指的位置)的数据时,发现valid标志位为0,所以表示该虚拟页未被缓存,这时会触发一个缺页异常,cup根据页表第二列的指针找到磁盘中对应的数据块VP3,然后根据规则选择一个内存中的牺牲页,把VP3覆盖到内存中。

 

上图中的内存中的vp4被替换成了VP3,valid的值从0变成1。

 

缺页异常返回后,会重启缺页指令,并更新虚拟地址,cpu再次访问PTE3的时候就能命中页,取到数据了。

 

总结

 

当每个进程创建的时候,内核会为每个进程分配虚拟内存,这个时候数据和代码还在磁盘上,当运行到对应的程序时,进程去寻找页表,如果发现页表中地址没有存放在物理内存上,而是在磁盘上,于是发生缺页异常,于是将磁盘上的数据拷贝到物理内存中并更新页表,下次再访问该虚拟地址时就能命中了。

 

现在你是不是对虚拟内存的工作原理更加了解了呢?

 

 

 

 

 

                                                                            欢迎关注我的公众号

发布了13 篇原创文章 · 获赞 169 · 访问量 6651

Guess you like

Origin blog.csdn.net/zhanyd/article/details/102987669