- Home /
Procedural Mesh, ToArray, and MemoryLeaks
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();
Answer by RuinsOfFeyrin · Nov 16, 2015 at 08:41 AM
So through lots of googling way back on like page 7, i came across a link to unitys website to an official memory leak bug with building a procedural mesh. This is supposed to be fixed in version beta 5.3.0 Im going to currently assume that this is related, and that my assumption that the use of ToArray was not the issue.
The bug is supposed to be replicable with the code from unitys website on procedural mesh generation. just increase the number of triangles to 1000, and rebuild the mesh a bunch. I however have no way to test this as i do not have access to 5.3.0 builds. Anyone with access care to confirm?
Your answer
Follow this Question
Related Questions
Mesh memory leak 3 Answers
creating a mesh procedurally 4 Answers
Easy way to convert a bunch of vertices to triangles or uv's? 1 Answer
minimize vertex artifacting for procedural mesh manipulations - vertex collision same object? 0 Answers
Procedurally generated mesh JS - explain the error please 0 Answers