Hello,
So I am working with some procedural meshes and I have noticed what appears to be a memory leak that I can not for the life of me seem to figure out how to fix. Using the profiler i have tracked it down to the rendering of the procedural mesh (The Code is below). From what i can see using the deep profile option, the ToArray function are the culprit… it does not seem the memory is being reclaimed from the temporary arrays being made? I never noticed it before because its a small amount, but now the mesh is being rebuilt several times a second, and there a LOTS of procedural meshes so i noticed it now that its starting to add up. If this is the case how am i supposed to avoid this with a procedural mesh?
_solidMeshFilter.sharedMesh.Clear ();
Mesh mesh = _solidMeshCollider.sharedMesh;
_solidMeshCollider.sharedMesh = null;
mesh.Clear();
_solidMeshCollider.sharedMesh = mesh;
chunkSolidObject.transform.position = new Vector3(worldX, worldY, worldZ);
if (meshData.transparentTriangles.Count > 0)
{
_solidMeshFilter.sharedMesh.subMeshCount = 2;
_solidMeshRenderer.materials = doubleMaterial;
}else{
_solidMeshFilter.sharedMesh.subMeshCount = 1;
_solidMeshRenderer.materials = singleMaterial;
}
_solidMeshFilter.sharedMesh.vertices = meshData.meshVertices.ToArray();
_solidMeshFilter.sharedMesh.colors32 = meshData.meshColors.ToArray();
if (meshData.meshTriangles.Count > 0)
_solidMeshFilter.sharedMesh.SetTriangles(meshData.meshTriangles.ToArray(),0);
if (meshData.transparentTriangles.Count >0)
_solidMeshFilter.sharedMesh.SetTriangles(meshData.transparentTriangles.ToArray(),1);
_solidMeshFilter.sharedMesh.uv = meshData.meshUVs.ToArray ();
_solidMeshFilter.sharedMesh.normals = meshData.meshNormals.ToArray();
_solidMeshCollider.sharedMesh.vertices = meshData.colliderVertices.ToArray();
_solidMeshCollider.sharedMesh.triangles = meshData.colliderTriangles.ToArray();