Hello all , so a little background , im making a Voxel terrain engine for my game , I use a LOD system and I have to regenerate the terrain when the player moves , Now I pool everything , The gameobjects ,the meshes , the 3d array used for the voxel values , trees etc .I create my mesh using unitys new meshdata and meshData array in another thread like so on my own thread
public void CreateMyMesh()
{
int size = verts.Count;
int T = tris.Count;
var m = meshData[0];
m.SetVertexBufferParams(size, new VertexAttributeDescriptor(VertexAttribute.Position, stream: 0), new VertexAttributeDescriptor(VertexAttribute.Normal, stream: 1), new VertexAttributeDescriptor(VertexAttribute.Color, VertexAttributeFormat.Float32, 4, 2), new VertexAttributeDescriptor(VertexAttribute.TexCoord0, VertexAttributeFormat.Float32, 2, 3));
m.SetIndexBufferParams(T, IndexFormat.UInt32);
NativeArray<Vector3> NVerts;
NVerts = meshData[0].GetVertexData<Vector3>(0);
NativeArray<Vector3> NNormals;
NNormals = meshData[0].GetVertexData<Vector3>(1);
NativeArray<Color> NColors;
NColors = meshData[0].GetVertexData<Color>(2);
NativeArray<Vector2> NUVs;
NUVs = meshData[0].GetVertexData<Vector2>(3);
NativeArray<int> NTris;
NTris = m.GetIndexData<int>();
int vCount = NVerts.Length;
for (int i = 0; i < vCount; i++)
{
NVerts[i] = verts[i];
NNormals[i] = normals[i];
NColors[i] = colorsM[i];
NUVs[i] = Uvs[i];
}
for (int i = 0; i < T; i++)
{
NTris[i] = tris[i];
}
var sm = new SubMeshDescriptor(0, T, MeshTopology.Triangles);
sm.firstVertex = 0;
sm.vertexCount = size;
m.subMeshCount = 1;
m.SetSubMesh(0, sm);
NVerts.Dispose();
NColors.Dispose();
NUVs.Dispose();
NNormals.Dispose();
NTris.Dispose();
verts = null;
tris = null;
normals = null;
colorsM = null;
Uvs = null;
}
Then in the main thread I Do this
//this object is pooled so I dont actually create a new mesh here its just for the explanation
mesh = new Mesh();
Mesh.ApplyAndDisposeWritableMeshData(meshData, m_voxelGo.mesh, MeshUpdateFlags.Default);
If i dont call this last part memory usage never increases beyond initial usage.
So everything runs fine if I dont create a unity mesh object , i can create and destroy the chunks and the lists of triangles vertices, normals, colors and uvs , but as soon as i assign those to a mesh or create the mesh unity memory usage climbs up and never drops , if I stand still the memory usage stays stable , but moving cause regeneration and the memory usage climbs up again until eventually my computer freezes , I can call GC.Collect and that works but I have to call it After each mesh creation otherwise the game will eventually freeze , this would be great except it freezes unity everytime i call gc.Collect.
Now my question is why is there so much garbage being created when creating a mesh ? and why is unity not doing garbage collection automatically , I looked on the profiler and GC.collect is never called , garbage is created but never collected , I did also check to see if maybe the meshes were being leaked and the number of meshes never increases beyond initial creation . Thanks for reading my long winded explanation
