1

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.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
cnaimi
  • 11
  • 2
  • can you add the actual leak data? – whosrdaddy Jun 09 '17 at 11:50
  • Delphi memory leak reporter doesn't "see" everytime a leak and when it reports one its only an unexepected small block leak. 45-52 bytes: unknown x 6 but on real memory usage it's 800K /1.5 Mega for every run of the code – cnaimi Jun 09 '17 at 12:19
  • I reformulate my question as the problem appear when app is running not on closing the app. I use memory leak but my bad it's more execessive memory usage when app is running. FastMM didn't detect a huge leak when closing (maybe something is releasing elsewhere) but I didn't understand how to release properly the object. – cnaimi Jun 09 '17 at 13:40
  • I would think your program is suffering memory fragmentation because of repeated allocation and deallocation. Consider to allocate the Python engine and components only once. – Tom Brunberg Jun 09 '17 at 13:51
  • Read the following q/a:s :https://stackoverflow.com/q/2569661/2292722, https://stackoverflow.com/q/4463979/2292722, https://stackoverflow.com/q/20668182/2292722. They all deal with the same topic, and therefore your question is a duplicate – Tom Brunberg Jun 09 '17 at 13:57
  • 1
    On a side-note, you're creating / destroying this *without* using a `try .. finally` block. This *could* potentially lead to a memory leak, if something failed in this code (it would never be free'd). – Jerry Dodge Jun 09 '17 at 15:19

0 Answers0