Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Memory leak : thousands of mesh from the same model

Discussion in 'Scripting' started by fomafomitch, May 9, 2022.

  1. fomafomitch

    fomafomitch

    Joined:
    Nov 22, 2020
    Posts:
    86
    Hello,

    I have a scene with a 3D Model made on blender , it's a boss that fight with units.
    Recently, I found at that my memory allocation for unity goes up 100 MB/s , so in like 100seconds I am pretty much 100% RAM with my computer. Sample from early scene :
    upload_2022-5-9_17-55-16.png

    Then it goes up and what I see, there is thousands of line of this model (it's my boss ingame). It's like every frames it add the model inside memory but why ? His animation change every line. I only have one gameobject of this type in my scene.
    upload_2022-5-9_17-56-12.png
     
  2. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,366
    If it's being added then there is code that is Instantiating it or copying it. Check that you are not instantiating anything in update() or duplicating it.
     
  3. fomafomitch

    fomafomitch

    Joined:
    Nov 22, 2020
    Posts:
    86
    Is there an easy way to know what script create this mesh?

    I do not instantiate my boss because it's already in my scene.
     
    Last edited: May 9, 2022
  4. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,366
    Is any other script attached to the model? Can you post your code for the GameObject?
     
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,357
    Is there any code that accesses the mesh filter? If you deactivate your boss object does the problem go away?
     
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,323
    Make a copy of your scene and start deleting things in it until the mesh or something that causes the problem remains.
     
  7. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,463
    Are you doing anything with meshfilter.mesh? And are you reassigning to that?
    I ran into this problem once with procedural mesh generation albeit I thought to have taken care to destroy the meshes I replace. Calling ".mesh" does produce a copy every time that mesh instance is accessed the first time (the intention is to allow per-instance editing of a mesh).
    You gotta use ".sharedMesh" instead.
    Similar with renderer.material -> renderer.sharedMaterial by the way.
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    Put debug logs, where you create meshes.

    BTW, this is wrong sub forum.
    General forum is not support forum.
    Should be in scripting.
     
  9. fomafomitch

    fomafomitch

    Joined:
    Nov 22, 2020
    Posts:
    86

    Thank you for your tips it was exactly that ! I have a script that scan mesh to add effect to the skin of the boss and it was that. The refreshrate was every frames so I changed it ! The script :

    Code (CSharp):
    1. public class SkinnedMeshToMesh : MonoBehaviour
    2. {
    3.     public SkinnedMeshRenderer skinnedMesh;
    4.     public VisualEffect VFXGraph;
    5.     public float refreshRate; // Was every frames
    6.     void Start()
    7.     {
    8.         StartCoroutine (UpdateVFXGraph());
    9.     }
    10.  
    11.     IEnumerator UpdateVFXGraph()
    12.     {
    13.         while (gameObject.activeSelf)
    14.         {
    15.             Mesh m = new Mesh();
    16.             skinnedMesh.BakeMesh(m);
    17.             Vector3[] vertices = m.vertices;
    18.             Mesh m2 = new Mesh();
    19.             m2.vertices = vertices;
    20.  
    21.             VFXGraph.SetMesh("Mesh", m2);
    22.            
    23.             yield return new WaitForSeconds (refreshRate);
    24.         }
    25.     }
    26. }
     
  10. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    You may not need to create new meshes each frame. Consider creating m and m2 outside of the while loop, then clearing them inside the loop before baking and assigning the vertices.