Clearing the scene and engine - is this overkill?

If I want to clear EVERYTHING to free up all the memory consumed by BabylonJS (forgetting about Chrome’s poor memory management at least…), is it enough to simply call:

engine = null;

Or should I try to manually dispose and set all lights/cameras/materials/meshes to null?

Right now I am using this code, and wondering if it is overkill:

for (let i = 0; i < this._scene.cameras.length; i++) {
	if (this._scene.cameras[i] && !this._scene.cameras[i].isDisposed()) {
		this._scene.cameras[i].dispose(false, true);
		this._scene.cameras[i] = null;
for (let i = 0; i < this._scene.lights.length; i++) {
	if (this._scene.lights[i] && !this._scene.lights[i].isDisposed()) {
		this._scene.lights[i].dispose(false, true);
		this._scene.lights[i] = null;
for (let i = 0; i < this._scene.materials.length; i++) {
	if (this._scene.materials[i]) {
		this._scene.materials[i] = null;
for (let i = 0; i < this._scene.meshes.length; i++) {
	if (this._scene.meshes[i] && !this._scene.meshes[i].isDisposed()) {
		this._scene.meshes[i].dispose(false, true);
		this._scene.meshes[i] = null;
for (let i = 0; i < this._scene.spriteManagers.length; i++) {
	if (this._scene.spriteManagers[i]) {
		this._scene.spriteManagers[i] = null;
for (let i = 0; i < this._scene.textures.length; i++) {
	if (this._scene.textures[i]) {
		this._scene.textures[i] = null;

this._scene = null;

this._engine = null;

its overkill i guess to dispose every engine and scene
i think Deltakosh knows this stuff better, why do u even
need to dispose everything?

And btw you can just call engine.dispose() to achieve the same goal

To clear the scene, there is scene.dispose(), to clear the engine there is engine.dispose().

From my experience, there was an odd memory leak happening when I was using scene.dispose(), while engine.dispose() worked well at cleaning everything in memory. I could not really investigate further what was wrong in my code, but here was the thread I started at the time:

My concern as a game dev is that when I want to reload a level, or load a new level of my game, I feel that I should be using scene.dispose() but I couldn’t, because of the memory leak I was getting.

I to needed to dispose of the scene, and not the engine. The game I’m working on is an MMO, so I need to be able to clean up all the resources whenever the player logs out. While I do eventually call scene.dispose(), I also end up cleaning up all objects that are in use. Each class has a ‘destroy’ function that gets called when the object is no longer needed. If the object contains any BabylonJS objects, it’ll call dispose() on those too. So when logging out, I call destroy() on all the objects, which will dispose of any BabylonJS objects and set their reference to null, and then I call scene.dispose() for good measure. Again it may be overkill, but I think it doesn’t really hurt. And so far I haven’t experienced any sort of memory related issues (knock on wood).

But I believe the scene object contains reference to all materials/meshes/etc. that have been added to the scene, so I suppose you can try to loop through all of those and dispose each individually (essentially manually disposing the scene), and see if there is still a memory leak.


you may be interested to use asset containers for your levels:

Also if you can repro the leak in the playground I will definitely fix it!