tags:

views:

220

answers:

2

Hi,

I'm using Win32 C++ in CodeGear Builder 2009 Target is Windows XP Embedded.

I found the PROCESS_MEMORY_COUNTERS_EX struct and I have created a siple function to return the Memory consumption of my process

SIZE_T TForm1::ProcessPrivatBytes( DWORD processID )
{
  SIZE_T lRetval = 0;
  HANDLE hProcess;
  PROCESS_MEMORY_COUNTERS_EX pmc;

  hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |
                              PROCESS_VM_READ,
                              FALSE, processID );
  if (NULL == hProcess)
  {
    lRetval = 1;
  }
  else
  {
    if ( GetProcessMemoryInfo( hProcess, (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc)) )
    {
      lRetval = pmc.WorkingSetSize;
      lRetval = pmc.PrivateUsage;
    }

    CloseHandle( hProcess );
  }
   return lRetval;
}
//---------------------------------------------------------------------------

Do i have to use lRetval = pmc.WorkingSetSize; or lRetval = pmc.PrivateUsage;

the privateUsage are what I see in perfmon. but what is that WorkingSetSize exactly.

I what to see every byte I allocate in the counter when I allocate it. Is this Posible?

regards

jvdn

+2  A: 

This is a much tougher question than you probably realized. The reason is that Windows shares most executable code between processes (especially the ones that make up most of Windows itself) between processes. For example, there's normally ONE copy of kernel32.dll loaded into memory, but it'll normally be mapped into every process. Do you consider that part of the memory your process is "using" or not?

Private memory is what's unique to that particular process. This can be somewhat misleading too. Since the executable for your process could potentially be shared with another process (i.e. two instances of your program could be run), that's not counted as part of the private memory, even if (as is often the case) there's only one instance of it running.

The working set size is about 99.999% meaningless. What it returns is whatever has been set as the preferred working set size for the process. You can adjust that with SetProcessWorkingSetSize(). Windows has a working set trimmer that attempts to trim down working sets. If memory serves, it uses the working set size to guess at whether it's worth trying to trim the working set of this process -- i.e. if its current working set is larger than the working set size was set to, it tries to trim it down. Otherwise, it (mostly) leaves it alone.

Chances are that nothing you do will show you ever byte you allocate as you allocate it though. Calling Windows to allocate memory is fairly slow, so what's normally done is that the run-time library allocates a fairly big chunk of memory from Windows. When you allocate memory, the run-time library gives you a piece of that big chunk. Only when that chunk is gone does it go back to Windows and ask for more.

Jerry Coffin
A: 

Hi

Jerry thanks for the answer. Memory Management is quit complicated. I will stik to the PrivateUsage.

  1. what is the size of the that windows is allocating for me? Can I look that up in my windows?

  2. Is there a way to modify that value?

Jvdn

jvdn