Reputation: 203
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 ?
Upvotes: 1
Views: 2636
Reputation: 222302
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:
Upvotes: 3
Reputation: 7479
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). 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
). Or it may have to read the memory from disk (aka a 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. 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. 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. You will often observe that Windows likes to keep a fair amount of memory in the Standby list.
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.
Upvotes: 3