cnaimi
cnaimi

Reputation: 11

App using too much memory with TPythonEngine in Delphi

I am using https://github.com/pyscripter/python4delphi/tree/master/PythonForDelphi I am experiencing a huge memory usage not being freed when I delete the TpythonEngine at runtime.

Here is the snippet of code

FPythonEngine := TPythonEngine.Create(nil);
FPythonEngine.Version := 'python35';
FPythonIO     := TPythonInputOutput.Create(nil);
FPythonEngine.IO         := FPythonIO;
FPythonEngine.RedirectIO := True;
FPythonIO.RawOutput     := False;
FPythonIO.UnicodeIO     := True;
FPythonIO.OnSendUniData := ForwardPythonOutput;
FPythonEngine.LoadDll;
FPytonScript := TStringList.Create();
FPytonScript.Add('print(''test-memory leak'')');
FPythonEngine.ExecStrings(FPytonScript);
FPytonScript.Clear;
FreeAndNil(FPytonScript);
FreeAndNil(FPythonIO);
FreeAndNil(FPythonEngine); 

code can be run from a button on a form. I also use a TMemo for the result with the function :

procedure TForm4.ForwardPythonOutput(Sender: TObject; const Data: string);
begin
   memo1.Lines.Add(data);
end;

When you run the code twice, memory is not released well on the FreeAndNil for TPythonEngine. If you loop many more it causes a crash of the app because not more memory is availabe. Are some parameters missing for really releasing the TPythonEngine?

I try UnloadDLL, Finalize, setting various properties like AutoLoad/AutoFinalize etc...

I can run several times the

FPythonEngine.ExecStrings(FPytonScript);

this is not creating abnormal memory usage but my goal was to create a Delphi object containing a Pythonengine. These object is created / destroyed several times so the memory usage is a nogo for my behavior.

Any suggestion or more recent library to use Python with Delphi 2010 / or help to solve the excessive memory usage.

Upvotes: 1

Views: 172

Answers (0)

Related Questions