Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


August 1998

Inside Memory Management, Part 1


RSS
Subscribe to Windows IT Pro | See More Internals and Architecture Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

File Mapping and Memory Sharing
A powerful feature of the NT Memory Manager is that it lets programs map files into their virtual address space. This mapping is accomplished in two steps. In the first step, the program creates an object, called a Section Object, to describe a file that the Memory Manager can map. The Section Object holds information about the name of a file, its size, and what portions of it are mapped. The Win32 function CreateFileMapping results in a Section Object's creation.

In the second step, the program maps all or part of the file into the process address space. The process invokes the Win32 function MapViewOfFile to do this mapping, specifying the start offset in the file to begin mapping and the number of bytes to map. The process specifies the protection (e.g., read-only, read/write) on the view at this time. After the view is mapped, the process can read to and write from the file simply by reading and writing data to the view's addresses in the process address map. The Memory Manager transparently works with the file systems to ensure that all processes accessing the file see any updates the original process makes and that the updates are eventually flushed to disk.

The file-mapping capability makes file I/O extremely straightforward, and NT uses it extensively. When an application launches, the Process Manager maps a view of the application's image to the address space of the application's process. The Process Manager transfers control to the entry point of the image in the address space, and as the application executes, any pages referenced for the first time generate page faults. The page faults result in the Memory Manager reading the applications pages from the file on disk.

NT uses a variation of file mapping to share memory. In this variation, MapViewOfFile takes a parameter that specifies memory mapping, rather than file mapping. The Memory Manager backs views of the section with the paging file when memory mapping is specified. The data in the view is no different from regular virtual memory that a process reserves and commits. However, NT can give sections of the data names in the Object Manager namespace so that two or more processes can open the same section, map views to their own address spaces, and then communicate with one another through this shared memory.

Copy-on-Write
The Memory Manager implements an important optimization of memory sharing called copy-on-write. There are several common scenarios in which a process might want to use the same data as another process but keep any modifications it makes private to itself. For example, if two or more processes start the same application and one process modifies the data in the image, other processes should not see that modification. The obvious way to accomplish this kind of private modification is to load multiple copies of the application into memory; however, this strategy wastes space. Instead, the Memory Manager marks the physical pages containing the image read-only and notes in an internal data structure (which I'll describe next month) that the page is a copy-on-write page. When a process tries to modify the copy-on-write page, that action will generate a page fault (because the page is read-only). The Memory Manager will see that the process referenced a copy-on-write page and will copy the contents of the copy-on-write page to another page that is private to the process that made the reference. Then, when the faulting instruction restarts, the process can modify its private copy of the page. Figure 5 demonstrates this procedure. In the figure, Process 1 and Process 2 share three copy-on-write pages. If Process 1 writes to one of the pages, it will get its own private copy of the page, and Process 2 will retain the original of the page.

NT uses the copy-on-write functionality mostly for executable images. When programs start, the Process Manager maps copy-on-write images. The POSIX subsystem also uses copy-on-write for handling the POSIX fork operation. When a POSIX process forks, the Process Manager creates a child process that will inherit the address space of the parent process. Instead of making a copy of all the parent's memory, the Memory Manager just marks the address-space pages of both the parent and child processes as copy-on-write. When either process makes a modification to its memory, it receives a private copy of the page it wants to modify, and the original of the page will then be private to the other process.

Stay Tuned
Next month, I'll dig deeper into the Memory Manager to describe the internal data structures it uses to keep track of pages. I'll present the details of how the Memory Manager implements shared memory, and I'll give you an overview of the way working sets--the amount of physical memory assigned to each process--are tuned as the system runs.

End of Article

   Previous  1  2  3  [4]  Next  


Reader Comments
Excellent! One of the best resources I have found on the net. I have to give my 5th semester OS exam and find this article on memory mgmt extremely helpful in what I have learnt from my OS course book. And its free! Overall, an excellent resource on OS articles.

Gaurav October 27, 2001


I think it was an excellent article on memory management as even the best of the books are not able to explain the concept very clearly.The best thing about the article is that, it has been explained in the most simplest of the ways as possible.

apaar November 04, 2003


Superb & cool narration!

Davis Joseph March 25, 2004


Excellent article.. Very well presented.. thanks a lot !!

Venkat May 25, 2004


Interesting article, but what I need to know is what to do when things go wrong. I have a system file backup program that runs automatically on my computer every day. this has worked for 5 years. Now suddenly I get a message: "The instruction at "0x...." referenced memory at "0x....". The memory could not be read. I assume there is a new offending program I'm running or a Windows update that is causing it. All I want is a little tool which will display a map of my memory showing which programs and services are using what memory.

Rob Campbell June 08, 2004


The best article I have ever seen on such a complex topic. Well done and keep it up.

Prabhat June 30, 2004


it stated that it had what I was looking for but it didnt.It had nothing that I was looking for

Anonymous User October 28, 2004 (Article Rating: )


Like Rob said, I too was looking for an answer to the error "The instruction at "0x...." referenced memory at "0x....". The memory could not be read.
wanted to know why it happens and what it might indicate (ie is my memory physically gone bad and may need replacement?)

Anonymous User November 05, 2004


one of the most useful information i got from net.thanks a lot

Anonymous User November 25, 2004


Like other people I need to find out why I am getting: "The instruction at "0x6aaa7324" referenced memory at "0x6aaa7324". The required data was not into memory because of an I/O error status of "0xc00000c4"." On a server that we upgraded to 4GB. Everything worked fine until the upgrade.

Anonymous User December 13, 2004 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Understanding File-Size Limits on NTFS and FAT

A general confusion about files sizes on FAT seems to stem from FAT32's file-size limit of 4GB and partition-size limit of 2TB. ...


Related Articles Inside Memory Management, Part 2

Related Events WinConnections and Microsoft® Exchange Connections

Windows Internals with Sysinternals Webinar

Check out our list of Free Email Newsletters!

Windows OSs eBooks Understanding and Leveraging Code Signing Technologies

A Guide to Windows Certification and Public Keys

SQL Server Administration for Oracle DBAs

Related Windows OSs Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement