Mesh memory leak

the problem I am having is that I have a class that creates a mesh that then I assign to a MeshFilter which is added to a new gameobject. All of this gameobject are added to a dictionary that I after traverse for candidates to be deleted.
Once I found a candidate, I use

DestroyImmediate( pairChunk.Value.m_gameObject.GetComponent<MeshFilter>().mesh );
DestroyImmediate( pairrChunk.Value.m_gameObject );

The problem is that they are not being destroyed, the number of Resources.FindObjectsOfTypeAll().Length just keeps getting bigger, as the mesh memory used.

I have read every other thread I could find on the subject, but I haven’t found a solution that could help me.
I will make a pool of meshes instead of creating new ones each step, because I know the ceiling of how many I will use, but I want this to be resolved so I have a grasp of whats going on, and what I am doing wrong.

By the way, I create the mesh components (vertices, indexes, normals, uvs) inside a thread, and after I compose the mesh in the main thread, using a modified not-singleton loom class.

loom.RunAsync( () => {
	loom.QueueOnMainThread( () => {
		filter.mesh = chunk.CreateMesh();
		filter.renderer.material = m_terrainMaterial;
	} );
} );

If you have any clue I could follow, I would be greatly appreciated. Thanks!

Don’t use Destroy( GetComponent<MeshFilter>().mesh );. The .mesh property will create an internal copy of the mesh for this MeshFilter instance at the first time you access the property. If you already created the mesh by hand you should exclusively use .sharedMesh. The ,sharedMesh property hold the reference to the original mesh instance.

Just as follow up, the same thing happens with material / sharedMaterial and materials / sharedMaterials. The material and mesh property has been created to allow easy manipulation of a Material / Mesh for this MeshFilter / Renderer only.

Even more generally every class that is derived from UnityEngine.Object is a “tracked object”. Instances of that type always have a native C++ counterpart. Once created they must be destroyed manually with “Destroy”. The only exception are Components. Since components can’t live without the parent GameObject, they are automatically destroyed along with the parent gameobject. Any other class derived from Unity’s Object class need to be destroyed manually. That includes: Mesh, Material, Shader, any ScriptableObject, GameObject, Texture2D, AnimationClip, AssetBundle, AudioClip and some others.

Watch out to only destroy instances that are created at runtime. If you call Destroy on an asset reference when testing inside the editor, the asset can be deleted permanently. Calling Destroy on an asset in a build doesn’t do permanent damage, but you can’t use that asset until the next application restart.

Have you perhaps tried destroying MeshFilter.sharedMesh? MeshFilter.mesh always returns a duplicate (or, after it creates the duplicate once, a reference to that duplicate), so you’re basically creating and immediately destroying that duplicate rather than destroying the original one.


Did you ever find a solution to your problem? I had the exact same problem, with the profiler showing meshes rocketing up to take up gigs of RAM and crashing the program within 20 minutes. The following has fixed my problem.
The big performance boost memory wise came from saving a reference to the collision mesh and initialising it only once during startup. After that to update the mesh I use colMesh.Clear(), set the verts and triangles to the calculated values, then use meshCollider.sharedMesh = colMesh.

Prior to this, I was using Mesh colMesh = new Mesh() every time I updated. I went from a gig of memory taken up by the mesh in 20 minutes when instantiating a new mesh every update, to 150megs taken up by meshes not matter how long the game runs by keeping a reference to the existing one.

//Attach script to a GameObject with a MeshFilter, MeshRenderer, and MeshCollider
[RequireComponent (typeof (MeshFilter))]
[RequireComponent (typeof (MeshRenderer))]
[RequireComponent (typeof (MeshCollider))]

//Variable declaration	    
private Mesh faceMesh;										
private MeshCollider meshCollider;
private MeshFilter meshFilter;
private Mesh colMesh;

private Vector3[] faceVertices;
private int[] faceTris;
private Color32 [] faceColors;
private Vector3[] colVertices;
private int[] colTris;

//Called externally when the chunk is instantiated
public void Initialise(){
		meshFilter = GetComponent<MeshFilter> ();
		faceMesh = GetComponent<MeshFilter> ().mesh;
		meshCollider = GetComponent<MeshCollider> ();
		colMesh = new Mesh ();

		faceVertices = new Vector3[maxNumFaceVerts];
		faceTris = new int[maxNumFaceTris * 3];
		faceColors = new Color32[maxNumFaceVerts];
		colVertices = new Vector3[maxNumColVerts];
		colTris = new int[maxNumColTris * 3];

public void UpdateMesh(){
		faceMesh.Clear ();
		faceMesh.vertices = faceVertices;
		faceMesh.triangles = faceTris;
		faceMesh.colors32 = faceColors;
		faceMesh.Optimize ();
		faceMesh.RecalculateNormals ();

		colMesh.Clear ();
		colMesh.vertices = colVertices;
		colMesh.triangles = colTris;
		meshCollider.sharedMesh = colMesh;