Under the Hood of .NET Memory Management - Simple Talk
Under the Hood of .NET Memory Management - Simple Talk
Under the Hood of .NET Memory Management - Simple Talk
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Chapter 7: The Windows <strong>Memory</strong> Model<br />
It is important to remember that <strong>the</strong> allocated memory is virtual, which means it doesn't<br />
directly refer to physical memory. Instead, it refers to a virtual memory address that is<br />
translated by <strong>the</strong> CPU to a physical memory address on demand.<br />
The reason for using virtual addresses is to allow for a much more flexible memory space<br />
to be constructed; one which is far larger than what is actually available in physical RAM.<br />
To make this possible, a machine's hard drive is used as a kind <strong>of</strong> secondary memory store<br />
to increase <strong>the</strong> total amount <strong>of</strong> memory available to our applications, and we will now<br />
explore that virtual/physical relationship.<br />
Virtual and Physical <strong>Memory</strong><br />
Physical memory is simply <strong>the</strong> amount <strong>of</strong> physical RAM installed on <strong>the</strong> machine.<br />
By definition, it has a fixed size that can only be increased by opening <strong>the</strong> back <strong>of</strong> <strong>the</strong><br />
machine and slotting more inside.<br />
Any resource with a fixed limit causes a problem; once that limit is reached – bang – <strong>the</strong>re<br />
is nowhere to go. In <strong>the</strong> absence <strong>of</strong> any o<strong>the</strong>r kind <strong>of</strong> memory, once <strong>the</strong> physical memory<br />
is all used up, things start to go horribly wrong.<br />
Virtual memory was devised as a way around <strong>the</strong> problem: using a machine's hard drive<br />
as a kind <strong>of</strong> alternative memory store <strong>of</strong> last resort. Physical disk capacity is much greater<br />
than RAM capacity, and so it makes sense to use part <strong>of</strong> your disk storage as a way <strong>of</strong><br />
increasing <strong>the</strong> amount <strong>of</strong> available memory. To facilitate this, a large file is created, <strong>of</strong>ten<br />
called a "swap" or "page" file, which is used to store physical memory sections that haven't<br />
been accessed for a while. This allows <strong>the</strong> memory manager (MM) to reuse physical<br />
memory and still have access to "paged" data when requested. I probably don't need to<br />
mention <strong>the</strong> downside <strong>of</strong> this cunning solution, but I will, anyway: accessing bits from<br />
RAM is pretty fast; getting <strong>the</strong>m <strong>of</strong>f a disk is really slow.<br />
198