简体   繁体   English

物理内存与虚拟内存

[英]Physical Memory Vs Virtual Memory

I know that when a new process is created there will be a virtual address space that is associated with that process .That virtual address space is located in the virtual memory pages and mapped into memory pages inside the physical memory in a non-contiguous way. 我知道创建新进程时将有一个与该进程关联的虚拟地址空间。该虚拟地址空间位于虚拟内存页面中,并以非连续方式映射到物理内存中的内存页面中。

My question is : 我的问题是:

When a thread will store a value or allocate space for a variable , it will be stored/allocated in the virtual memory , but are those changes written in the same time to physical memory ? 当线程将值存储或为变量分配空间时,它将存储/分配在虚拟内存中,但是那些更改是否同时写入到物理内存中?

In short, yes. 简而言之,是的。

In order for you to be able to read or write to memory, the virtual address must be mapped to physical memory. 为了使您能够读取或写入内存, 必须将虚拟地址映射到物理内存。 So writes to your variable will always be backed by physical memory (there may be a delay due to caching, but that has nothing to do with physical vs virtual). 因此,对变量的写入将始终由物理内存支持(由于缓存而可能会有延迟,但这与物理vs虚拟无关)。 If the memory isn't currently mapped when you do the write, then a page fault occurs which will allow the OS to step in and map the physical memory. 如果在执行写入操作时当前未映射内存,则将发生页面错误,这将允许操作系统介入并映射物理内存。

If it needed to map the memory in, it can come from the "standby" list, which means that the memory was already in physical memory and the OS just needed to hook it up (a soft fault ). 如果需要映射内存,它可以来自“备用”列表,这意味着该内存已经在物理内存中,而OS仅需要将其挂接( soft fault )。 Or it may have to read the memory from disk (aka a hard fault ); 或者它可能必须从磁盘读取内存(又称hard fault ); which can come from the pagefile, a memory mapped file, or contents of a binary file. 可能来自页面文件,内存映射文件或二进制文件的内容。


Edit - Clarification on Memory Lists and Page Faults: 编辑-澄清内存列表和页面错误:

Zero List These are pages of physical memory that are free and have been zeroed out. 零列表这些是可用的物理内存页面,这些页面已被清零。 When an application allocates more memory, the OS first pulls from this pool of memory (if available). 当应用程序分配更多的内存时,操作系统首先从该内存池中提取(如果有)。 Mapping these into a processes' address space is a soft fault. 将它们映射到进程的地址空间是一个软故障。

Free List These are pages of physical memory which the OS is in the process of scrubbing and is about to stick onto the Zero list. 空闲列表这些是物理内存页面,操作系统正在清理这些页面,这些页面将保留在“零”列表上。

Standby List Windows will periodically unmap memory from your virtual memory on the off chance that someone else may need more memory. 备用列表 Windows将定期从您的虚拟内存中取消映射内存,以防其他人可能需要更多内存。 Conversely, if there is bunch of memory in the Zero List, it will find pages of memory which your application is likely to need again and pre-load it into memory. 相反,如果“零列表”中有大量内存,它将找到您的应用程序可能再次需要的内存页面,并将其预加载到内存中。 All of these pages are stored in the Standby list. 所有这些页面都存储在“ Standby列表中。 They are not assigned to any one application and are liable to be scrubbed and re-assigned if there is an application which has a sudden need for more memory. 它们没有分配给任何一个应用程序,如果存在突然需要更多内存的应用程序,则有可能对其进行清理和重新分配。

Run perfmon.exe /res and click on the "Memory" tab to see how much is associated with the various Lists. 运行perfmon.exe /res ,然后单击“内存”选项卡,以查看与各个列表关联的数量。 You will often observe that Windows likes to keep a fair amount of memory in the Standby list. 您将经常观察到Windows喜欢在“待机”列表中保留大量内存。

Soft Fault If your application allocates memory or reads or writes to memory which the OS stole or pre-loaded (and placed on the standby list), then it is a very simple thing for the OS to assign back to your process. 软故障如果您的应用程序分配了内存,或者对其进行了操作系统窃取或预加载(并置于备用列表中)的内存进行读写,那么对于操作系统而言,将其分配回您的进程是非常简单的事情。 Soft faults are cheap. 软故障很便宜。

Hard Fault If the memory you need is not anywhere in physical memory, then a hard fault is encountered, and the OS needs to "page" it in from some type of storage device. 硬故障如果所需的内存不在物理内存中的任何地方,则会遇到硬故障,并且操作系统需要从某种类型的存储设备中对其进行“分页”。 This is typically slow and are what developers may be concerned about when performance tuning. 这通常很慢,并且是性能调整时开发人员可能会关注的问题。

To answer your comment 回答您的评论
Allocating memory typically results in soft faults as the OS pulls from the Zero List and then steals from the Standby list to fulfill the request. 当操作系统从零列表中拉出,然后从待机列表中窃取以满足请求时,分配内存通常会导致软故障。 Nothing needed to be read from any physical media so, no hard faults were encountered. 无需从任何物理介质读取任何内容,因此不会遇到硬故障。

Once you've allocated the memory, the OS may later push that memory out to the Standby List. 分配内存后,操作系统可能会在以后将该内存推出备用列表。 If you reference it again, then there is a soft fault to put it back into your address space. 如果再次引用它,则存在软错误,无法将其放回地址空间。 If someone else needed the memory, then the OS can flush that memory to the page file (writing the data out is not a "fault"). 如果其他人需要内存,则操作系统可以将该内存刷新到页面文件(将数据写出不是“故障”)。 Once you then reference that memory address again, a hard fault occurs and the page is read and mapped back into your address space. 一旦您再次引用该内存地址,就会发生硬故障,并且将读取页面并将其映射回您的地址空间。

There is no virtual memory separate from physical memory. 没有虚拟内存与物理内存分开。 All reads and writes to memory must go to physical memory. 所有对内存的读取和写入都必须进入物理内存。

Here is how virtual memory typically works in hardware: 这是虚拟内存通常在硬件中的工作方式:

  • When a load or store instruction is executed, the processor first calculates the address in the virtual memory address space. 当执行加载或存储指令时,处理器首先计算虚拟内存地址空间中的地址。 (This often involves adding together the contents of processor registers and/or values encoded into the instruction itself.) (这通常涉及将处理器寄存器的内容和/或编码为指令本身的值加在一起。)
  • Then the processor looks up the virtual address in tables it has. 然后,处理器在其具有的表中查找虚拟地址。 These are tables that describe the translation from virtual memory to physical memory. 这些表描述了从虚拟内存到物理内存的转换。
  • If the processor finds a match for the virtual address, it uses data in the matching table entry to change the virtual address into a physical address. 如果处理器找到虚拟地址的匹配项,则它将使用匹配表条目中的数据将虚拟地址更改为物理地址。 (Usually, the high bits of a virtual address are a key to the table and the low bits are an offset within a page. To form the physical address, new high bits are taken from the table entry, and the same low bits are used.) (通常,虚拟地址的高位是表的关键字,低位是页面内的偏移量。要形成物理地址,请从表条目中获取新的高位,并使用相同的低位。)
  • Then the processor performs the load or store operation using the calculated physical address. 然后,处理器使用计算出的物理地址执行加载或存储操作。

If a virtual address is not found in the tables the processor has in its special registers, then one of several things may happen: 如果在处理器的特殊寄存器中的表中找不到虚拟地址,则可能发生以下几种情况之一:

  • Some processor models search special tables in memory (which are created and maintained by the operating system) for additional information about maps from virtual memory to physical memory. 某些处理器模型在内存中的特殊表(由操作系统创建和维护)中搜索有关从虚拟内存到物理内存的映射的​​其他信息。
  • If the processor cannot do the above or does do a search but fails to find a match, it generates an exception. 如果处理器无法执行上述操作或执行搜索但未能找到匹配项,则会生成异常。 An exception stops the current program, makes some special changes inside the processor, and starts executing part of the operating system instead of the user program. 异常会停止当前程序,在处理器内部进行一些特殊更改,并开始执行部分操作系统而不是用户程序。
  • The operating system examines the cause of the exception and decides what to do. 操作系统检查异常原因并决定要做什么。

The operating system typically does one of several things: 操作系统通常执行以下操作之一:

  • If the reason for the exception is just that the processor did not have the appropriate table entries for the particular address the process tried to access, but that address is part of the process' virtual address space as established by the operating system, then the operating system loads the appropriate data into the processor and resumes execution of the process. 如果发生异常的原因仅仅是处理器没有针对进程尝试访问的特定地址的适当表条目,但是该地址是操作系统所建立的进程虚拟地址空间的一部分,则操作系统系统将适当的数据加载到处理器中,然后继续执行过程。
  • If the user process tried to access memory that was in its virtual address space but currently did not have a physical address, then the operating system allocates new physical memory for the process, sets up the tables to describe the new map from that part of virtual address space to the allocated physical memory, and resumes execution of the user process. 如果用户进程尝试访问其虚拟地址空间中但当前没有物理地址的内存,则操作系统将为该进程分配新的物理内存,并设置表以描述虚拟机那部分中的新映射将地址空间分配给分配的物理内存,并继续执行用户进程。 (This allocation of new physical memory might involve deallocating other physical memory. Sometimes that can be done simply by unmapping pages that have not been used in a while and erasing their contents [for security]. Sometimes it requires writing the contents of pages to disk, to save them for later when they are needed again.) (这种分配新的物理内存可能涉及取消分配其他物理内存。有时可以通过取消映射一段时间内未使用的页面并擦除其内容来实现[出于安全性考虑。]有时需要将页面的内容写入磁盘,以保存它们以备以后再次使用时使用。)
  • If the user process tried to access memory that was not in its virtual address space (of course, the numbers are in the space, but there might have been no allocation of virtual memory to that portion of the address space), then the system sends the process a signal, causing it to execute a signal handler for the error. 如果用户进程尝试访问不在其虚拟地址空间中的内存(当然,数字在该空间中,但是可能没有为该地址空间的那部分分配虚拟内存),则系统将发送处理信号,使其执行错误的信号处理程序。 If the process has no signal handler for this problem, the operating system will terminate the process. 如果该进程没有针对此问题的信号处理程序,则操作系统将终止该进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM