# Memory Management: Cleaning up \_geometriesByUniqueId, Material meshMap, and clonedMeshMap

**URL:** <https://forum.babylonjs.com/t/memory-management-cleaning-up-geometriesbyuniqueid-material-meshmap-and-clonedmeshmap/52990>\
**Category:** Bugs\
**Tags:** geometry\
**Created:** [August 24, 2024, 12:35pm UTC](https://forum.babylonjs.com/t/memory-management-cleaning-up-geometriesbyuniqueid-material-meshmap-and-clonedmeshmap/52990 "2024-08-24T12:35:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yong\_hu](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/yong_hu/32/36242_2.png) [@yong\_hu](https://forum.babylonjs.com/u/yong_hu)\
**Post date:** [August 24, 2024, 12:35pm UTC](https://forum.babylonjs.com/t/memory-management-cleaning-up-geometriesbyuniqueid-material-meshmap-and-clonedmeshmap/52990/1 "2024-08-24T12:35:56Z")

</div>

Hello Babylon.js community,

I’m working on a project that renders vehicle visualization data. I’m trying to optimize memory usage by removing unnecessary references. I have a few questions about my current approach:

1. Is it safe to directly access and modify the `_geometriesByUniqueId` property of the scene?
2. Are there any potential issues with the following code that aims to clean up `_geometriesByUniqueId`, `material.meshMap`, and `material.clonedMeshMap`?
3. Is this an effective way to prevent memory growth, or are there better approaches?
4. How should I properly handle and clean up the `clonedMeshMap`?

Here’s the code I’m currently using:

```typescript
// Cleaning up _geometriesByUniqueId
const geometriesByUniqueId = (this._scene as any)?._geometriesByUniqueId as
  | Record<string, number>
  | undefined;
if (geometriesByUniqueId) {
  Object.entries(geometriesByUniqueId).forEach(([key, value]) => {
    if (value === undefined) {
      delete geometriesByUniqueId[key];
    }
  });
}

// Cleaning up material.meshMap and material.clonedMeshMap
this._scene?.materials.forEach((mat) => {
  if (mat.meshMap) {
    Object.entries(mat.meshMap).forEach(([key, mesh]) => {
      if (mesh === null || mesh === undefined) {
        delete mat.meshMap![key];
      }
    });
  }
  
  // Attempt to clean up clonedMeshMap ,this is pseudocode
  if ((mat as any).clonedMeshMap) {
    Object.entries((mat as any).clonedMeshMap).forEach(([sourceId, clones]) => {
      if (Array.isArray(clones)) {
        (mat as any).clonedMeshMap[sourceId] = clones.filter(clone => clone && !clone.isDisposed());
        if ((mat as any).clonedMeshMap[sourceId].length === 0) {
          delete (mat as any).clonedMeshMap[sourceId];
        }
      }
    });
  }
});

```

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/1/5/15f4ddeaea6421365a023317756b3304eab1952a.jpeg)

Additional context:

- I’m using TypeScript with Babylon.js
- The `_geometriesByUniqueId` object can potentially grow to contain millions of entries
- I’m dealing with cloned meshes, and I’m not sure if my approach to cleaning up `clonedMeshMap` is correct or efficient
- I’m concerned about both memory usage and performance implications

Specific questions about `clonedMeshMap`:

1. Is my approach to cleaning up `clonedMeshMap` correct?
2. Are there any risks in removing entries from `clonedMeshMap`?
3. Should I be concerned about potential side effects when modifying `clonedMeshMap`?
4. Is there a more efficient or recommended way to manage cloned meshes and prevent memory leaks?

Any insights, best practices, or alternative approaches would be greatly appreciated. Thank you in advance for your help!

Version: [6.27.1]  
Browser: [chrome]

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [August 26, 2024, 8:39am UTC](https://forum.babylonjs.com/t/memory-management-cleaning-up-geometriesbyuniqueid-material-meshmap-and-clonedmeshmap/52990/2 "2024-08-26T08:39:41Z")

</div>

cc @sebavan

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [August 26, 2024, 5:27pm UTC](https://forum.babylonjs.com/t/memory-management-cleaning-up-geometriesbyuniqueid-material-meshmap-and-clonedmeshmap/52990/3 "2024-08-26T17:27:41Z")

</div>

In theory, you should not touch any \_… function as they are all internal implementation detail that can and probably will change without notices.

That said those maps should take almost no memory. I can not understand why would they grow so much you need to clean them.

> - The `_geometriesByUniqueId` object can potentially grow to contain millions of entries

Could you share a repro for that ? we should instead fix the root cause within the framework 🙂

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [September 3, 2024, 3:18pm UTC](https://forum.babylonjs.com/t/memory-management-cleaning-up-geometriesbyuniqueid-material-meshmap-and-clonedmeshmap/52990/4 "2024-09-03T15:18:55Z")

</div>

yes a repro would be more than welcome if we have a leak
