x


Procedural Mesh leaks memory everytime the game object is deleted in the editor

Each time i delete the game object, its mesh seem to be still existing. Is there something explicitly i have to do to delete the mesh?

I tried delete the mesh in OnDisable() using DestroyImmediate but the side effect is the mesh gets deleted if i hide the gameobject.

Am creating a mesh from scratch like this:

public class RouteCmp : MonoBehaviour {

    ...

    [SerializeField]
    Mesh m_Mesh=new Mesh();

    void Awake()
    {
        GetComponent<MeshFilter>().mesh = m_Mesh;
        GetComponent<MeshCollider>().sharedMesh = m_Mesh; 
    }

    public void BuildMesh()
    {
        ...
        ...

        this.Mesh.Clear();
        this.Mesh.vertices = vVertices;
        this.Mesh.colors = aColors;
        this.Mesh.uv = vVertUV;
        this.Mesh.triangles = nTriangleIndex;

    }
}

The image shows the before and after screen shots Click here

more ▼

asked Jan 11 '10 at 09:15 PM

pkamat gravatar image

pkamat
443 11 12 20

humm image link is missing.. http://img687.yfrog.com/i/meshleak.jpg/

Jan 11 '10 at 09:18 PM pkamat
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Currently the best you can do is to delete the mesh in OnDisable() like you mentioned yourself.

If you want to avoid deleting the mesh when hiding the GameObject, you can delete the mesh in your own function (say, MyDestroy), and manually call that function when you know you are about to destroy the GameObject for real (not just hiding it).

The mesh is not garbage collected because the engine still keeps a reference to it. You can find the meshes you have created by using FindObjectsOfType(), even if no GameObject currently keeps a reference to it. This is also why it is shown in the editor.

If you want to make Unity garbage collect objects that are not currently used, you can use Resources.UnloadUnusedAssets() - this is also called by Unity whenever you load a new scene. However, in your case it is definitely better to destroy the mesh yourself since you know it is not needed by other objects.

more ▼

answered Jan 12 '10 at 01:27 PM

runevision gravatar image

runevision ♦♦
8.1k 29 46 112

Hello Rune, i dont think there is a way to differentiate between hiding and actual deletion. as both of the call OnDisable.

For now i delete the mesh in OnDisable and create it again in OnEnable. I still dont understand why it requires manual deletion when everything else is garbage collected. Its is inconsistency am concerned about.

Jan 13 '10 at 05:33 AM pkamat

I added to my answer above to explain this.

Jan 13 '10 at 03:53 PM runevision ♦♦

Thank you so much for this! Completely fixed my problem!

Thank you again!

Oct 11 '11 at 03:59 PM HarvesteR
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5096
x328

asked: Jan 11 '10 at 09:15 PM

Seen: 3333 times

Last Updated: Oct 11 '11 at 03:59 PM