Memory Usage in a C++ Program
March 1th,2011
Background Information

Process:

A process is an instance of a computer program that is being executed. It contains the program code and its current activity. In another word, a process is an operating system level concept used to describe a set of resources (such as external code, libraries and primary thread) and the necessary memory allocations used by a running application. For each *.exe loaded into memory, the OS creates a separate and isolated process for use during its life time [
1, 2].

Virtual Memory:
Virtual memory is a common part of most operating systems on desktop computers. It has become so common because it provides a big benefit for users at a very low cost. Most computers today have something like 32 or 64 megabytes of RAM available for the CPU to use. Unfortunately, that amount of RAM is not enough to run all of the programs that most users expect to run at once.For example, if you load the operating system, an e-mail program, a Web browser and word processor into RAM simultaneously, 32 megabytes is not enough to hold it all. If there were no such thing as virtual memory, then once you filled up the available RAM your computer would have to say, "Sorry, you can not load any more applications. Please close another application to load a new one." With virtual memory, what the computer can do is look at RAM for areas that have not been used recently and copy them onto the hard disk. This frees up space in RAM to load the new application .The area of the hard disk that stores the RAM image is called a page file. It holds pages of RAM on the hard disk, and the operating system moves data back and forth between the page file and RAM. On a Windows machine, page files have a .SWP extension [
3].

Working Set:
Working set is the set of memory pages that have been recently touched by a program. At this point these 10 pages are added to the working set of the process. If the process then goes and copies this data into another 10 MB cache previously allocated, everything else remains the same but the Working Set goes up again by 10 Mb if those old pages where not in the working set. But if those pages where already in the working set, then everything is good and the programs working set remains the same.

Memory Usage:
Mem Usage is the working set size, not its total virtual memory usage. It is the amount of physical memory which is directly (currently) allocated to the process. It can be accessed without causing a page fault. This includes pages shared with other processes [
4].

How To Get and Display Memory Usage in Your C++ Program?
Now let's get our hands dirty with a simple c++ program since we know the theory behind Process, Memory Usage, and Working set. I assume that you are already familiar with c++ language and also know how to make a simple win32 windows application. What we are going to do is to make a function that returns to us the memory usage in KB. Once that's done, we pass the current process ID by calling GetCurrentProcessId() to this function. That's all.
appMemUsage() Function
showTextTitle() Function
ScreenShot
Download Source Code
MemUsage.rar
References:
[1] - Process (computing). http://en.wikipedia.org/wiki/Process_(computing)
[2] - Andrew Troelsen.(2010). Pro C# 2010 and the .net 4 Platform, Fifth Edition.
[3] - Jeff Tyson. How Virtual Memory Works. http://www.howstuffworks.com/virtual-memory.htm
[4] - Jerold Schulman. http://www.windowsitpro.com/article/tipjsi-tip-0722-what-does-mem-usage-and-vm-size-mean-on-task-manager-s-processes-tab-.aspx


 

 

All contents copyright © 2008-2012 Hamed Sabri ALL RIGHTS RESERVED