What is the correct way to unload a scene from memory?

When a scene is loaded using loadSceneHeirachy, what is the correct way to unload the scene and free up all assets for GC? We currently have the following…

assetlistloader.load() // preloads a set of assets
loadSceneHeirachy() // runs the scene
// some time later
// root.destroy() // removes entities but not assets
// what next?

in this situation, I believe the assets are internally cached, so they remain in memory. The assetlist loader has a destroy method, but I that just stops any further loading and doesn’t seem to touch the assets themselves. unloadSceneData seems to invalidate the data property of the scene registry item but I’m unsure if that also clears the cache. Also if the Assetlist loader maintains reference to the assets then they’d never get GC

Hi @Mark_Lundin ,

You can use the unload method on each asset you want to unload from the memory:
Capture

https://developer.playcanvas.com/en/api/pc.Asset.html

What I usually do is tag assets according to the scene in which they are used, eg if I have 2 levels, I would tag level 1 assets “level_1” and before loading level 2 I would find these assets by tag and unload them and will wait for the “unload” event to be fired for all the assets, then load the next scene using loadSceneHierarchy and also its settings using loadSceneSettings. You might also want to destroy textures created in any scripts as well.

1 Like