1

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 ..

  1. Why this happens ? (it's some normal behaviour, some bug ..)
  2. 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 ?
Community
  • 1
  • 1
alasoft
  • 21
  • 4
  • 5
    What does "DoSomething" do? (It is also in the scope of that loop, and could be the leak source.) You've also provided the loop without any surrounding context, or indicated how you've identified FindFirst and friends as the source of the leak. How are you determining that they are "out of doubt" the culprits? – Ken White Apr 02 '14 at 17:51
  • You need to do more work to identify the leak. What diagnostic tools are you using? – David Heffernan Apr 02 '14 at 18:25
  • 1
    SetProcessWorkingSetSize(..., -1, -1) just pushes as many pages as possible out to swap file. All it will achieve is to make the perf of your app worse. You have not identified your leak, if there is a leak. Step 1 is to use fastmm full debug. – David Heffernan Apr 02 '14 at 18:42
  • 3
    Could you show real code? (FindFirst and FindNext are Integer, not Boolean functions) – MBo Apr 02 '14 at 18:44
  • Which Delphi version are you using? – mjn Apr 02 '14 at 19:38
  • 2
    Show the real code. All else is guesswork. – JensG Apr 02 '14 at 22:00
  • First .. my mistake, sorry, please accept my apologyze sincerely. – alasoft Apr 03 '14 at 17:19
  • Second .. I' have found some 'obvious' mistake - not destroying some TList and objects contained - .. precisely, deep inside the 'DoSomething(..)' method .. so, apologyze again – alasoft Apr 03 '14 at 17:21
  • Finally .. the memory (in Task Manager) seems to be increasing, slowly this time (once fixed my bug), but still increasing. Now I'm becoming worried .. because It's not obvious now, where and how this increase of memory is happening. So I will try this 'FastMM' .. Thank you everybody and sorry for the hassle. – alasoft Apr 03 '14 at 17:24

1 Answers1

3

The code in your question does not compile. However, it looks like you just transcribed it wrongly in to the question.

This file enumeration loop does not leak in itself. There is no problem with it. If the code in the question really is leaking then the only sane conclusion is that DoSomething is responsible for the leaks.

The normal way to debug this is to use the full version of FastMM. Ask it to report memory leaks on shutdown. Run the program, close it, and study the diagnostics. This should lead you to a number of leaks. Fix them until there are none left.

As for SetProcessWorkingSetSize(..., -1, -1), that pushes memory out of RAM onto disk. You can certainly do that but it is liable to lead to disk thrashing. You push the memory onto disk. Then you need it and have to read it into RAM again. Let the system manage your memory.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • David .. as I stated above, I apologize for being my mistake. I have found a very obvious 'memory bug' (simply no disposing some colletion) in that 'DoSomething' .. but then, memory still continues growing - at much slower rythm indeed - .. that really confuses me. So I'm checking using this 'FastMM' (first time in my life :-) .. Thank you and thank everybody for you patience !! – alasoft Apr 03 '14 at 20:07
  • Yes, basically yes. The problem now is why the memory is still growing .. and where are those leaks, and how to fix them. So I will try with FastMM. Thanks. – alasoft Apr 03 '14 at 20:12
  • Ah .. okey .. I didn't knew about that 'formality'. Thanks. – alasoft Apr 03 '14 at 20:15