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
2
Question by Glaskows · Jun 05, 2014 at 07:59 PM · meshmemoryproceduralmemory-leakleak

Mesh memory leak

Hi, the problem I am having is that I have a class that creates a mesh that then I assign to a MeshFilter which is added to a new gameobject. All of this gameobject are added to a dictionary that I after traverse for candidates to be deleted. Once I found a candidate, I use

 DestroyImmediate( pairChunk.Value.m_gameObject.GetComponent<MeshFilter>().mesh );
 DestroyImmediate( pairrChunk.Value.m_gameObject );

The problem is that they are not being destroyed, the number of Resources.FindObjectsOfTypeAll().Length just keeps getting bigger, as the mesh memory used.

I have read every other thread I could find on the subject, but I haven't found a solution that could help me. I will make a pool of meshes instead of creating new ones each step, because I know the ceiling of how many I will use, but I want this to be resolved so I have a grasp of whats going on, and what I am doing wrong.

By the way, I create the mesh components (vertices, indexes, normals, uvs) inside a thread, and after I compose the mesh in the main thread, using a modified not-singleton loom class.

 loom.RunAsync( () => {
     chunk.CalculateMesh();
     loom.QueueOnMainThread( () => {
         filter.mesh = chunk.CreateMesh();
         filter.renderer.material = m_terrainMaterial;
     } );
 } );

If you have any clue I could follow, I would be greatly appreciated. Thanks!

Comment
Add comment · Show 3
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 rutter · Jun 05, 2014 at 08:05 PM 0
Share

Just to get the easy question out of the way: have you tried Resources.UnloadUnusedAssets?

avatar image Glaskows · Jun 05, 2014 at 08:12 PM 1
Share

Yeah... in fact, if, after the destroying method I do

 Debug.Log ( "BEFORE: " + Resources.FindObjectsOfTypeAll<$$anonymous$$esh>().Length );
 Resources.UnloadUnusedAssets();
 Debug.Log ( "AFTER: " + Resources.FindObjectsOfTypeAll<$$anonymous$$esh>().Length );

I get the same result in both lines, and the mesh count doesn't goes down

avatar image Itaros · Oct 28, 2014 at 07:07 PM 0
Share

Vertices array and all of that are considered part of the mono heap. Are you sure it actually leaks ins$$anonymous$$d of allowed heap expansion?

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Bunny83 · May 09, 2016 at 11:00 PM

Don't use Destroy( GetComponent<MeshFilter>().mesh );. The .mesh property will create an internal copy of the mesh for this MeshFilter instance at the first time you access the property. If you already created the mesh by hand you should exclusively use .sharedMesh. The ,sharedMesh property hold the reference to the original mesh instance.

Just as follow up, the same thing happens with material / sharedMaterial and materials / sharedMaterials. The material and mesh property has been created to allow easy manipulation of a Material / Mesh for this MeshFilter / Renderer only.

Even more generally every class that is derived from UnityEngine.Object is a "tracked object". Instances of that type always have a native C++ counterpart. Once created they must be destroyed manually with "Destroy". The only exception are Components. Since components can't live without the parent GameObject, they are automatically destroyed along with the parent gameobject. Any other class derived from Unity's Object class need to be destroyed manually. That includes: Mesh, Material, Shader, any ScriptableObject, GameObject, Texture2D, AnimationClip, AssetBundle, AudioClip and some others.

Watch out to only destroy instances that are created at runtime. If you call Destroy on an asset reference when testing inside the editor, the asset can be deleted permanently. Calling Destroy on an asset in a build doesn't do permanent damage, but you can't use that asset until the next application restart.

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
avatar image
1

Answer by laessnb · Oct 28, 2014 at 06:55 PM

Have you perhaps tried destroying MeshFilter.sharedMesh? MeshFilter.mesh always returns a duplicate (or, after it creates the duplicate once, a reference to that duplicate), so you're basically creating and immediately destroying that duplicate rather than destroying the original one.

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
avatar image
0

Answer by cijolly · May 09, 2016 at 02:55 PM

@Glaskows

Did you ever find a solution to your problem? I had the exact same problem, with the profiler showing meshes rocketing up to take up gigs of RAM and crashing the program within 20 minutes. The following has fixed my problem. The big performance boost memory wise came from saving a reference to the collision mesh and initialising it only once during startup. After that to update the mesh I use colMesh.Clear(), set the verts and triangles to the calculated values, then use meshCollider.sharedMesh = colMesh.

Prior to this, I was using Mesh colMesh = new Mesh() every time I updated. I went from a gig of memory taken up by the mesh in 20 minutes when instantiating a new mesh every update, to 150megs taken up by meshes not matter how long the game runs by keeping a reference to the existing one.

 //Attach script to a GameObject with a MeshFilter, MeshRenderer, and MeshCollider
 [RequireComponent (typeof (MeshFilter))]
 [RequireComponent (typeof (MeshRenderer))]
 [RequireComponent (typeof (MeshCollider))]


 //Variable declaration        
 private Mesh faceMesh;                                        
 private MeshCollider meshCollider;
 private MeshFilter meshFilter;
 private Mesh colMesh;
 
 private Vector3[] faceVertices;
 private int[] faceTris;
 private Color32 [] faceColors;
 private Vector3[] colVertices;
 private int[] colTris;
 
 
 //Called externally when the chunk is instantiated
 public void Initialise(){
         meshFilter = GetComponent<MeshFilter> ();
         faceMesh = GetComponent<MeshFilter> ().mesh;
         meshCollider = GetComponent<MeshCollider> ();
         colMesh = new Mesh ();
 
         faceVertices = new Vector3[maxNumFaceVerts];
         faceTris = new int[maxNumFaceTris * 3];
         faceColors = new Color32[maxNumFaceVerts];
         colVertices = new Vector3[maxNumColVerts];
         colTris = new int[maxNumColTris * 3];
 }
 
 public void UpdateMesh(){
         faceMesh.Clear ();
         faceMesh.vertices = faceVertices;
         faceMesh.triangles = faceTris;
         faceMesh.colors32 = faceColors;
         faceMesh.Optimize ();
         faceMesh.RecalculateNormals ();
 
         colMesh.Clear ();
         colMesh.vertices = colVertices;
         colMesh.triangles = colTris;
         meshCollider.sharedMesh = colMesh;
 }
Comment
Add comment · Show 1 · 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 Bunny83 · May 09, 2016 at 11:20 PM 0
Share

See my answer for more details on the issue. Also note that Glaskows hasn't been on here since march 2015.

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

26 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 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

0 or Blank Ref Count Items in Memory Profiler 0 Answers

Memory problems with procedurally generated meshes 2 Answers

Huge world without huge lag? 1 Answer

m_Memory.renderTextureBytes<0 1 Answer

Unity not unloading procedurally generated mesh when parent GameObject is destroyed and its not referenced 0 Answers


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