2

I use OmniThreadLibrary. It has a parallel method ForEach.

I'm working on high loaded system and I need to handle some array in parallel. But I get a memory leak.

Here is a sample:


procedure TForm7.FormCreate(Sender: TObject);
var loop: IOmniParallelLoop<Integer>;
begin
  while True do
    begin
      loop := Parallel.ForEach(1, 100);
      loop.Execute(procedure (const i: Integer)
        begin

        end);

       loop := nil;
       Sleep(100);
    end;
end;

When I run this code, I can see a virtual memory leak.

What is wrong?

splash27
  • 2,057
  • 6
  • 26
  • 49
  • 1
    `loop := nil` looks odd. Why are you doing that? – David Heffernan Dec 16 '15 at 07:13
  • 2
    This code doesn't compile. Plus I'm also sure that you are not running an endless loop without processing any messages in your real program. Please show a more meaningful (and compilable) example. – gabr Dec 16 '15 at 07:21
  • @gabr, I've updated code sample. No need to publish real program, because even this sample cause memory leaks. – splash27 Dec 16 '15 at 08:56
  • 2
    Memory leaks if it is not released in a normal way when a program exits. In your case program never exits, so ... If you replace this code with a 'for' loop, you will probably have no memory leaks at all. Most probably the main reason for the problem is that your program does not process messages so some internal OTL processing doesn't occur and some memory is not released. If that is the case, this is by design. – gabr Dec 16 '15 at 09:02
  • @gabr, infinite loop - is just for example to show that memory leaks. In real program I always `Execute` the Parllel.ForEach and the program crashes with error "Out of Memory". As I suppose, the memory should be released, when the particular `Parllel.ForEach` has been completed. – splash27 Dec 16 '15 at 09:36
  • As I said - if you don't process messages, some memory is not cleared. This is by design. Show a real problem and we'll help you solve it. – gabr Dec 16 '15 at 09:38
  • @gabr, ok. What should I do in this particular example to prevent memory's leaks? – splash27 Dec 16 '15 at 10:06
  • Call Application.ProcessMessages. – gabr Dec 16 '15 at 11:18
  • In a real program you won't call ProcessMessages. – David Heffernan Dec 16 '15 at 12:53
  • What does the code do in a console app instead of a forms app? – Disillusioned Dec 16 '15 at 21:38
  • 1
    What people _would_ or _should_ do is often quite far removed from what they **do**. E.g. Not only does this code lock up the main thread, it does so in **FormCreate**! I suggest some modifications to at least make it look like a realistic sample: 1) Get rid of the `while True` loop and the `Sleep()`. 2) Move the code into a `TTimer` event firing every 100 ms. (The `loop := nil` would now be superfluous) 3) Then you could even add buttons to enable/disable the timer to your form. – Disillusioned Dec 16 '15 at 21:49

0 Answers0