Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by kofe · Oct 20, 2015 at 11:39 PM · meshmemorymeshrenderermeshfilterleak

Possible material or mesh leak

Hello!

I searched here some answers but they didn't help me with my problem...

I want to create a random cave generator (see the lesson) but with some modes. The idea is simple – I press RMB and new cave is generated. I decided to load material from recourses by code.

What I'm doing:

    1. I'm generating map and loading material "Cave".
    2. I'm creating a helping squares to draw mesh with a "new CSquareGridXY(genMap, width, height, 1)".

private CRandMap map;
private Material caveMat;

    void Start () {
               SetUI();    
                map = new CRandMap(width, height, threshold,useSeed, seed);
                genMap = map.Generate();
                caveMat = Resources.Load("Cave") as Material;
                new CMeshGenerator(new CSquareGridXY(genMap, width, height, 1), caveMat);        
            } 

    3. In a "new CMeshGenerator" I'm creating vertices and triangles and assaying them all. Here is the leak, but i don't know where exactly is. In method "prepareMesh" I fill the Lists of vertices and triangles

  public class CMeshGenerator
     {
    
    public  List<Vector3> vertices;
    public List<int> triangles;
 
     MeshFilter meshF;
     MeshRenderer meshR;
     Mesh mesh;
 
     public CMeshGenerator(CSquareGridXY grid, Material material)
     {
 
         vertices = new List<Vector3>();
         triangles = new List<int>();
         mesh = new Mesh();
 
         
         GameObject gameObject = GameObject.Find("GameController");
 
         meshF = gameObject.GetComponent<MeshFilter>();
         
         if(meshF == null)
             meshF = GameObject.Find("GameController").AddComponent<MeshFilter>();
 
         meshR = gameObject.GetComponent<MeshRenderer>();
         if (meshR == null)
             meshR = GameObject.Find("GameController").AddComponent<MeshRenderer>();
       
 
 
         for (int y = 0; y < grid.squares.GetLength(1); y++)
         {
             for (int x = 0; x < grid.squares.GetLength(0); x++)
             {
                 PrepareMesh(grid.squares[x, y]);
             }
         }
 
         mesh.SetVertices(vertices);
         mesh.SetTriangles(triangles,0);
         mesh.RecalculateNormals();
 
        
         meshF.sharedMesh = mesh;
         meshR.sharedMaterial = material;
            
     }
 ...
 }

    4. On Update I only recreate my CMeshGeneratoclass.

   void Update () 
 { 
 CheckUI(); 
    
     if (Input.GetMouseButtonDown(1))
     {
         
         map.MapUpdate(threshold, useSeed, seed);
         genMap = map.Generate();
                    
         new CMeshGenerator(new CSquareGridXY(genMap, width, height, 1), caveMat);
          GC.Collect();
            
    }

I'll be glad if someone can help me with this problem :)

Comment
Add comment · Show 1
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 maccabbe · Oct 21, 2015 at 02:32 AM 0
Share

http://docs.unity3d.com/ScriptReference/Resources.UnloadUnusedAssets.html

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Oct 21, 2015 at 03:20 AM

Materials and Meshes are like most things that are derived from UnityEngine.Object managed by Unity itself. So they aren't garbage collected even when you have lost all references to them. You can still regain a reference by using FindObjectsOfType.

Since you replace the mesh of an existing object with a new one, the old Mesh still exists. So you either should "Destroy()" the old mesh before you assign the new one, or simple reuse the mesh. You can call Clear() on the mesh to start new. However if you assign new arrays to the mesh that isn't even necessary.

So instead of mesh = new Mesh(); you should do after you have the MeshFilter reference:

 mesh = meshF.sharedMesh; 
 if(mesh == null)
     meshF.sharedMesh = mesh = new Mesh();
 // mesh.Clear();

The Material shouldn't be a problem since you already reuse the same material and assign it to the sharedMaterial property. Watch out: If you call Destroy on references to assets in your project, they will be gone for good. That's actually true for all kind of assets. In a build those assets won't be erased forever, but you would have to restart the application if you delete an asset. In the editor however you literally delete it. So be careful when you "clean up" ^^. Unity has now a security check when you try to destroy an asset. That's why you shouldn't use DestroyImmediate in runtime code ^^.

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 kofe · Oct 21, 2015 at 11:38 AM 0
Share

Omg! I'm so thankful for you! You are my savior! :3 Your advice solved my problem) But I steel have some misunderstanding: Why I have had leak (20-40 kb) every second, when I created only one mesh (call constructor only once). And when I use old mesh (mesh = meshF.shared$$anonymous$$esh) leaks stoppes. I can understand if I were constantly calling constructor and it was creating new mesh every time and loosing old pointer, but even when I called it once I have had leak, why is that? =\

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Mesh memory leak 3 Answers

Im reading a .obj file and converting it to a mesh (doesnt work) 0 Answers

Submeshs doesn't combine with materials 1 Answer

Why is my script constructed mesh rotating the wrong way 0 Answers

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