• Bugs
  • Possible Memory Leak when Copying Attachments in Unity

Hello there,

Apologies if this has already been posted on the forum before.
In short, I believe there is a memory leak when using attachment.GetRemappedClone().

Information about our project:

  • For our project we are making use of Spine to allow players to customize characters in Unity on both Android and iOS.
  • We are using a sprite sheet generated with Texture Packer to load sprites into attachments to allow for customizing each attachment by the player without the need for every possibility to be a skin setup in Spine.
  • We are on the latest 3.8 version of Spine for Unity.
  • The game kept freezing after a somewhat fixed amount of time regardless of mobile testing device

Upon further investigating with the Unity profiler it appeared that textures would go up every time we changed any attachment by using GetRemappedClone.

In the end we fixed this by calling Resources.UnloadUnusedAssets() and AtlasUtilities.ClearCache() which seems to fix the memory leak.

I am uncertain whether this is truly caused by GetRemappedClone() or if this is caused by us using a Sprite sheet not from Spine/Unity to remap.

Let me know if I can provide more information that might be useful

Greetings,

Steven

Related Discussions
...

Thanks for your detailled writeup, very much appreciated!

Actually this is intentional behaviour, you need to either call GetRepackedSkin() or GetRepackedAttachments() with the parameter bool clearCache set to true or call AtlasUtilities.ClearCache() to group the unload and trigger it when your gameplay permits the additional time spent.

We will add the respective code or comments at all Mix and Match example scenes (currently it's only included in the Mix and Match Skins scene).

Ah gotcha, expected something as much but since I did not notice this from the docs I assumed (wrongly) that this was a bug.
Atm I am not sure we can afford to repack on every attachment change as that makes the game drop down to 1-5 FPS on mobile, where as calling Resources.UnloadUnusedAssets and AtlasUtilities.ClearCache only drops to around 25 FPS.

We worked around it by adding a loading circle to improve the UX but as I am still planning to repack the skins at least once we know the player is done customizing, I will look further into using your suggestion also after attachments are changed.

Thank you for the quick response and glad it will be added to the other examples

One more potential improvement could be to use GetRemappedClone() with bool premultiplyAlpha parameter set to false, as this will then not create any Texture copy in the first place. You could do this either by:

  • a) creating the sprite sheet as PMA in the first place (so already premultiply it upon creation), or
  • b) by switching your used Material workflow at the customizable skeletons from PMA to Straight alpha (which might be too late unless you're at the beginning of your project).

I have just updated documentation and example script code to describe caching and cleanup in this commit, hopefully helping prevent some of the problems in the future. It will be released with the next unitypackages.

For option B we may be too far in indeed, but will give option A a shot as that sounds like a good optimization regardless.

Nice, thank you again 🙂

Also credit where credit is due, really appreciate the quick replies and really loving Spine, the customization feature we now implemented really opened my eyes to what one can do with Spine and I am very impressed, basically without it we would not have done such a feature

Great to hear, thanks very much for your kind words! 🙂

9日 後

Hello again,

I finally got around to trying out your suggestions.

a) creating the sprite sheet as PMA in the first place (so already premultiply it upon creation), or
b) by switching your used Material workflow at the customizable skeletons from PMA to Straight alpha (which might be too late unless you're at the beginning of your project).

I created the spritesheets as PMA and then set the bool flag on RemapClone for PMA to false, this leads for us to additional outlines showing, left side in the screenshot.

And also tried out option B, I set the bool flag for IsStraightAlpha on the material and we exported the corresponding sprite sheet without PMA enabled. Which lead to the artifacts showing on the right side of the screenshot.

As far as I understand it I followed everthing outline listed here: Premultiplied Alpha Guide

Do you have any suggestions how we may fix this?

Sorry to hear you are still facing troubles.

Regarding both artifacts: Are you sure that you have set the proper Texture import settings at your PMA sprite sheet Texture in the Inspector, and setup the corresponding Material Straight alpha settings? Please see especially the screenshots in section "Correct Texture Packer export and Texture and Material import settings" here:
spine-unity Runtime Documentation: Advanced Premultiplied vs Straight Alpha Import

The right artifact looks as if the texture was exported as a straight alpha texture, and rendered as straight alpha.
I'm not yet sure about the left artifact though. Could you please post screenshots of the respective Texture import settings and material settings?

There is an old posting by Pharan which shows some typical artifacts upon incorrect combinations:
Premultiplied Alpha Guide

As far as I am aware yes, will make the screenshots etc as soon as I am able, currently a bit short on time hence the late reply.
Will have a look through the docs as well, thank you