Article View: borland.public.cppbuilder.nativeapi
Article #11926How to 100% reliably determine memory usage of a proceess at a certain time?
From: EH
Date: Wed, 07 Jan 2004 11:41
Date: Wed, 07 Jan 2004 11:41
79 lines
3930 bytes
3930 bytes
Hello!
My program (process) produces memory leaks.
I see the growing memory consumption in the Task Manager.
I could find out some cases, where the leaks occure, but
since the algorithm is very complicated, i cannot further
encircle the leak producing code.
CodeGuard doesn't show me leaks.
So, the first step is to examine a certain piece of code
(let's say, a function), whether it produces a leak or not.
My idea is to have a function, that 100% reliable returns
me the actual memory consumption of my app.
I would call it before and after the code to examine:
Something like:
Mem1 = GetMemoryUsage();
// here the code to examine
MyFunction();
Mem2 = GetMemoryUsage();
Leak = Mem2 - Mem1; // in Bytes
I learned some things about Windows Virtual Memory Manager,
but it's not clear, what kind of memory is involved in a
process' overall memory consumption. I came to the conclusion,
that the Workingset and the pagefiled memory should be the
only kinds of memory, that my app uses. Is this right?
To get tese memory informations, I use the WinAPI function
GetProcessMemoryInfo(), that returns me the current size of
the process' memory in the structure PROCESS_MEMORY_COUNTERS:
int GetMemoryUsage( bool DoResetWorkingSetSize=true,
bool DoAddPageFileUsage=true )
{
static PROCESS_MEMORY_COUNTERS pmc;
static bool Ok;
static int Mem = 0;
if (DoResetWorkingSetSize) {
SetProcessWorkingSetSize( GetCurrentProcess(), 0xFFFFFFFF, 0xFFFFFFFF );
}
Ok = GetProcessMemoryInfo( GetCurrentProcess(), &pmc, sizeof( pmc ) );
if (Ok) {
Mem = pmc.WorkingSetSize;
if (DoAddPageFileUsage) {
Mem = Mem + pmc.PagefileUsage;
}
}
return( Mem );
}
With flag DoResetWorkingSetSize I can reset the Workingset size to the
minimum of the process' RAM usage before getting the memory informations.
BTW - I searched a long time for this - this is, what Windows does, when
minimizing an application.
With flag DoAddPageFileUsage I can determine, whether also the memory in
the pagefile should be counted.
Unfortuantely, when calling GetMemoryUsage() with DoAddPageFileUsage=true,
the returned memory information is not every time the same as it should be.
When calling GetMemoryUsage() with DoAddPageFileUsage=false, only the memory
in the physical memory (RAM) is returned, but as I saw, there may be
situations, when memory is at once placed to the page file, e.g.
the memory I request via the line
char *xxx = new char[1000000]
seems to be at once transfered to the pagefile. This also happens, when
calling GetMemoryUsage() with DoResetWorkingSetSize=false.
Conclusion: this idea to find out the real memory consumption is not 100%
reliable. But how can I get this info? What's THE method to 100% reliably
take a 'snapshot' of the process' memory consumption at a certain time?
Thanx in advance,
EH :-)
Technical Details
Message-ID:
<3ffbf061$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,5a77e45c447d389f,start X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2004-01-07 03:54:02 PST Date: Wed, 07 Jan 2004 12:41:20 +0100 From: EH <spam@127.0.0.1> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.5) Gecko/20031013 Thunderbird/0.3 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: How to 100% reliably determine memory usage of a proceess at a certain time? Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 80.131.139.40 Message-ID: <3ffbf061$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1073475681 80.131.139.40 (7 Jan 2004 03:41:21 -0700) Lines: 80 Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail