Our game loads 518 AnimationClips during gameplay, taking up around 85MB RAM (or double that on iPad 4 for some reason). But those clips are never freed from memory - load a new scene with one single GameObject in it with no animations, that scene still has 518 AnimationClips. It’s causing a real problem, as the game is running out of memory going from a game level to a menu scene (the menu scene has no AnimationClips in it).
We’ve tried calling Resources.UnloadUnusedAssets, followed by a System.GC.Collect, no dice. Finding them all with FindObjectsOfTypeAll and setting each one to null, nope. Calling Destroy on them all Unity just gives the error message that it doesn’t allow destroying resources. Nothing we’ve tried works. Any ideas?
Calling FindObjectsOfTypeAll to find them all after loading a new (empty) scene and calling Resources.Unload on each one did the trick, but that seems like a hack, not the right way to go about things. Note, we’re having the same exact issue with Textures (a whopping 1,607 textures are still in memory after loading a new, empty scene!) and Shaders (122 of them). UnloadUnusedAssets doesn’t work, so somewhere Unity must think they are being used somehow.
Are you sure you don’t have any object that is kept between scenes that holds these animations loaded in memory ?
You can use the profiler to see what objects are currently loaded in memory. In the profiler “Memory” timeline, select “detailed”:
Then during gameplay, you can hit the “Take Sample” button to capture a snapshot of the objects currently in memory. Look up those animation assets to see if they are indeed listed there.
I can look in the hierarchy and see there are only 3 things in the scene, and I’ve been using the profiler to see that the animations are taking all the memory, that’s how I first learned they were staying in memory, until I manually looped through and Resources.UnloadAsset each one.
Unfortunately, there’s more things that are staying in memory, textures and shaders, but I’m not sure which ones; doing the call to get the full list of textures and shaders in memory gets all the ones used in the Unity Editor as well, and no way to separate those from ones from my game. I guess I could do a check for not running in the editor and only then free up all the textures.
There are also hidden objects - ones that will not show up in the hierarchy, but are still there.
I don’t believe just the hierarchy anymore i look at the profiler, where it tells me what objects are referencing each asset (animation, texture, etc).
Not the animation clips, it just seems that Unity 4 never automatically removes those on scene load. Since they are from the legacy animation system, they probably just stopped looking for bugs in it, and unless you have a whole ton of them like we do most people wouldn’t notice. Note that these are not actual memory leaks, because if you reload the scene that created them, it will re-use them. Our problem was the menu that didn’t use animation clips was getting memory crashes.
I did discover something else causing some things to secretly stay in memory: setting HideFlags.DontSave. This keeps an object from showing up in the hierarchy, but also makes it so the object will not be automatically cleared from memory either. We had a RenderTexture being set to that for some reason, and also nGui (which we use) uses it a lot, and the version of nGui we have isn’t always cleaning up after itself (we have an older version since we’ve been working on our game for a few years).
Have you tried to profile a build? As you can see in your screenshot, “Referenced By” shows a lot of UnityEditor related stuff, which doesn’t exist in a build.
The following video explains in detail why profiling in the editor is normally not that useful or causing you to chase ghosts.
Unite Seoul 2018 - When optimization goes wrong
If the issue persists, updating Unity might be worth a shot too. According to the public issue tracker, they had quite some memory leaks in the Animator.