For years I have been under specific impression on how assets reference and management occurs between scenes. Our current project - triggering out of memories constantly on a very limited platform - appears to be proving me I was dead wrong.
When loading the main menu, we have a bunch of managers loading bunches of definition; localization, fonts, UI, etc. At that moment, we hover around 650Mb. Looking up the loaded asset, it’s all as expected.
When loading the first gameplay scene, the memory climbs up to 2.4Gb. Also as expected. We have large scene, lot of enemies, lightings. It’s a full fledged third person RPG.
When quitting the game and returning to the main menu, we load an empty scene, call Resources.UnloadUnusedAsset and… 1.1Gb. What? For a while I thought it was maybe just Unity shuffling around memory, keeping it to itself to re-allocated it faster. Except that memory would keep climbing between different scenes. Something was sticking around.
Until I pulled the Memory Profiler:
A ScriptableObject with 0 Ref Count. This ScritableObject was referenced from the unloaded scene; it contains the definition of an enemy, a reference to the prefab, the animation set, the different weapons it uses and it’s stats. In the scene, a spawner script uses that definition to build up a character.
I have pretty much 550Mb of characters data - mesh, animation, textures - floating around memory, with a ScriptableObject referencing it, but nothing referencing the ScriptableObject. And Unity is clearly not disposing it.
Why is Unity keeping those around? How do I prevent that from occurring? I always read that SO were to be handled like any other asset. But clearly that’s now what’s happening here. If nothing references a texture, the texture is unloaded. Do I have to crawl around object types with some nasty FindAllObjectsOfType and do something with them? If so, what exactly?
Resources.UnloadAsset doesn’t appear to be doing anything.
Resources.UnloadUnusedAssets clearly doesn’t catch those.
Destroy sounds dangerous, especially while playing in the editor, as those are “disk asset”, loaded by a scene reference.

