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

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:

// 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];
        }
      }
    });
  }
});

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]

cc @sebavan

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 :slight_smile:

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