0

I'm developing a Windows service in Delphi that creates an instance of a TThread class. This class uses TEvent to repeatedly call a processing procedure at defined intervals. Inside the processing procedure, I use OmniThreadLibrary's Parallel.ForEach to create tasks. However, I observe that the memory used by the service increases over time. To simulate the issue, I used the following minimal code with Sleep:

unit uDevice;

interface

uses
 System.SysUtils, System.Classes, UniProvider, MySQLUniProvider, Data.DB,
 DBAccess,Uni,JsonDataObjects,DateUtils,
 Winapi.Windows,system.Generics.Collections,
 MVCFramework.Commons,MVCFramework.RESTClient,
 MVCFramework.SystemJSONUtils,MVCFramework.Serializer.JsonDataObjects,
 MVCFramework,
 System.Diagnostics,
 MVCFramework.Serializer.Commons,
 OtlCommon,OtlComm,
 OtlParallel, OtlTask,
 OtlTaskControl,
 OtlCollections,
 OtlThreadPool,
 System.SyncObjs,
 OtlSync;

type
 TDevice=class(TThread)
  protected
   procedure Execute; override;
   procedure DoTerminate; override;
  private
   FWaitEvent: TEvent;
   FInterval: Integer;
   procedure callMicroservice;
  public
   constructor Create(CreateSuspended: Boolean); reintroduce;
   destructor Destroy; override;
 end;

implementation
//------------------------------------------------------------------------------
destructor TDevice.Destroy;
begin
  FWaitEvent.Free;
  inherited;
end;
//------------------------------------------------------------------------------
constructor TDevice.Create(CreateSuspended: Boolean);
begin
 inherited Create(CreateSuspended);
 FInterval := 5000;
 FWaitEvent := TEvent.Create(nil, False, False, '');
end;
//------------------------------------------------------------------------------
procedure TDevice.Execute;
begin
  inherited;
  while not Terminated do begin
    callMicroservice;
    if wrSignaled = FWaitEvent.WaitFor(FInterval) then
     Break;
  end;
end;
//------------------------------------------------------------------------------
procedure TDevice.DoTerminate;
begin
 inherited;
end;
//------------------------------------------------------------------------------
procedure TDevice.callMicroservice;
begin
 begin
  Parallel.ForEach(0,5)
   .NumTasks(Environment.Process.Affinity.Count)
   .Execute(
    procedure(const task: IOmniTask; const value: integer)
    begin
     sleep(3000);
    end
  );
 end;
end;
//------------------------------------------------------------------------------
end.

I also attempted this approach, but the issue persists:

procedure TDevice.callMicroservice;
var
 loop: IOmniParallelLoop<Integer>;
begin
  loop := Parallel.ForEach(0,5);
  loop.NumTasks(Environment.Process.Affinity.Count)
   .Execute(
    procedure(const task: IOmniTask; const value: integer)
    begin
     sleep(3000);
    end
  );
  loop := nil;
end;

Currently I'm calling it from VCL application using button click event:

procedure TForm1.Button2Click(Sender: TObject);
var
 Devices:TDevice;
begin
 Devices:=TDevice.Create(false);
end;

As you can see in this image that memory is increasing by running (time):

enter image description here

With the current number of iterations in the parallel loop in this minimal code, the memory issue still occurs. In my production environment, the number of iterations can vary depending on API calls (e.g., the number of devices) and could reach 1K or more.

Delphi 10.4.2 + Omnithreadlibrary 3.07.7 + 32 bit service (app)

8
  • 3
    I'm not seeing any leak in this code. On the other hand, you didn't show how or where TDevice is being created/managed, either. Please provide a minimal reproducible example Commented Aug 2 at 20:23
  • What is the number of active threads of your application? Is it perhaps slowly growing? Based on your code it could cause memory consumption to grow in case if you are creating the taks faster than they are being processed since each time your callMicroservice is executed a new set of tasks is being created and this a new set of threads as well. Commented Aug 3 at 3:38
  • I updated my original question. Commented Aug 3 at 5:47
  • Where and how are you releasing Devices instance?
    – Dalija Prasnikar
    Commented Aug 3 at 13:24
  • I release it when I stop the service, my question is while it is running why memory is increasing? Commented Aug 3 at 15:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.