Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by janzdott · Mar 13, 2013 at 01:36 PM · meshverticestrianglesgcgarbage collector

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[i] and Mesh.vertices[i]. 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[i].

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[i]];
             v2 = mesh.vertices[mesh.triangles[i + 1]];
             v3 = mesh.vertices[mesh.triangles[i + 2]];
         }
     }
 }

alt text

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.

meshtest.png (40.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by CHPedersen · Mar 13, 2013 at 01:56 PM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image janzdott · Mar 13, 2013 at 02:30 PM 0
Share

You're right! I spent an hour last night messing with this. Reassigning the arrays, copying them, cloning them. Just to realize that I forgot to change one of my for loops that was still accessing mesh.triangles. DOOOH! Thanks for helping me realize that! Its the dumb mistakes that always get me -_-

avatar image Bunny83 · Mar 13, 2013 at 02:39 PM 0
Share

Exactly. To be more precise in each iteration you create 4 triangles-array copies and 3 vertices-array copies.

Btw: If you plan to modify the vertices multiple times you should keep the vertices array in memory, change it and just re-assign it. Unity will still have to "copy over" the data, but it doesn't create a new array each time.

avatar image CHPedersen · Mar 13, 2013 at 03:09 PM 0
Share

Yeah, I realize I was inaccurate in the answer. I only saw afterwards that multiple accesses were taking place in the loop body as well. :)

avatar image
0

Answer by Blenderik · Dec 27, 2017 at 12:11 PM

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

 Vector3 myVec = Vector3.zero;
 Vector3[] normals = mesh.normals;
 int length = normals.Length;
 for (int i = 0, i < length; i++)
 {
     Vector3 normal = normals[i];
     // do something
 }

I used

 foreach (Vector3 normal in mesh.normals) 
 {
 // do something
 }

which was basically realtime for a 50k vert mesh.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Connecting flatshaded vertices 0 Answers

can 5 verts and 4 triangles be done on a square texture? 1 Answer

Does mesh.triangles also return a copy of the triangle array? 1 Answer

Rebuilding mesh.triangles after removing vertices 1 Answer

Generated Mesh Triangles not Being Made/Visible? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges