Link to home
Create AccountLog in
Avatar of softbreeze
softbreezeFlag for United States of America

asked on

Will the working set of an app shrink automatically if the memory causing it to grow is freed?

I have an app that the working set keep growing during the life of the app. Initially I thought this might be a memory leak but none are detected using EurekaLog. The app allocates a chuck of memory for a data structure and places it on a list. The number of allocations vary from 1 to 1024. I allocate the objects, place them on the list, use them, and then free all of them. Thirty seconds later I do it again. This is the only memory allocations done in the app by me. Should I expect to see the working set size shrink after I free the memory? I'm using the following code snippet to get the working set size:

using psAPI....
 
function GetMemoryUsed: DWord;
var
  pmc:         PPROCESS_MEMORY_COUNTERS;
  cb:          Integer;
begin
     cb := SizeOf(_PROCESS_MEMORY_COUNTERS);
     GetMem(pmc, cb);
     pmc^.cb := cb;
     Result := 0;
     if GetProcessMemoryInfo(GetCurrentProcess(), pmc, cb) then
       begin
       Result := pmc^.WorkingSetSize;
       end;
     FreeMem(pmc);
end;

Open in new window

Avatar of MerijnB
MerijnB
Flag of Netherlands image

it says in the tags you are using Delphi 2007, is this true?
Avatar of softbreeze

ASKER

Yes: CodeGear" RAD Studio 2007  Version 11.0.2902.10471 with the December update and the April Hotfix
ok, in that case you (probably) won't have a problem with memory fragmentation.

How did you check for leaks with Eurekalog? If something leakes while the application is running, but this leaked memory _is_ cleaned up when you close your application, it's hard to detect with tools like Eurekalog.
It is my understanded that when you select the option to check for memory leaks it keeps track of all memory allocations and deallocations. When the program exits it reports the allocations that were not freed. I've only just started using it and it seems to be very reliable.
My original question though was 'Should I expect to see the working set size shrink after I free the memory?' I've done OS development on several well known operating systems and some shrink the working set back to the highest memory allocation for the address space others don't. I'm just not sure with the Windows.  
I've narrowed it down to a few calls to the Service Control Manager (SCM) to return all the services on various servers on my domain. I still searching the code...
I'm actually not sure, you can't depend on it. There is also the Delphi MM in between which might keep memory pre-allocated.
If you are not sure, how can you state that 'you can't depend on it'? It works one way or the other and it is not a crap shoot.

I believe based on a little testing that windows does reduce the working set as memory is released. I was looking for confirmation. I also believe that I have a design flaw that prior to releasing the memory I must allocate some that causes the working set to grow. The fact that the other memory was freed would not alter the 'new' larger working set size. I've been looking for days for such a possiblity in my code. So far I haven't found it.

Thanks anyway
ASKER CERTIFIED SOLUTION
Avatar of MerijnB
MerijnB
Flag of Netherlands image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thanks for your time