[英]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: 这是虚拟内存通常在硬件中的工作方式:
If a virtual address is not found in the tables the processor has in its special registers, then one of several things may happen: 如果在处理器的特殊寄存器中的表中找不到虚拟地址,则可能发生以下几种情况之一:
The operating system typically does one of several things: 操作系统通常执行以下操作之一:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.