How to avoid Memory allocation on procedural Mesh generation

Hi everybody,

i toyed around with a little Hexagon-Voxel thing in Unity, and everything is working fine - except for some framerate hickups caused by CG.
I also found the problem - my terrain is divided in chunks (as usual when rendering Voxels) and with each chunk mesh-change my program allocates nearly half a MB of ram - which is of course not good. The profiler tells me that all of these memory allocation is caused by copying my vertex, triangle, uv etc. data from list to Array. (list.ToArray() call).
This is because i have some lists for vertices, triangles, uvs etc. which i fill in my CalcMesh routine based on my voxel data.

I can’t use static size arrays because i don’t know how many vertices/triangles etc. the mesh will have. Is there any strategy/trick/hack/whatever to avoid the list and work with something, which does not allocate this much memory?

I hope i could describe what i mean, and i hope for any ideas or solutions!


I actually posted something on this a few days ago in another question.

As far as i can tell this is a bug in unity. There is an entry in the issue tracker on the unity website for version 5.3 that mentions fixing of a memory leak in procedural mesh generation. The memory leak is evident in the example for procedural mesh generation in the unity documentation itself.

Hope that helps.