Can I prevent this memory from leaking?

Hello guys.
I’ve been working on a HUD element for my game and noticed that accessing the ‘fillAmount’ field of an image causes 20 Bytes of memory to be allocated every time I change it.

I wonder if there is something that can be done about this. It this simply a bug/an oversight or is this inevitable due to how the UI system works internally? I mean, I cannot see what happens behind the scenes, but generating garbage every time a float is being changed is a bit of a problem since I use that particular feature on multiple GameObjects simultaneously.

Again, there might be a good explanation for this, but I cannot know it.

The source for Unity UI is viewable. You can dig around the implementation here. : https://bitbucket.org/Unity-Technologies/ui/src/ccb946ecc23815d1a7099aee0ed77b0cde7ff278/UnityEngine.UI/UI/Core/Image.cs?at=5.1#Image.cs-92

I suspect the allocation may be from it generating a new vertex array after it’s marked dirty by you setting a new fill amount.

1 Like