Click here to Skip to main content
12,452,000 members (26,220 online)
Click here to Skip to main content
Add your own
alternative version

Stats

87.6K views
1K downloads
28 bookmarked
Posted

Performance meter and memory leaks detector

, 5 Dec 2001
Rate this:
Please Sign up or sign in to vote.
This article describes how to monitor program heap for memory leaks

Introduction

While working in QA team I got a task "Measure product-components performance, i.e. CPU/Memory usage during their work and detect possible memory leaks". Detecting of memory leaks isn't a simple procedure. At first because there is no any definite technique for making it. The first idea was viewing the process memory space at the page level. But this method is rough enough ( I guess), and  Windows doesn't always free allocated blocks immediately. So I decided to calculate PROCESS_HEAP_ENTRY_BUSY nodes in program heap before using all components (loading dll's) and after it.

Details

About CPU usage - the first idea wasn't so bright again Smile | :) I've tried to measure CPU usage by Windows NT Pdh-functions, but then there is the necessity of loading pdh.lib. So I used NtQuerySystemInformation technique (unfortunately I don't know the author 's name, because I get sources through third person).

Well now, to detect memory leaks in the components of your application and to get system resources info during work, you can define a monitoring thread function like this:

#define PERFORMANCE_FILENAME "DocProcPerf.log"

typedef struct EVENTS
{
    HANDLE StartEvent;
    HANDLE StopEvent;
};

DWORD WINAPI UserThreadProc(LPVOID lpParameter)
{
    EVENTS* hWait = (EVENTS *)lpParameter;
    DWORD  dwStartTime = GetTickCount();

    CCompInfo* hInfo = new CCompInfo(PERFORMANCE_FILENAME);

    hInfo->HeapMakeSnapShot();
    hInfo->HeapStoreDumpToFile();

    SetEvent((HANDLE)hWait->StartEvent);

    hInfo->m_log->print( "DocProcTest started at %s\n", 
        hInfo->GetTimeString() );

    while (1)
    {
        if(WaitForSingleObject(
            (HANDLE)hWait->StopEvent,0) == WAIT_OBJECT_0)
            break;
        hInfo->m_log->print(
            "%s CPU[%d%%] Memory[%dKb]\n", 
            hInfo->GetTimeString(), 
            hInfo->GetCPUInfo(),
            hInfo->HeapCommitedBytes()/1024);
        Sleep(1000);
    };

    hInfo->m_log->print( "%s CPU[%d%%] Memory[%dKb]\n",
        hInfo->GetTimeString(), 
        hInfo->GetCPUInfo(),
        hInfo->HeapCommitedBytes()/1024);

    hInfo->m_log->print( "DocProcTest finished at %s\n",
        hInfo->GetTimeString() );
    hInfo->m_log->print( "Elapsed time %d sec\n",
        (GetTickCount() - dwStartTime)/1000 );
    hInfo->m_log->print( 
        "Total memory difference: %dKb\n\n",
        hInfo->HeapCompareSnapShots()/1024 );

    CloseHandle((HANDLE)hWait->StopEvent);
    CloseHandle((HANDLE)hWait->StartEvent);

    if (NULL != hWait)
        delete hWait;

    hInfo->HeapCompareDumpWithFile(FALSE); // basic report
    hInfo->HeapCompareDumpWithFile(TRUE);  // extended report

    if (NULL != hInfo)
        delete hInfo;

    return 0;
}

Thus we just have to add our keep-an-eye-thread initialization lines in the main application function like this:

printf(  "\nTest started.\n");
EVENTS *hEvent = new EVENTS;
hEvent->StartEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
hEvent->StopEvent = CreateEvent(NULL,TRUE,FALSE,NULL);

HANDLE hTread = CreateThread( NULL,
                             NULL,
                             UserThreadProc,
                             hEvent,
                             NULL,
                             NULL); 
WaitForSingleObject((HANDLE)hEvent->StartEvent,15000);

...
//program body goes here
...

if (NULL != hEvent)
SetEvent((HANDLE)hEvent->StopEvent);
while (WaitForSingleObject(hTread,1000) != WAIT_OBJECT_0)
Sleep(1000);
printf(  "\nTest finished.\n");

In conclusion I want to add that this method is under research now so feel free to ask me for new stuff or modifications of it. I'll be grateful for your comments and suggestions.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Andrei Iarantsev
Web Developer
Belarus Belarus
QA engineer

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Generalyes but Pin
OscarTV4-May-04 4:23
sussOscarTV4-May-04 4:23 
GeneralCCompInfo Pin
Anonymous10-Feb-04 2:28
sussAnonymous10-Feb-04 2:28 
Generalmemory leaks Pin
Srinivas Goud8-Jun-03 22:53
memberSrinivas Goud8-Jun-03 22:53 
GeneralProcess perfomance metet Pin
Dvir30-Dec-02 10:41
sussDvir30-Dec-02 10:41 
GeneralRe: Process perfomance metet Pin
shivpal24-Jul-03 19:31
membershivpal24-Jul-03 19:31 
GeneralThanks for the sample Pin
moodboom15-Jun-02 17:17
membermoodboom15-Jun-02 17:17 
GeneralQuestions Pin
BMW_Power8-Dec-01 8:09
memberBMW_Power8-Dec-01 8:09 
GeneralRe: Questions Pin
Andrei Iarantsev (Minsk, Belarus)9-Dec-01 21:46
memberAndrei Iarantsev (Minsk, Belarus)9-Dec-01 21:46 
QuestionIs this right? Pin
Darren Schroeder6-Dec-01 7:48
memberDarren Schroeder6-Dec-01 7:48 
AnswerRe: Is this right? Pin
Andrei Iarantsev (Minsk, Belarus)10-Dec-01 21:07
memberAndrei Iarantsev (Minsk, Belarus)10-Dec-01 21:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 6 Dec 2001
Article Copyright 2001 by Andrei Iarantsev
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid