Mesh that is regenerated every frame causes constant memory growth! How to avoid?

The game I’m working on has some objects in the scene whose meshes need to be dynamically regenerated every frame. For this purpose, I’ve been using the following code:

// VertexList is generated vertex data that I need to create a mesh from.
Vector3[] vertices = new Vector3[vertexList.Length];
for(int i = 0; i < vertices.Length; i += 6)
{
    vertices _= vertexList*.lowerRight;*_

vertices[i+1] = vertexList*.lowerLeft;*
vertices[i+2] = vertexList*.upperRight;*
vertices[i+3] = vertexList*.lowerLeft;*
vertices[i+4] = vertexList*.upperLeft;*
vertices[i+5] = vertexList*.upperRight;*
}
int[] triangles = new int[vertexList.Length];
for(int i = 0; i < triangles.Length; i++)
{
triangles = i;
}

Vector2[] uvs = new Vector2[vertexList.Length];
for(int i = 0; i < uvs.Length; i++)
{
uvs_.x = vertexList*.uv1X*
uvs.y = vertexList*.uv1Y;
}*_

Vector2[] uvs2 = new Vector2[vertexList.Length];
for(int i = 0; i < uvs2.Length; i++)
{
uvs2_.x = vertexList*.uv2X;
uvs2.y = vertexList.uv2Y;
}*_

Color[] colors = new Color[vertexList.Length];
for(int i = 0; i < colors.Length; i++)
{
colors_.r = vertexList*.someColorDataR;
colors.g = vertexList.someColorDataG;
colors.b = vertexList.someColorDataB;
}
// Must always clear the mesh before assigning new verts/triangles/etc.*

mesh.Clear(false);_

// Assign it all to the mesh.
mesh.vertices = vertices;
mesh.uv = uvs;
mesh.uv2 = uvs2;
mesh.colors = colors;
mesh.triangles = triangles;
This works great and executes quickly, but I noticed while profiling the game that mono’s heap size increases at constant rate and doesn’t decrease, ultimately leading to a crash, especially on iOS devices. Calling GC.Collect() helps to resolve this issue, but obviously kills the frame-rate, and I’d prefer to avoid that all together if possible.
Ideally, I’d like to create my vertices, uvs, uvs2, colors, and triangles arrays once and reuse them for every frame, but I also need to take into account that the data I’m using isn’t always the same size - sometimes the number of vertices required will be small, sometimes large. I’d be happy to just size the arrays to the largest required vertex amount, but it seems that if I have a large array only partially filled with data, Unity won’t be able to use that correctly either.
So, the crux of the issue is that doing this every frame starts to eat up a lot of memory. I’m guessing this is due to creating “new” arrays every frame, but I’m not sure how I can avoid that, given the way the mesh class takes in vertex data. Has anyone encountered similar issues, or does anyone know of a way to work around this? Thanks!

I found that pulling the array declarations out of the loop that runs every frame fixed the problem. Since arrays in C# ARE classes, creating a new instance of a class every frame isn’t all that great. In fact, creating new instances of structs every frame also seems less than ideal in some cases!

Anyway, I made the vertices, triangles, uvs, uvs2, and colors arrays class member variables and initialized them to a basic size on startup. Then, as vertex lists showed up, I checked whether the current arrays could fit all the data. If not, I do a resize operation, but a smaller list of vertex data will use larger arrays correctly.

When the actual vertex data is smaller than my array sizes, I just fill the extra position data with 0s. This seems to work fine.