Memory leak when repeatedly initialising and stopping community codes
Describe the bug
When repeatedly initialising and stopping a community code in AMUSE (tested on Huayno, Ph4 and Hermite), memory usage continues to grow over time. Resources don't seem to be fully released upon stop() or cleanup_code().
To Reproduce The following AMUSE script can be used to show the memory leak (requires a profiler).
from amuse.community.ph4.interface import Ph4 from amuse.community.huayno.interface import Huayno from amuse.lab import Particles from amuse.units import nbody_system, units from amuse.ic.plummer import new_plummer_model
from memory_profiler import profile
@profile def spawn_code(p): code = Ph4(converter) code.particles.add_particles(p) code.cleanup_code() code.stop()
converter = nbody_system.nbody_to_si(1.0 | units.MSun, 1.0 | units.km) p = new_plummer_model(10, convert_nbody=converter) for i in range(100): spawn_code(p) print(f"Iteration {i+1} completed.")
Expected behavior While spawning a code will consume memory, upon cleaning it up, it should be released.
Environment: Local workstation: OS and version: Ubuntu 20.04.5 LTS Compiler: gcc (conda-forge gcc 11.4.0-13) 11.4.0 mpiexec (Open MPI) 5.05
Cluster: OS: Red Hat Enterprise Linux release 8.10 (Ootpa) Compiler: gcc (conda-forge gcc 13.3.0-2) 13.3.0 mpiexec (Open MPI) 5.08