I have multiple games using Spine, and when I open a game, it loads the Spine materials as expected. However, when I return to the main scene, the materials are not being unloaded. Over time, as I open different games, new materials keep loading without releasing the previous ones, eventually causing a memory overflow.
Memory Leak Issue: Spine Materials Not Unloading Between Scenes
It seems like you are experiencing a memory leak issue where Spine materials are not being properly unloaded between scenes in your games. This is a common problem that can occur if the materials are not being explicitly released or if there are references preventing them from being garbage collected. To address this, you should ensure that all references to the materials are cleared when transitioning between scenes. This includes removing any event listeners, clearing any cached references, and ensuring that the materials are not being held by any static variables. Additionally, you might want to look into implementing an on-demand loading system for your materials, as suggested in some forum discussions, to manage memory usage more effectively.
@DaDava Please note that normal Unity rules apply regarding Material and Texture unloading. If you hold any indirect reference to a material or texture, it won't be unloaded. Note that the SkeletonDataAsset
holds a reference to the AtlasAsset
and the AtlasAsset
references the Materials
.
Apart from that, when repacking skins or attachments, there is a cache which can be cleared as shown in the Mix and Match
example scene scripts:
// `GetRepackedSkin()` and each call to `GetRemappedClone()` with parameter `premultiplyAlpha` set to `true`
// cache necessarily created Texture copies which can be cleared by calling AtlasUtilities.ClearCache().
// You can optionally clear the textures cache after multiple repack operations.
// Just be aware that while this cleanup frees up memory, it is also a costly operation
// and will likely cause a spike in the framerate.
AtlasUtilities.ClearCache();
Resources.UnloadUnusedAssets();
Hello,
Thank you for your answer. I tried your suggestion, but unfortunately, it didn’t work. I still see materials in the Memory Profiler.
Let me explain how my game flow works:
1.When I open the Main Menu, I can choose a game to play.
2.After selecting a game, the game scene loads, followed by the specific game (loaded via Addressables).
3.While in the game, I can return to the Main Menu, which I reload as a single scene and clear the atlas cache.
4.However, despite this, some Spine atlases remain loaded and are not being cleaned up.
Any insights on how to properly unload them would be greatly appreciated.
@DaDava Thanks for the additional info. Unfortunately if AtlasUtilities.ClearCache();
did not resolve your issue, it's likely that you are somewhere holding a reference to a SkeletonDataAsset
or the like which is indirectly referencing the Texture. Unfortunately we can't help with the screenshots alone, you would need to trace what is holding the reference to the shown Spine.AtlasPage
and the AtlasAsset
.
You would need to build a very minimal Unity project which still shows this issue, without any gameplay or menus, just simple script code.