Huge GC Overhead When Accessing Tris/Verts From Mesh?

Hey everybody. I ran into this problem when I was looping through some meshes in Unity 4.0.0f7 I never noticed if the other versions did this as well. Unity took 10 seconds just to loop through the triangles and vertices in my scene. I opened up the profiler to find out what was so slow. It appears that there is a huge garbage collector overhead when using Mesh.triangles and Mesh.vertices_. I was writing an editor script to loop through all meshes in my scene, which was not very complex. The garbage collector generated OVER 1 GB of garbage, and used almost all of the cpu, JUST from accessing triangles via Mesh.triangles*.*
For a simple test, I attached this script to a Unity sphere and got these results in the profiler.
using UnityEngine;
using System.Collections;_

public class MeshTest : MonoBehaviour {
public Mesh mesh;

void Start() {
Vector3 v1;
Vector3 v2;
Vector3 v3;
for (int i = 0; i < mesh.triangles.Length/3; i++) {
v1 = mesh.vertices[mesh.triangles*];*
v2 = mesh.vertices[mesh.triangles[i + 1]];
v3 = mesh.vertices[mesh.triangles[i + 2]];

The script loops through each triangle and gets its vertices. Nothing more. As you can see, the garbage collector eats up most of the cpu. Its not possible for me to loop through meshes bigger than a sphere if accessing mesh data is going to be so slow. Anyone know if there is a work around? Or if this is a bug? I tried using Array.Copy and Array.Clone, so I wasn’t accessing Mesh.triangles directly, but it had no effect. Any insight would be appreciated._

I think this happens because your for-loop is acquiring a new copy of the entire triangles array every time it iterates because it has to re-evaluate the termination condition:

for (int i = 0; i < mesh.triangles.Length/3; i++) // Re-fetches the triangle array

Without wanting to assume too much knowledge of the internal workings of the Mesh class, I suspect the Mesh class returns deep copies of its data arrays (including colors, normals, vertices and triangles). That’s why these are properties (rather than public fields), and it’s why you have to re-assign arrays back to the Mesh after edits; you’re working on a copy after accessing it.

In your code, the for-loop checks the length of the triangles array by continually accessing it, so every time the for-loop iterates, the Mesh’s triangles property is probably creating a deep copy of the entire array before returning it to the for-loop. That’s what generates the huge GC overhead.

To test if this is the case, move the access to mesh.triangles outside the for-loop and store the Length in a local variable, then test against that in the for-loop instead.

Btw. Stumbled over the same problem, but I only needed to read the vert. info, so instead of

Vector3 myVec =;
Vector3[] normals = mesh.normals;
int length = normals.Length;
for (int i = 0, i < length; i++)
    Vector3 normal = normals*;*

// do something
I used
foreach (Vector3 normal in mesh.normals)
// do something
which was basically realtime for a 50k vert mesh.