I found out that creating lots of temporary C++ objects through CxxWrap takes a lot of memory (duh!) and does not return that memory to the system after GC.
My use case is this package wrapping Voro++ library. Logically, iterating over a Voronoi tessellation there is meant to yield a Voronoi cell object on each iteration, and ideally I’d like the objects to be independent.
That rises an issue as each of the objects does not get immediately GC’ed if unused after the current iteration, so that memory allocation rises. Then at a later point GC deletes the objects. But the delete operator does not return the memory to the system but instead returns it to some memory pool for reuse. So, even though the objects get GC’ed at some point, the memory they allocated isn’t returned to the system.
That alone isn’t a huge problem, the problem arises when I run multiple loops creating such objects. Then, if the GC does not run after each loop, the allocated memory piles up and can consume all RAM.
So, I’m wondering how to make the object get collected as soon as possible after they are not needed?
The solution I have so far is to make two iterators, a “safe” one which allocates fresh objects, and “unsafe” which reuses a single objects for iteration. My feeling, though, is that neither is user-friendly. The first one because it can quickly lead to OOM, especially in an exploratory environment where a user tries the same thing with minor modifications over and over again. The second one needs a user to be concious that the results of iteration are not independent.
Am I missing a good third way to do the right thing without risking OOM?