Graphics.DrawMesh Leaking Mesh Memory

I’m using Graphics.DrawMesh to draw meshes for my terrain chunks (voxel engine). Any time I draw new meshes with DrawMesh, the memory taken up by meshes goes up and it never goes back down again.

Here’s the relevant code that I am using:

using UnityEngine;
using System.Collections.Generic;

public class ChunkRenderer 
{
	private static MeshData data = new MeshData();
	public static bool buildingMesh = false;

	public Chunk chunk;

	public bool updateMesh;
	private bool meshReady;

	public Mesh[] meshes = new Mesh[MeshData.MeshCount];

	private Vector3 worldPos;

	public ChunkRenderer(Chunk chunk)
	{
		this.chunk = chunk;
		worldPos = chunk.GetWorldPosition();

		for (int i = 0; i < MeshData.MeshCount; i++)
			meshes *= new Mesh();*
  • }*

  • public void Update()*

  • {*

  •  if (updateMesh)*
    
  •  {*
    
  •  	if (!chunk.NeighborsLoaded(true))*
    
  •  		return;*
    
  •  	updateMesh = false;*
    
  •  	ThreadManager.QueueMesh(this);*
    
  •  }*
    
  •  if (meshReady)*
    
  •  {*
    
  •  	for (int i = 0; i < MeshData.MeshCount; i++)*
    

_ data.GetMesh(meshes*, i);*_

* data.Clear();*
* meshReady = false;*

* buildingMesh = false;*
* }*

* for (int i = 0; i < meshes.Length; i++)*
_ Graphics.DrawMesh(meshes*, worldPos, Quaternion.identity, Engine.GetMaterial(i), 0);
}*_

* public void ClearMeshes()*
* {*
* for (int i = 0; i < meshes.Length; i++)*
_ meshes*.Clear();
}*_

* public void BuildMesh()*
* { *
* buildingMesh = true;*

* for (int x = 0; x < Chunk.Size; x++)*
* {*
* for (int y = 0; y < Engine.Height; y++)*
* {*
* for (int z = 0; z < Chunk.Size; z++)*
* {*
* ushort block = chunk.GetBlock(x, y, z);*

* if (block != 0)*
* block.Build(chunk, x, y, z, data);*
* }*
* }*
* }*

* meshReady = true;*
* }*
}
I assign empty meshes on start. There’s a mesh for each material that can be used.
The update function is triggered by an event (this isn’t a MonoBehaviour), and when it is flagged for updating, it sends a background thread to go through the voxels and build the mesh data.
That process adds the vertex/triangle/color/normals/uvs to the MeshData object, which stores these. There’s only one shared per all chunks because I only assign a single background thread for meshes (the rest handle terrain and lighting).
When the background thread has built the meshes, it sets a boolean which tells the main thread that it’s ready since I can’t do this on a background thread.
I call the GetMesh function in MeshData to assign the mesh data, and it looks like this:
public void GetMesh(Mesh mesh, int index)
{
* if (vertices[index].Count == 0)*
* return;*

* mesh.Clear();*
* mesh.vertices = vertices[index].ToArray();*
* mesh.normals = normals[index].ToArray();*
* mesh.uv = uv[index].ToArray();*
* mesh.colors = colors[index].ToArray();*
* mesh.triangles = triangles[index].ToArray();*
}
And then I draw it using Graphics.DrawMesh.
Now, here’s the thing:
Any time a chunk is unloaded (too far away from the player), I call ClearMeshes. As you can see, all it does is it calls Mesh.Clear().
I’d expect the memory from the mesh to be freed here, but this is not the case. All the memory from each mesh created stays allocated forever!
I even tried assigning each mesh component (vertices, triangles, etc) to an empty array of length 0 in that function and the same problem happened.
When I commented out Graphics.DrawMesh, but left all the rest, there were no leaks anymore.
Seems Graphics.DrawMesh causes this, but I’m not sure what to do about it.
Any help would be greatly appreciated.
(I’ve even tried using Destroy() on the meshes and creating new ones, but that didn’t seem to do anything to help either.)

Same problem with iOS / IL2CPP!!! Very critical, please help. Also commenting Graphics.DrawMesh remove memory leaks