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:
- Is it safe to directly access and modify the
_geometriesByUniqueId
property of the scene? - Are there any potential issues with the following code that aims to clean up
_geometriesByUniqueId
,material.meshMap
, andmaterial.clonedMeshMap
? - Is this an effective way to prevent memory growth, or are there better approaches?
- 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
:
- Is my approach to cleaning up
clonedMeshMap
correct? - Are there any risks in removing entries from
clonedMeshMap
? - Should I be concerned about potential side effects when modifying
clonedMeshMap
? - 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]