The Gizmo class does not release memory

I have the following simple code below, the problem is that the dispose() function does not work … I have no other references anywhere in source code, but the memory will pile up to cloud heights. How to force Babylon or the browser to free that memory away? Thanks in advance.

var counter1 = 0;

    var counter2 = 0;

    var gizmo;

    var utilLayer;

    

    function DrawGizmo()

    {

        if (counter1 == 1) {

            utilLayer.dispose();

            gizmo.dispose();

        }

        counter2 = counter2 + 0.001;

        // Create utility layer the gizmo will be rendered on

        utilLayer = new BABYLON.UtilityLayerRenderer(scene);

        // Create the gizmo and attach to the sphere

        gizmo = new BABYLON.AxisDragGizmo(new BABYLON.Vector3(1, counter2, 1), new BABYLON.Color3(106 / 255, 201 / 255, 249 / 255), utilLayer);

        gizmo.attachedMesh = sphere;

        //change colors gizmo

        gizmo._coloredMaterial.specularColor = new BABYLON.Color3(106 / 255, 201 / 255, 249 / 255);

        gizmo._hoverMaterial.diffuseColor = new BABYLON.Color3(106 / 255, 201 / 255, 249 / 255);

        gizmo._disableMaterial.diffuseColor = new BABYLON.Color3(106 / 255, 201 / 255, 249 / 255);

        gizmo._hoverMaterial.specularColor = new BABYLON.Color3(106 / 255, 201 / 255, 249 / 255);

        counter1 = 1;

    }

You can’t force the browser to release memory, it will do it whenever it seems appropriate, as long as there are no more references to the objects that must be freed.

To know if the memory is released(able), you can try to force a garbage collection and see if the memory lowers: in the Memory tab of Chrome you have a trash icon which will force a garbage collection when clicked.

If that doest not work, you will need to provide a playground that demonstrates the problem for us to help more.

1 Like

Hello there, I’m sending below - the playground situation which I described. The “Dispose” method is for decoration. Babylon, the browser accumulates a huge amount of data in the ram memory of my PC, the memory is not released. Please adjust the playground to see where I’m making big mistake of rookie. Thank you very much in advance.

https://playground.babylonjs.com/#25M5MN#6

Is it because you should defined scene as global as well ? I am currently seeing an error so it keeps creating without disposing cause scene is undefined ?

2 Likes

Making scene and sphere global variables (as @sebavan suggests) resolves the console errors: same color axis drag | Babylon.js Playground (babylonjs.com). The issue is that they were only defined in the scope of createScene(), so DrawGizmo wasn’t able to reference them.

Does that help?

2 Likes

Thank you for the playground. It probably helped partly. I’m looking for another skeleton in the closet …

1 Like

I am seeing a relatively steady memory usage at around 40-50mb, but I might not be measuring correctly. Could you show me your data on the memory use? I am just not able to repro the leak ATM

1 Like