OpenModelica
OpenModelica copied to clipboard
co-simulation FMU export: memory leak in simulation
Description
Detected a memory leak in exported C-code / FMUs. Memory pool continues to grow until simulation crashes/is killed by the OS due to excessive memory usage.
Steps to Reproduce
If a modelica module contains a function with a protected real array (e.g. in Modelica.Media.Water.IF97_Utilities.BaseIF97.Basic.dptofT or Modelica.Media.Water.IF97_Utilities.BaseIF97.Basic.tsat), the generated C-code allocates space for the real_array, but does not free it before function return. Thereby the memory pool keeps growing. The garbage collector does not catch this.
Expected Behavior
If possible, code generator should issue a "free" on the allocated array before function return to prevent memory leak.
Screenshots
Memory profiling output of valgrind/massif
100.00% (3,221,512,364B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->99.99% (3,221,266,528B) 0x5950BEF: malloc_zero (in /path/to/FMU/binaries/linux64/PSS.so)
| ->99.99% (3,221,225,472B) 0x5950B9C: pool_malloc (in /path/to/FMU/binaries/linux64/PSS.so)
| | ->99.99% (3,221,225,472B) 0x5953554: alloc_real_array (in /path/to/FMU/binaries/linux64/PSS.so)
| | ->66.66% (2,147,483,648B) 0x58B72F8: omc_Modelica_Media_Water_IF97__Utilities_BaseIF97_Basic_dptofT (PSS_functions.c:9636)
| | | ->66.66% (2,147,483,648B) 0x589D8E2: omc_Modelica_Media_Water_IF97__Utilities_BaseIF97_Regions_boilingcurve__p (PSS_functions.c:20111)
| | | ->66.66% (2,147,483,648B) 0x58FBA93: omc_Modelica_Media_Water_IF97__Utilities_BaseIF97_Regions_hl__p (PSS_functions.c:20766)
| | | ->66.66% (2,147,483,648B) 0x58B3282: omc_Modelica_Media_Water_IF97__Utilities_BaseIF97_Regions_region__ph (PSS_functions.c:21580)
| | | ->66.66% (2,147,483,648B) 0x58B15DB: omc_Modelica_Media_Water_IF97__Utilities_waterBaseProp__ph (PSS_functions.c:1847)
| | | ->66.66% (2,147,483,648B) 0x590894F: PSS_eqFunction_216 (PSS_02nls.c:326)
| | | ->66.66% (2,147,483,648B) 0x5908D20: residualFunc219 (PSS_02nls.c:383)
| | | ->66.66% (2,147,483,648B) 0x5972792: wrapper_fvec (in /home/karl/OpenFOAM/karl-9/platforms/linux64GccDPInt32Debug/lib/FMUs/v01/binaries/linux64/PSS.so)
| | | ->66.66% (2,147,483,648B) 0x59766F2: getNumericalJacobianHomotopy (in /home/karl/OpenFOAM/karl-9/platforms/linux64GccDPInt32Debug/lib/FMUs/v01/binaries/linux64/PSS.so)
| | | | ->66.66% (2,147,483,648B) 0x59727E0: wrapper_fvec_der (in /home/karl/OpenFOAM/karl-9/platforms/linux64GccDPInt32Debug/lib/FMUs/v01/binaries/linux64/PSS.so)
| | | | ->66.66% (2,147,483,648B) 0x597212E: solveHomotopy (in /home/karl/OpenFOAM/karl-9/platforms/linux64GccDPInt32Debug/lib/FMUs/v01/binaries/linux64/PSS.so)
| | | | ->66.66% (2,147,483,648B) 0x595D624: solve_nonlinear_system (in /home/karl/OpenFOAM/karl-9/platforms/linux64GccDPInt32Debug/lib/FMUs/v01/binaries/linux64/PSS.so)
| | | | ->66.66% (2,147,483,648B) 0x588F6B3: PSS_eqFunction_219 (PSS.c:470)
| | | | ->66.66% (2,147,483,648B) 0x5897666: functionODE_system0 (PSS.c:3529)
| | | | ->66.66% (2,147,483,648B) 0x58974B9: PSS_functionODE (PSS.c:3677)
| | | | ->66.66% (2,147,483,648B) 0x594EEE6: internalGetEventIndicators (fmu2_model_interface.c.inc:1989)
| | | | ->66.66% (2,147,483,648B) 0x59500A4: fmi2DoStep (fmu2_model_interface.c.inc:2311)
| | | | ->66.66% (2,147,483,648B) 0x48D246E: fmi2_capi_do_step (fmi2_capi_cs.c:41)
| | | | ->66.66% (2,147,483,648B) 0x486E7DE: fmi2_import_do_step (fmi2_import_capi.c:374)
[...]
| | | | ->66.66% (2,147,483,648B) 0x10C100: main (memleak.cpp:72)
| | | |
| | | ->00.00% (0B) in 1+ places, all below ms_print's threshold (01.00%)
| | |
| | ->33.33% (1,073,741,824B) 0x58B6268: omc_Modelica_Media_Water_IF97__Utilities_BaseIF97_Basic_tsat (PSS_functions.c:11576)
| | | ->33.33% (1,073,741,824B) 0x58B341D: omc_Modelica_Media_Water_IF97__Utilities_BaseIF97_Regions_region__ph (PSS_functions.c:21625)
| | | ->33.33% (1,073,741,824B) 0x58B15DB: omc_Modelica_Media_Water_IF97__Utilities_waterBaseProp__ph (PSS_functions.c:1847)
| | | ->33.33% (1,073,741,824B) 0x590894F: PSS_eqFunction_216 (PSS_02nls.c:326)
| | | ->33.33% (1,073,741,824B) 0x5908D20: residualFunc219 (PSS_02nls.c:383)
| | | ->33.33% (1,073,741,824B) 0x5972792: wrapper_fvec (in /path/to/FMU/binaries/linux64/PSS.so)
| | | ->33.33% (1,073,741,824B) 0x5972113: solveHomotopy (in /path/to/FMU/binaries/linux64/PSS.so)
| | | | ->33.33% (1,073,741,824B) 0x595D624: solve_nonlinear_system (in /path/to/FMU/binaries/linux64/PSS.so)
| | | | ->33.33% (1,073,741,824B) 0x588F6B3: PSS_eqFunction_219 (PSS.c:470)
| | | | ->33.33% (1,073,741,824B) 0x5897666: functionODE_system0 (PSS.c:3529)
| | | | ->33.33% (1,073,741,824B) 0x58974B9: PSS_functionODE (PSS.c:3677)
| | | | ->33.33% (1,073,741,824B) 0x594EEE6: internalGetEventIndicators (fmu2_model_interface.c.inc:1989)
| | | | ->33.33% (1,073,741,824B) 0x59500A4: fmi2DoStep (fmu2_model_interface.c.inc:2311)
| | | | ->33.33% (1,073,741,824B) 0x48D246E: fmi2_capi_do_step (fmi2_capi_cs.c:41)
| | | | ->33.33% (1,073,741,824B) 0x486E7DE: fmi2_import_do_step (fmi2_import_capi.c:374)
[...]
| | | | ->33.33% (1,073,741,824B) 0x10C100: main (memleak.cpp:72)
| | | |
| | | ->00.00% (0B) in 1+ places, all below ms_print's threshold (01.00%)
| | |
| | ->00.00% (0B) in 1+ places, all below ms_print's threshold (01.00%)
| |
| ->00.00% (41,056B) in 1+ places, all below ms_print's threshold (01.00%)
|
->00.01% (245,836B) in 1+ places, all below ms_print's threshold (01.00%)
Additional Context
I would like to fix the issue, but I am not familiar with the OMC backend or how its code generation works. I need assistance in fixing this issue.