I have the folowing Delphi piece of code, running inside some Windows Service:
if FindFirst(path,faArchive,searchrecord) then
try
DoSomething(searchrecord);
while FindNext(searchrecord) do
DoSomething(searchrecord);
finally
FindClose(searchRecord);
end;
This is actually running every n' seconds inside a thread (searching directory content, then sending mails for every file found within .. then new content will be drop in that folder by another process .. then again .. ).
Everything is fine (the mails were sent, the files moved to another folder, etc), but our customer complain about huge memory consumption ... increasing rapidly.
So we check out, and first whe confirmed that memory leak, then identify out of doubt that block of code .. FindeFirst -> FindNext -> FindClose .. , is the 'offender'
Then searching and searching (first place, this .. then the web), we find the 'mysterious'
SetProcessWorkingSetSize(GetCurrentProcess(), DWORD(-1), DWORD(-1))
Watch here, here .. and many other stackoverflow entries, discussing about the benefits or inconveniences of using this Windows function.
The final fact is that memory usarge seems to be increasing and increasing when that block of code is executed (FindFirst .. FindClose) .. watching this consumption in the Windows Task Manager
So .. dear fellows ..
- Why this happens ? (it's some normal behaviour, some bug ..)
- What is the 'solution' ? (is something to be 'solved' ? .. is
SetProcessWorkingSetSize(GetCurrentProcess(), DWORD(-1), DWORD(-1))
appropriate for this case ? .. then how to use it ?