Detecting Resource Leaks

Environment: Windows NT, Windows 2000, Windows XP

I was working on a project and it was the moment to deliver the product to the client. I had used my own developed memory leak tool to test the application for memory leaks. My utility passed the test and I was comfortable that there were no memory leaks. But this project was very important and I wanted to be one hundred percent sure that the application did not leak any memory. And, what about handle leaks? My utility does not detect any handle leaks and I was not sure whether my application was leaking handles.

My problem was simple. I did not need a tool to troubleshoot to locate the memory leaks in my code but just wanted to verify that my application was not leaking memory and handles. I started the task manager and observed the memory columns to check the memory usage statistics and ran my application. The memory usage of the application increased and stabilized after some time. I was a bit comfortable but not 100% comfortable as I had no information about the handle leaks in my application.

I searched the net for a suitable free tool to suit my needs. There were many memory leak detection tools, but none for handle leaks. My search ended at the MSDN site, where I found the answer to all of my queries. My application was to run on windows 2000 and windows XP. I came to know that the most reliable method on this case is the perfmon tool bundled with Windows NT onwards. I just needed to check the status of private bytes and handle count counters for my process using perfmon.

Well, I ran the tool and was satisfied to see that the private bytes count (process memory usage) did not increase and stabilized soon. However, I was horrified to find that the handle count was increasing continuously. Clearly, my application was leaking handles. After a few hours of struggling, I fixed the handle leaks which were mainly due to Registry open calls. After fixing the handle leaks, the perfmon window was as in the following figure.



Click here for a larger image.

Here is the detailed process to watch the memory and handle counters for your process. This is applicable for Windows 2000 but should be same for Windows NT and Windows XP, as well.

  • Open perfmon by clicking Start, Settings, Control Panel, Administrative Tools, Performance.
  • Click the Add counters tool box(the tool with the + sign). It will display the Add Counter dialog box.
  • Check the 'Use local computer counters' radio button.
  • Select Process under the 'Performance Object:' drop-down combo box.
  • Select the 'Select counters from list' radio button and select 'Handle Count' and 'Private Bytes' from the combo box. This box is a multiline select combo box.
  • Select the 'Select instances from list' radio button and select your application from the combo box just below it.
  • Press the Add button, followed by the Close button.

This will open the perfmon window. You will see two graphs each for memory (private bytes) and handle count. Run your application and you will see the present status of memory usage and open handles for you process. If any of the counter values increase continuously without being stabilized, there are memory/handle leaks in the application.

There are other counters that are worth watching, especially if your application has a performance hit and needs troubleshooting.



Comments

  • Usefull

    Posted by Rakesh on 04/03/2012 02:19am

    Good article

    Reply
  • Helped me to find a dangerous memory leak

    Posted by Legacy on 08/03/2003 12:00am

    Originally posted by: Michael Lev

    Thank you Shiv, your article helped me to find a memory leak, which caused a process at the customers site to accummulate a good 2 million handles !! Fortunately it did not crash before I found the bug.
    BTW - I do not agree with Vlad ("Even easier"), because Task manager shows you some data, but perfmon gives you much more possibilities and is user friendlier.
    Meiki

    Reply
  • Detect leaks programmatically

    Posted by Legacy on 07/23/2003 12:00am

    Originally posted by: Andrew

    I have used TaskInfo to monitor handle counts. For some reason (can't remember) I found it better than PerfMon.

    Whichever you use the real problem is to find exactly where in the code the leak occurs. In the software I was recently debugging there was an enormous amount of code that could have been causing the leak. On a 2MHz processor a lot can happen in a second or 2.

    What we really need is a way to get the handle counts in the program. Then you can do something like:

    void ReallyLengthyProcess()
    {
    #ifdef DEBUG
    int handle_count = get_handle_count();
    #endif
    // lots of code that uses handles but should free them
    // ...
    ASSERT(handle_count == get_handle_count());
    }

    Does anyone know how to get handle counts from software? PerfMon and TaskInfo do it so it must be possible, even if OS specific.

    Reply
  • Useful Article, Thanks a lot.

    Posted by Legacy on 05/08/2003 12:00am

    Originally posted by: Ranganath


    Useful Article, Thanks a lot.

    Reply
  • Even easier

    Posted by Legacy on 03/19/2003 12:00am

    Originally posted by: Vlad Vissoultchev

    i personally use task manager for leakage troubleshooting.

    start taskmgr (ctrl+shift+esc) and select processes tab. then choose view->"select columns..." menu and in the "select columns" dialog choose "handle count", "thread count" and "gdi objects". also take a look at the other counters available.

    the best thing about task manager is that it usually stays always-on-top so you can monitor gdi/handle resources as you brute force your dialogs :-))

    Enjoy, and happy hacking!

    HTH,
    </wqw>

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

  • The latest release of SugarCRM's flagship product gives users new tools to build extraordinary customer relationships. Read an in-depth analysis of SugarCRM's enhanced ability to help companies execute their customer-facing initiatives from Ovum, a leading technology research firm.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds