- Home /
Unload Sprite Atlas from memory
Hello! We are loading Sprite Atlases at runtime in our game using Resources.Load()
. But when trying to eliminate all references to unload the atlas and therefore all the rextures from memory, there is always one reference left according to the profiler, which is the "SpriteAtlasDatabase".
So, how do you unload a sprite atlas completely and correctly?
I am also the same problem. The reference count by SpriteAtlasDatabase is protected by Resources.UnloadUnusedAssets. Is this behavior only in the editor or is it a bug?
I am pretty sure that it was behaving like this when building to an iPad as well. I can not say if it is a bug or not, but I will try to reproduce this with a completely new project and see if it occurs there as well. To be continued :)
Answer by DarkDayZ · Mar 09, 2018 at 10:18 AM
In case someone happens to have weird memory occupation caused by sprite atlases: Delete your lib files. That fixed it for us. The lib files contain a huge amount of cached atlas files (ca. 11GB in our case). Cleaning that up got rid of the weird behaviour.
Answer by arthuronoszko · Aug 08, 2018 at 07:52 AM
Hi! I have a similar problem, where I load the new scene, where the Atlas is not referenced in any way, but according to the Profiler, it still has a lot of references to it.
This is the code for when I am changing scenes:
IEnumerator Transition(string fromScene, string toScene) {
yield return SceneManager.UnloadSceneAsync(fromScene);
yield return StartCoroutine(LoadSceneAndSetActive(toScene));
yield return Resources.UnloadUnusedAssets();
}
IEnumerator LoadSceneAndSetActive(string sceneName) {
yield return SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
var newlyLoadedScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
SceneManager.SetActiveScene(newlyLoadedScene);
}
Am I unloading the assets correctly? If I get rid of the SpriteAtlases, then I do not have any references to any of the sprites in the previous scene, so my conclusions is that is has to do something with SpriteAtlas.
Anyone got the same problem as I? Or am I profiling wrong? I have also tried to profile with the MemoryProfiler (https://bitbucket.org/Unity-Technologies/memoryprofiler) where when I select the atlas it says: "No root is keeping this object alive. It will be collected next UnloadUnusedAssets() or scene load"
I think the bitbucket memory Profiler didn't take Hide flags into account, like the Editor only flag: HideAndDontSave.
These might keep these around in memory in the Editor for Editor purposes. Be sure to make a Development builds and profile that. Also try out the Memory Profiler Package that shows the HideFlags in the Advanced foldout of the Selected Item Details section.
Answer by JohnstableHome · Oct 17, 2018 at 05:21 AM
If you use the SpriteAtlasManager callbacks, you can load and unload the Atlases yourself, instead of going through the database.
See example in https://github.com/jconstable/SpriteSleeper
Answer by MartinTilo · Apr 23 at 01:58 PM
The Memory Profiler Module doesn't show you the full reference chain from your C# scripts and it gives cryptic remarks if something is kept around because of Hide flags, like the Editor only flag: HideAndDontSave.
These Flags might keep these sprite atlases around in memory in the Editor for Editor purposes (like generating, regenerating or displaying their preview). Be sure to make a Development builds and profile that. Also try out the Memory Profiler Package that shows the managed references to these Atlases. It also shows the HideFlags in the Advanced foldout of the Selected Item Details section.
Your answer

Follow this Question
Related Questions
Unity iOS memory usage 0 Answers
2D Animation does not start 1 Answer
How to handle huge images in unity? 0 Answers