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):
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)
TDevice
is being created/managed, either. Please provide a minimal reproducible examplecallMicroservice
is executed a new set of tasks is being created and this a new set of threads as well.Devices
instance?