Some Memory Management Issues and Questions

Hey guys, got a few questions about memory management I was hoping to get some clarification on.

First off, I’ve been doing some testing with the “DetectLeaks” script on the wiki and found that when I move between scenes, some assets that were used in the previous scene will still be loaded in memory. For example, when I transition from the main menu to a game level with LoadLevel, I find that Unity still has several texture atlases from the main menu loaded in memory, like MainMenuAtlas, OptionsAtlas, LevelSelectAtlas, etc. Shouldn’t these be unloading on the scene change? And more importantly, am I doing something wrong to cause this?

Second, I’ve heard that using Resources.Load may be a bad idea because it requires a constant memory presence. Would this still be a problem if I’m only loading in prefabs that reference other prefabs? For example, I have a prefab in Resources called “GlobalPrefabs” that only contains references to other prefabs in my project. Then, when I start the game, I have easy access to a list of prefabs that I can use for instantiation throughout the game. But doing this doesn’t load all the prefabs referenced by the Resources prefab into memory as well does it? That would mean the difference between my GlobalPrefabs object taking up a few kb and up to several mb.

Thanks!

Try:

If you want to be doubly sure that resources are removed you can use the Destroy() function.
But really unity will take care of that when you change scenes.
UnloadUnusedAssets only removes assets that are unreachable from the scene hierarchy and script components so if you’ve got your menu assets referenced some place then they probably wont get removed.

That detect leaks script must not be accurate, loading a new scene definitely clears all unused assets in my experience. The only reason those textures should be sticking around is if they’re actually present in both scenes. “present” means instantiated in the scene, or referenced by something in the scene.

Resources.Load is what I use in my projects, and I recommend it. The only “constant presence” they have is in your build size. If you have 7gigs of stuff in your resources folder, your build size will include all of that stuff even if your never instantiate any of it. For assets outside the resources folder, they’re only included in the build if you actually use them in your scenes somewhere. The contents of your resources folder only consume RAM if you actually instantiate them via Resources.Load though.