Sample Code to Detect Performance Services That Leak Memory (296794)



The information in this article applies to:

  • Microsoft Win32 Application Programming Interface (API), when used with:
    • the operating system: Microsoft Windows XP
    • the operating system: Microsoft Windows 2000
    • the operating system: Microsoft Windows NT 4.0

This article was previously published under Q296794

SUMMARY

Your application can use either the Performance Data Helper (PDH) functions or the RegQueryValueEx function with the HKEY_PERFORMANCE_DATA registry key to collect performance data information about any performance object.

If you notice that your application experiences a memory growth (as measured by the Private Bytes counter), this may indicate a memory leak. If your application is only collecting performance data, one or more performance services on the system may be responsible for the memory growth over a period of time. This article describes how to determine which performance service (or services) on the system is leaking memory.

MORE INFORMATION

When you access performance data information, the Performance Library (PerfLib), which is part of the Advapi32.dll file, calls the Collect functions of all performance services on the system. These functions are executed by a thread in your process, and any performance service memory leaks will show up in the context of your process.

It can be difficult to debug this kind of memory leak because the leak does not originate in your program; it originates in a performance DLL file. This article describes MemLeakTest.exe, which is a tool that you can use to determine which performance services on the system may be leaking memory.

The following file is available for download from the Microsoft Download Center:
Release Date: June 20, 2002

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file. You can also download the C++ source code and build the tool yourself.

The following file is available for download from the Microsoft Download Center:

Release Date: June 20, 2002

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

Performing the Memory Leak Test

After you download the tool, follow these steps to perform the memory leak test:
  1. Determine which performance services are installed on your system. To obtain a list, call MemLeakTest.exe with no parameters.

    This tool also shows the DLL file size and time stamp.
  2. Call MemLeakTest.exe ALL Global.

    • The first parameter (MemLeakTest.exe) refers to the set of performance services.
    • ALL indicates to test all of the performance services.
    • The second parameter (Global) is the list of objects to test.
    The tool loads each of the Performance Extension DLLs in a separate MemLeakTest.exe process and then monitors for the Private Bytes memory growth by calling the collect procedure directly every 100 milliseconds. The Private Bytes information is logged to a file for each performance service in a .txt file every 1 minute. Every performance service will have a corresponding .txt file (for example, Tcpip.txt for TCP/IP Performance Service) that is created in the current working directory.

    As long as the MemLeakTest.exe tool is running, the Private Bytes information is logged to the respective .txt files continuously in a loop until you stop the test by pressing any key at the command prompt. After you run the tool for some time, you can analyze the .txt files for any memory growth.
  3. Examine each of the log files to see whether any of the DLLs are leaking memory.
  4. If none of the log files indicates a memory leak, try calling MemLeakTest.exe ALL ALL.

    This adds objects individually to the query instead of using the Global object. Rarely does this test uncover leaks that the ALL Global test misses, and it makes sense to run it only if your software queries objects individually.

Options When Running MemLeakTest.exe

You can use the following options when you run MemLeakTest.exe:
  • Run MemLeakTest.exe /?. This displays the various command line options.
  • Run MemLeakTest.exe without any command-line arguments. This displays the performance services that are installed on the system, including file size and date/time on the Performance Extension DLL.
  • Run MemLeakTest.exe ALL Global.
  • Run MemLeakTest.exe ALL ALL. This is the same as MemLeakTest.exe ALL Global, except that the Collect procedure of each performance service is called with all valid object indexes on the system.
  • Run MemLeakTest.exe Tcpip Global. This is the same as either MemLeakTest.exe ALL Global or MemLeakTest.exe ALL ALL, except that the Collect procedure is called only for the specified performance service name with the specified value name.

    The ValueName parameter can be any of the following:
    • Global
    • Object indexes that are enclosed in quotation marks, for example, "2 4".
    • ALL, to indicate all object indexes.
After you run the MemLeakTest.exe tool for a while to collect the .txt files, and then you stop the tests by pressing any key, you can analyze the .txt files to determine which performance services show a steady Private Bytes memory growth, as reported in the .txt file. If one or more performance services show memory growth in the corresponding .txt file, contact the vendor responsible for the Performance Extension DLL, and then report the problem to determine the memory growth at the code level.

This article contains the following downloadable files:
File nameSize
MemLeakTest.exe56 KB
MemLeakTestCode.exe27 KB

Modification Type:MinorLast Reviewed:8/9/2004
Keywords:kbdownload kbdownload kbAPI kbfile kbinfo kbKernBase kbPerfMon KB296794