X
29 Rate this article:
No rating

INTERNAL: Tracking Memory Leaks on Windows

Zachary Norman
Topic:
This help article discusses why the Windows Task Manager's "Mem Usage" should not be used for tracking memory leaks. Furthermore, it discusses the proper method for tracking this information.

Discussion:
When tracking potential memory leaks, the Windows Task Manager's "Mem Usage" display can be misleading as it reflects the "Working Set" instead of the "Private Bytes" usage of a process.

Definitions from MSDN:

Working Set:
Shows the current number of bytes in the working set of this process. The working set is the set if memory pages touched recently by the threads of the process. If free memory in the computer is above a certain threshold. pages are left in the working set of a process even if they are not in use. When free memory falls below a certain threshold, pages are trimmed from the working sets.

Private Bytes:
Shows the current number of bytes that a process has allocated that cannot be shared with other processes.

The problem with Working Set memory is that it counts memory that has been allocated and assigned to the process, even if some of that memory has been freed, but not yet taken away from the process. Windows does not lower that value until another application requests memory, which will cause Windows to take the freed memory away from the the original process to give it to another process. To see this in action, run an application and watch the Mem Usage value in the Task Manager go up. When the application comes to an idle state, minimize the application and watch the Mem Usage fall. This is because Windows will reap all memory that has been freed, but is still assigned to the application when, when the application is minimized. Now, restoring the application will result in Mem Usage increasing showing the memory rgar has been allocated but not freed.

To get a true, dynamic accessment of a process' memory usagem the Private Bytes must be monitored, This will show memory that is truly allocated but not freed. This can be monitered with the Perfmon utiliy as follows:

1. Start the application in question and get it to the state at which it should be monitored.

2. Start the Perfmon utility by selecting Start->Run and entering "perfmon".

3. In the menu bar immediately above the right display pane, click on the furthest left icon "New Counter Set" to clear out any old counters.

4. Right-click on the display pane, then click on "Add Counters".

5. In the "Performance Object" drop down list pick "Process".

6. In the right list-box pick the application process name to be monitored, e.g. idlde.

7. In the left list-box scroll to the bottom and select "Private Bytes" and then click "Add".

8. Click "Close" to dismiss the dialog. You should now see the amount of memory being used by the IDL session.

The range of the graph will need to be adjusted. To do this, right-click in the graph, select Properties which will open the System Monitor Properties dialog. Under the Graph tab, the vertical scale of the graph can be set.

9. Run the IDL application in question.