Recreating the application without memory leaks

Hi!

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:

series([
	(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:

(this.app.assets as any)._callbacks = {};
(this.app as any).lightmapper = null;
this.app.batcher = 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.