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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do i remove a mesh from memory that was created by the combineMesh script?

Discussion in 'Scripting' started by kurylo3d, Nov 16, 2011.

  1. kurylo3d

    kurylo3d

    Joined:
    Nov 7, 2009
    Posts:
    1,123
    For performance reasons i often combine a whole lot of meshes together... and it happens often and it changes often. It works great, but my problem now is i realize that each time i do it .. it adds to the total vram used. It says so in unity and it will crash my android app after about 30 combines... So....

    The question now is how do i remove a mesh from memory that was created by the combineMesh script?

    I have tried using destroyImmediate() on the gameobject that houses the mesh filter... i have even went and did something like this on the mesh in the mesh filter.....

    "if (currentNode.GetComponent<MeshFilter>())
    {
    Mesh mesh = currentNode.GetComponent <MeshFilter>().mesh;
    DestroyImmediate(mesh);
    }

    DestroyImmediate(currentNode);

    "

    So what am i missing? Why do i continue to add to the VRAM usage until i run out?
     
  2. kurylo3d

    kurylo3d

    Joined:
    Nov 7, 2009
    Posts:
    1,123
    Just realize it also adds to the VBO Total ....

    My only assumption is that im creating meshes that arent being removed from memory when im done with them.... Am I wrong? What do i do?
     
  3. kurylo3d

    kurylo3d

    Joined:
    Nov 7, 2009
    Posts:
    1,123
    I have tried using...
    "DestroyImmediate(mesh, true);"
    and
    " DestroyImmediate(mesh, false);"

    neither make any difference...
     
  4. kurylo3d

    kurylo3d

    Joined:
    Nov 7, 2009
    Posts:
    1,123
    well i managed a workaround which is kind of an annoying workaround, but for now i am using "Resources.UnloadUnusedAssets()"

    And that removes stuff from the memory, but still I would love to know why the mesh doesnt get destroyed when i try... anyone know?
     
    nguyenqphan likes this.
  5. kurylo3d

    kurylo3d

    Joined:
    Nov 7, 2009
    Posts:
    1,123
    The below code is the answer I have been looking for. instead of <MeshFilter>().mesh; im using <MeshFilter>().sharedMesh;

    "if (currentNode.GetComponent<MeshFilter>())
    {
    Mesh mesh = currentNode.GetComponent <MeshFilter>().sharedMesh;
    DestroyImmediate(mesh, tue);
    }

    DestroyImmediate(currentNode);

    "
     
    Liam2349, Bunny83, KMDeclius and 6 others like this.
  6. rumanwork

    rumanwork

    Joined:
    Dec 7, 2012
    Posts:
    1
    Thank you, my good man!
     
  7. joel_b

    joel_b

    Joined:
    May 23, 2013
    Posts:
    4
    Great!
     
  8. Yang-Pil-Sun

    Yang-Pil-Sun

    Joined:
    May 10, 2013
    Posts:
    1
    Thanks hero!
     
  9. Dante_CyberdeckGames

    Dante_CyberdeckGames

    Joined:
    Mar 14, 2015
    Posts:
    4
    I know this is an older thread, just want to say thank you, as I was building up a lot of memory from unused meshes and this thread saved the day!
     
    nguyenqphan likes this.
  10. GLeBaTi

    GLeBaTi

    Joined:
    Jan 12, 2013
    Posts:
    47
    Thanks!)
     
  11. MoribitoMT

    MoribitoMT

    Joined:
    Jun 1, 2013
    Posts:
    301
    DestroyImmediate(mesh, true), is this destroys asset permenantly from project ?
     
  12. Rodniid

    Rodniid

    Joined:
    Aug 11, 2018
    Posts:
    29
    Thanks!)))))))))))))))
     
  13. Shaman7

    Shaman7

    Joined:
    May 10, 2020
    Posts:
    1
    Due to DestroyImmediate usage are not recommended I use this:
    Code (CSharp):
    1. meshFilter.sharedMesh.Clear();
    2. GameObject.Destroy(meshFilter.sharedMesh);
    3. GameObject.Destroy(<GameObjectWithMeshFilter>);
     
    KMDeclius likes this.
  14. hsc0der

    hsc0der

    Joined:
    Sep 27, 2019
    Posts:
    3
    Thanks hero!hero!hero!
     
  15. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I’m curious. I’m not familiar with the combined mesh script. But should this way be done for any mesh?
     
  16. KMDeclius

    KMDeclius

    Joined:
    Aug 12, 2019
    Posts:
    213
    Kudos!
    Thanks to actual Profilers in Unity, the mesh count can be tracked in the Profiler.
    But the issue still remains in Unity 2021 (I'm using 2021.2.13f1). A modified version of the code of @Shaman7 with reinstantiating the GameObject even when the Mesh is only replaced with another one saves me 30MByte/s of Garbage.