# Safari memory leak when scene dipose and rebuid serval times

**URL:** <https://discourse.threejs.org/t/safari-memory-leak-when-scene-dipose-and-rebuid-serval-times/59014>\
**Category:** Questions\
**Tags:** memory-leak\
**Created:** [December 8, 2023, 9:24am UTC](https://discourse.threejs.org/t/safari-memory-leak-when-scene-dipose-and-rebuid-serval-times/59014 "2023-12-08T09:24:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![duanjz.nnu](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/duanjz.nnu/32/42756_2.png) [@duanjz.nnu](https://discourse.threejs.org/u/duanjz.nnu)\
**Post date:** [December 8, 2023, 9:24am UTC](https://discourse.threejs.org/t/safari-memory-leak-when-scene-dipose-and-rebuid-serval-times/59014/1 "2023-12-08T09:24:17Z")

</div>

I have created a scene using THREEJS that loads GLTF files and image-format tile maps. When I repeatedly destroy and rebuild the scene(ensuring that each destruction removes DOM nodes and all relevant THREEJS objects), I notice that the memory keeps increasing in the Safari browser, but the same code performs normally in the Chrome browser. I have ensured that I properly dispose of all relevant resources when destroying the scene.

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/3/c/3c049eaab626b7b918026fca8a965591d550fde4.png)  
 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/c/d/cd5f255d1e3623374b96f011a4f54dc4e359cb03.png)

The following is the core code of my destruction function:

```javascript
static disposeObject(object, withMaterial: boolean = true){
        const self = this;
        const disposeMaterial = (material: Material) => {
            Object.getOwnPropertyNames(material).forEach((property: string) => {
                const materialProperty = (material)[property];
                if (materialProperty !== undefined && materialProperty instanceof Texture) {
                    const texture = materialProperty;
                    const image = texture.source.data;
                    if (image instanceof ImageBitmap) {
                        console.log('dispose imagebtmap');
                        image.close();
                    }else{
                        console.log('dispose image');
                        texture.image = null;
                    }
                    texture.dispose();
                }
            });
            material.dispose();
        };

        if (object instanceof Object3D) {
            if (object.parent) {
                object.parent.remove(object);
                object.parent = null;
            }
        }

        if(object.children&&object.children.length>0){
            for (let i = object.children.length -1; i >=0 ; i--) {
                this.disposeObject(object.children[i]);
            }
        }
        object.children.length = 0;
        object.children = null;

        if (object.geometry !== undefined) {
            object.geometry.dispose();
        }

        if (object.geometries !== undefined) {
            for (const geometry of object.geometries) {
                for (const key in geometry.attributes) {
                    geometry.deleteAttribute(key);
                }
                geometry.setIndex([]);
                geometry.dispose();
            }
        }

        if (object.material !== undefined && withMaterial ) {
            if (object.material instanceof Array) {
                object.material.forEach((material: Material | undefined) => {
                    if (material !== undefined) {
                        disposeMaterial(material);
                    }
                });
            } else {
                disposeMaterial(object.material);
            }
        }
        for(let key in object.userData){
            delete object.userData[key];
        }
        object.userData = {};
        object = undefined;
    };

```

THREEJS Version: r138  
Google Chrome Version: 119.0.6045.200  
Safari Version: 17.0(19616.1.27.211.1)  
Is there anyone who can give me some advice? Thanks！！！

---

<div class="post-metadata">

**Author:** ![manthrax](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/manthrax/32/2596_2.png) [@manthrax](https://discourse.threejs.org/u/manthrax)\
**Post date:** [December 12, 2023, 5:08am UTC](https://discourse.threejs.org/t/safari-memory-leak-when-scene-dipose-and-rebuid-serval-times/59014/2 "2023-12-12T05:08:12Z")

</div>

Can you link the bug you posted on Safari’s bug report channel?

---

<div class="post-metadata">

**Author:** ![andrewhodel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/andrewhodel/32/54739_2.png) [@andrewhodel](https://discourse.threejs.org/u/andrewhodel)\
**Post date:** [December 15, 2024, 5:22pm UTC](https://discourse.threejs.org/t/safari-memory-leak-when-scene-dipose-and-rebuid-serval-times/59014/3 "2024-12-15T17:22:50Z")

</div>

This is a discussion about the same problem with consideration of the condition in `GLTFLoader` regarding Safari version 17 and the fact that today Safari 18 is used.

> [@Proof that GLTFLoader does not free memory in Safari that it frees in Chrome](https://discourse.threejs.org/t/proof-that-gltfloader-does-not-free-memory-in-safari-that-it-frees-in-chrome/75201/10):
>
> Ohh I see its an explicit fallback. Related (?): smiley

---

<div class="post-metadata">

**Author:** ![andrewhodel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/andrewhodel/32/54739_2.png) [@andrewhodel](https://discourse.threejs.org/u/andrewhodel)\
**Post date:** [December 15, 2024, 5:23pm UTC](https://discourse.threejs.org/t/safari-memory-leak-when-scene-dipose-and-rebuid-serval-times/59014/4 "2024-12-15T17:23:30Z")

</div>

There is also a link to JSFiddle with an example that demonstrates the memory leak in less than 50 lines of code.

> [@Proof that GLTFLoader does not free memory in Safari that it frees in Chrome](https://discourse.threejs.org/t/proof-that-gltfloader-does-not-free-memory-in-safari-that-it-frees-in-chrome/75201/10):
>
> Ohh I see its an explicit fallback. Related (?): smiley

@duanjz.nnu

---

<div class="post-metadata">

**Author:** ![lpassini](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/lpassini/32/53164_2.png) [@lpassini](https://discourse.threejs.org/u/lpassini)\
**Post date:** [September 17, 2025, 12:24pm UTC](https://discourse.threejs.org/t/safari-memory-leak-when-scene-dipose-and-rebuid-serval-times/59014/5 "2025-09-17T12:24:33Z")

</div>

Hello, are there any news about this? We have the same exact problem on Safari.

Thanks.
