Memory usage increase after a mesh rewrite???

Okay, this is going to be complicated but I want to explain what I’m doing here.

I’ve only been learning Unity for about a year and although I’ve learned A LOT from others, this is my first question that I’ve posted. I’m really hoping that someone can point out something very simple that I am missing as this is crucial to my goals.

I am building a game that amongst many other things is going to require that many random worlds to be generated. I wrote an algorithm that does just that, without getting into the specifics. It works, beautifully in fact…

So, what exactly are you looking at? Well, it’s a cube map that stores height data essentially…simply six “plane-like” meshes whose vertices have been normalized into the shape of 1/6 a sphere each. The problem that I am having is in the mesh updates. The above picture is after 5000 iterations of the randomization algorithm. Initially, I set the script to update the meshes (all 6) every 5 iterations…an amount that with only a single body even on high settings allows a surprisingly smooth frame rate considering the number of calculations performed.

However, optimization is a priority at this point and to that end I have completely revamped the code recently. However, there remains a major “anomaly” that is preventing me from fully implementing this. What I noticed though is that with every mesh update the memory used increases. Here is an earlier iteration:

One mesh update later (I adjusted it to update every 250 iterations as this minimized the effect of this issue) it is using roughly 4MB more memory (roughly the amount the program uses to create the planet initially). This continues every update until as in the first picture it has eventually consumed an amount of memory many, many times its original size.

I have isolated this to the mesh update but cannot seem to fix it. I imagine it will be something simple as usual. Like I said, I am a relative newbie to Unity. Here is the code that updates the meshes and mind the custom classes…

public void updateMesh() {
	normalizeFace ();
	// create the temporary arrays
	Vector3[] meshVertices = new Vector3[numVertices];
	Vector2[] meshUV = new Vector2[numVertices];
	Color32[] meshColors = new Color32[numVertices];
	int[] tris = new int[numTris * 3];
	// loop to populate the arrays
	for (int x = 0; x < numVertices; x++) {
		meshVertices[x] = meshData[x].pos;
		meshUV[x] = meshData[x].uv;
		meshColors[x] = meshData[x].color;
	// triangles
	for (int x = 0; x < numTris; x++) {
		tris[x*3] = meshTris [x].vertices[0];
		tris[x*3+1] = meshTris [x].vertices[1];
		tris[x*3+2] = meshTris [x].vertices[2];

	// throw the lists to the mesh
	// create the temporary mesh variable or clear the old
	if (publicMesh == null) {
		publicMesh = new Mesh ();
	} else {
		publicMesh.Clear ();
	publicMesh.vertices = meshVertices;
	publicMesh.uv = meshUV;
	publicMesh.colors32 = meshColors;
	publicMesh.triangles = tris;
	publicMesh.RecalculateNormals ();
	GetComponent<MeshFilter> ().mesh = publicMesh;


This all seems right to me but yet every time I call this function, it seems to create and lose another instance somewhere. This obviously needs fixed if I am to ever scale this up and create dozens of these at a time…which should be very easy if I can keep them at a manageable ~4MB each.

If anybody can offer some insight, I would very greatly appreciate it. Any memory management aspects that for whatever have eluded me? I feel that this should be easy to solve, I just apparently do not have the right information.

Nevermind, everyone. As so often happens, I answered my own question here. I modified the last few lines of code to be:

publicMesh.RecalculateBounds ();
publicMesh.RecalculateNormals ();
publicMesh.Optimize ();
GetComponent<MeshFilter> ().mesh = publicMesh;

The Optimize() in particular is important for procedurally generated meshes. This seems to have completely stabilized the memory usage. Now, I can generate many of these objects simultaneously without issue. Most excellent!!!