Data Access Worldwide Knowledge Base
Article ID 2141 Article Title INFO: Memory usage of VDF programs according to Windows Article URL http://www.dataaccess.com/kbasepublic/KBPrint.asp?ArticleID=2141 KBase Category VDF (GENERAL) Date Created 07/13/2005 Last Edit Date 07/14/2005
Article Text
QUESTION:
A developer reports that when they start their VDF application the Windows taskmanager (in the mem usage) column shows an amount of memory. In their case something like 20 Mb. When they minimize (iconize) the application the amount of memory shown will be dramatically lower. Maximizing will increase again but not the previous size -- way less even.
Isn't this memory display in the taskmanager totally weird? Why does it use so much less memory after minimizing and maximizing? Would this be applicable for webapplications as well? Does it reduce the footprint?
It is not VDF specific; Microsoft Outlook behaves identically. Initially it takes 17.5Mb, mimimized 2.5Mb and maximized again 6Mb. There is a peak of 19Mb.
EXPLANATION:
Task Manager has the ability to display two different memory usage counters. The column names can be a little cryptic: Mem Usage actually displays the process current Working Set (I'll explain in a moment), and VM Size actually displays the amount of memory allocated by the process (this is the real number you're after). By default, the Task Manager displays Mem Usage but not VM Size. To display VM Size, select View -> Select Columns, and check Virtual Memory Size.
Virtual Memory Size is the number that you'd be looking for if you want to know how much memory your process has allocated, as this is the total amount of memory allocated. This is called Virtual Memory, as each process has its own virtual memory space of up to 4GB (although not all can be used at application level as some space is reserved for kernel level). Naturally not all processes can have 4GB of actual RAM available, hence it's called Virtual Memory. A process' Virtual Memory is then mapped by the operating system to physical memory. This mapping is performed in chunks, called pages, which is usually 4K each. IOW, the memory address used by your application of 1000, for example, may at any point map to any physical memory address, such as 64000. This mapping is performed with help from the hardware -- the CPU has built in support for virtual memory to physical memory address mappings. It's this mapping that lets processes run in isolated memory spaces, so as to not interfere with other processes. This whole scheme was introduced in Intel CPUs with Intel 286/386 processors, and it's called Protected Mode.
Furthermore, this virtual memory to physical memory address mapping also lets the operating system pretend that there is more memory available than physical memory in the machine. When a process needs more physical memory pages, the operating system can choose to disconnect some other virtual to physical memory page mapping and swap the contents of that physical memory page to disk. Thereby freeing up a physical memory page to be used by another process that needs it right now. This whole process is called Paging.
The amount of physical memory pages currently available and mapped to a particular process virtual memory pages is called the process Working Set. IOW, the process working set is the amount of physical memory currently directly available and mapped to the process. This is, of course, often very different from the amount of actual memory that has been allocated by the process. Remember, each process never deals with physical memory at all. When you call HeapAlloc() to allocate memory, you're allocating virtual memory. The operating system then maps the process' virtual memory addresses to physical memory addresses.
As you can imagine, the act of mapping a virtual memory page to a physical memory page, and paging memory, however quick it may seem, takes time and CPU resources. The system performs much better overall if it can minimize the amount of paging. This is why the operating system automatically adjusts the process Working Set to what it thinks is the most optimal at the moment. This is of course a very complex algorithm that takes lots of different information into account. If there's a lot of unused pages of physical RAM, it may choose to give a process much more physical pages than it currently needs, growing the process Working Set. But if there's very little physical memory pages available it may choose to lower the process Working Set. Virtual Memory pages that haven't been used by process in a while are often selected first when it needs to lower the process Working Set. It's also very common for applications to allocate a lot of memory when the process is just starting up, and to speed that process up, the operating system initially gives the process a predefined Working Set size, thereby reducing the number of times the Working Set needs to be increased and increasing performance. A process also has a minimum Working Set and a maximum Working Set that are automatically calculated by the operating system, although they can be overridden. These numbers, and the amount of physical memory initially assigned to a process Working Set typically varies with the amount of physical memory installed in the machine for example. And as you have also noticed, when you minimize an application, the process Working Set is automatically reduced. This is because if you minimize an application, you're very likely to not use it for a while, and so the operating system automatically frees up som physical memory pages to be available for other processes. Again, this is to attempt to improve performance when other processes needs to increase their Working Set.
So, to sum up, the process Working Set (Mem Usage in Task Manager) is the amount of physical memory pages currently available to the process. This is completely controlled by the operating system, and has very little to do with the amount of virtual memory allocated by the process.
The Virtual Memory Size (VM Size in Task Manager) is the total amount of virtual memory allocated by the process. This is completely controlled by the program, it increases as you allocate memory (using HeapAlloc(), or malloc() for example), and it decreases as you deallocate memory (using HeapFree(), or free() for example). This is the number you refer to when you want to know how much memory your program has allocated.
In case you've always wondered what the Invalid Page Fault error message really means when an application crashes, here goes. As you may now have guessed, "page" in that sentence is of course referring to this paging mechanism explained above. When a process is reading/writing to a virtual memory page that currently has no physical memory page mapping, the CPU automatically triggers a Page Fault exception. The operating system catches this Page Fault exception, and tries to remap a physical memory page to that virtual memory page. This often involves reading the specified memory page back from disk and into physical memory. If the operating system discovers that the specified page is not present in the paging file, IOW, it's a reference to an address that does not belong to any virtual memory page currently marked in use, it does not belong a virtual memory block returned by HeapAlloc() or that's part of the stack, it's a garbage pointer. Then the operating system terminates the process with an "Invalid Page Fault" exception/error. IOW, it's a page fault requested for an invalid page, thereby it's an invalid page fault.
Contributed By:
Sonny Falk
Company: Data Access Worldwide
Web Site: http://www.dataaccess.com
Email this Article
Email this Article to a Colleague
Send Feedback on this Article to Data Access Worldwide
Copyright ©2010 Data Access Corporation. All rights reserved.
The information provided in the Data Access Technical Knowledge Base is provided "as is" without warranty of any kind. Data Access Corporation disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. In no event shall Data Access Corporation or its suppliers be liable for any damages whatsoever including direct, indirect, incidental, consequential, loss of business profits or special damages, even if Data Access Corporation or its suppliers have been advised of the possibility of such damages. Some states do not allow the exclusion or limitation of liability for consequential or incidental damages so the foregoing limitation may not apply.