hshatti / TONNXRuntime

TOnnxRuntime is a Microsoft ONNXRuntime AI and Machine Learning Library for Freepascal / Delphi
MIT License
48 stars 10 forks source link

memory leak in example fastercnn10 on lazarus windows 10 64bit? #5

Closed armgong closed 11 months ago

armgong commented 11 months ago

Compile fastercnn10 using Lazarus (master branch) on windows 10 64, just start it then close. Lazarus reported "2 unfreed memory blocks : 8"

InitTaskDialogIndirect: TaskDialogIndirect(nil,nil,nil,nil)=$80070057 (=E_INVALIDARG)                 
InitTaskDialogIndirect: TaskDialogIndirectAvailable=TRUE                                              
clear                                                                                                 
Heap dump by heaptrc unit of D:\src\TONNXRuntime\examples\Lazarus\FasterRCNN10\lib\FasterRCNN10.exe   
3297 memory blocks allocated : 1673401/1676528                                                        
3295 memory blocks freed     : 1673393/1676512                                                        
2 unfreed memory blocks : 8                                                                           
True heap size : 1540096 (192 used in System startup)                                                 
True free heap : 1539456                                                                              
Should be : 1539504                                                                                   
Call trace for block $0000000001618DF0 size 4                                                         
  $0000000100011CDB                                                                                   
  $00000001001A4297  NEWREF,  line 1662 of ../../../source/onnxruntime.pas                            
  $00000001001A483B  initialize,  line 1774 of ../../../source/onnxruntime.pas                        
  $0000000100010588                                                                                   
  $00000001001AA7CC  ONNXRUNTIME_$$_init$,  line 3463 of ../../../source/onnxruntime.pas              
  $00000001000110BC                                                                                   
  $0000000100002D8E  main,  line 18 of FasterRCNN10.lpr                                               
  $0000000100002DF6                                                                                   
  $0000000100017530                                                                                   
  $0000000100002D70                                                                                   
  $00007FFA45B57344                                                                                   
  $00007FFA45EE26B1                                                                                   
Call trace for block $0000000001618D10 size 4                                                         
  $0000000100011CDB                                                                                   
  $00000001001A49F7  NEWREF,  line 1662 of ../../../source/onnxruntime.pas                            
  $00000001001A4ED3  initialize,  line 1769 of ../../../source/onnxruntime.pas                        
  $0000000100010588                                                                                   
  $00000001001AA7B9  ONNXRUNTIME_$$_init$,  line 3463 of ../../../source/onnxruntime.pas              
  $00000001000110BC                                                                                   
  $0000000100002D8E  main,  line 18 of FasterRCNN10.lpr                                               
  $0000000100002DF6                                                                                   
  $0000000100017530                                                                                   
  $0000000100002D70                                                                                   
  $00007FFA45B57344                                                                                   
  $00007FFA45EE26B1                                                                                   
hshatti commented 11 months ago

Hi thanks for the observation, the short solution since it's a small 16 byte onetime leak (in x64) and will never leak further memory as you use it, you can disable this behaviour by going to : [Project] - > [Project Options] -> select [Debugging] -> uncheck [Use Heaptrc unit...]

The long explanation is that I kept the heap tracker ON in the example for debugging, however, the memory leak in this case is due to the "ORTEnv" automatic allocation which is happening one time only during the programme launch, taking little two blocks and because of a conflict between onnxruntime memory management and the internal pascal housekeeping mechanism, having said that, it will never go beyond that 16 bytes even while extensive model load/usage or frequent inferences or trainings, this should be insignificant and has no side-effects on the memory knowing that it will be freed once the program exit anyway, until I find a better workaround, it's not an issue for now.

If you have a special case that unusually affects performance or memory using this library compared to other implementations (for example Python implementation takes ~150% more memory for a lesser performance in CPU), please let me know about it and I'll try to help otherwise it should work as expected.

Finally, please star this project, it will encourage uploading further and more interesting native pascal AI and Data Science related projects. H