Recreating the application without memory leaks


When the user quit the game to go back to the menu, I destroy the application using application.destroy. Then, when the user start again the game, I create a new Application.

I am using Vue.js so it’s done without page reloading and Playcanvas is keeping all the objects in memory even after I destroy the application (MeshInstance, GraphNode, etc…).

Is there a clean way to free the resources of an application or a scene ?

Did you try to iterate application root and unload() the entities/assets?

Application.destroy is already destroying the root and unloading the assets.

However, even if it unloads the assets, they are still there (application.assets._assets is not empty).

unloading an asset destroys it’s resource, it does not remove it from the asset registry. You must also call app.assets.remove(asset) for each asset in the registry.

(Though I think app.destroy should do this for you)

I tried calling app.assets.remove(asset) before app.destroy(), but I still have all the Mesh, MeshInstance and GraphNode retained by playcanvas in memory.

Here is how I load the assets and scene:

	(cb: () => any) => app.configure('config.json', cb),
	(cb: () => any) => app.preload(cb),
	(cb: () => any) => app.loadScene(`${levelId}.json`, cb),
	(cb: () => any) => app.loadSceneHierarchy(`Prefabs.json`, cb)
], callback);

For now, the only way I found to reduce the leak is to remove the mesh instances manually:

( as any)._callbacks = {};
( as any).lightmapper = null; = null as any;

And some MeshInstance are still retained by window.pc.Scene.defaultMaterial.meshInstances, window.pc.Scene.defaultMaterial._scene._layers, and window.pc.ModelHandler.DEFAULT_MATERIAL._scene._layers.