Hello everybody !
I would like to have a few advices on how to fix a problem I already have searched a lot about. Any help would be very appreciated
I’m trying to implement the marching cubes algorithm in C# in Unity, running on grid of size 128**3. The calculation must be done at runtime and has to be instantaneous. I’m developing a toolbox to visualize scientific/molecular data, so I have to generate a lot of surfaces on user demand. To obtain a fast calculation I implemented a recursive version of the algorithm. That’s ok the calculation is always done < 30-40 ms even on ~old computer.
BUT I have 2 problems :
- Insane memory leaks, due to the memory I have to use during each run of marching cubes and it is obviously not released/freed
- Stack overflow exceptions, maybe due to stack size restrictions or wrong auto adaptation, but I HAVE to do this using recursion and have a large stack size
1) Memory leaks
I have a class MarchingCubesRec.cs that executes the work and call another script to generate the final GameObject(s) using the generated mesh. I’m calling it like this :
MarchingCubesRec MCInstance; // global MCInstance = new MarchingCubesRec(); // at start MarchingCubesRec.MCRecMain(my_arguments); // in function, on user demand
GenerateMesh GMInstance; // global GMInstance = new GenerateMesh(); // at MCRecMain call // Do the mesh calculation, using only global variables initialized at function call GenerateMesh.GM(my_other_arguments); // when mesh calculation finished, GM does GameObject(s) construction //variables are used the same way than the 2 scripts before
That’s the way I build each GameObject composing my surface. I’m new to C# and I don’t really know if it’s the good way to call functions. I also tried static types for both variables and/or functions and other organizations of my code, but each time I generate a surface and create a GameObject my memory is increasing until I get an Out of memory Exception. I have to precise that only one surface is displayed at a time and it never takes more than 5-10 GameObjects. Previously generated GameObjects are destroyed using Destroy() at the same time than the new ones are created.
Does anybody knows WHY my memory is never freed ?
In fact I seems that 5-10% of allocated space is freed, from times to times. But I always end up with a few GB of memory used within a short time of utilization. I read a bit about the garbage collector handling of memory and I understood that recursion can sometimes prevent a normal (=“efficient” ?) garbage collection.
Then, does anybody knows HOW am I censed to handle this ?
2) Stack overflow exceptions
To perform my mesh calculation I use a recursive version of the marching cubes algorithm. My grid size is 128**3 (~2 millions points), so I understood that it’s normal to have a large stack size for this kind of thing. I’m sure my recursion is not infinite. The problem is I tried to use the Thread class to force a stack size that fits my needs, but I couldn’t generate the mesh while the new thread is not the main thread. Next step I want to try is recuperate the arguments from the calculation thread and then generate mesh on main thread.
Do you think that could solve my stack overflow exception problem ?
Thanks a lot for your help, you’re awesome, and I’ve been struggling on this for really too long