SpriteShapeController GarbageCollection

Hi,

We are creating a mobile 2D endless runner and make heavy use of sprite Shapes.
Unfortunately they create a HUGE amount of Garbage collection.
We enabled “Cache Geometry” since we only need to create the shapes at editor time but there is
still a lot of GC being generated. Anyone know how to reduce this?

We use:
Unity 2021.2.6
SpriteShape 7.0.3

Any hints in the right direction would be greatly appreciated.

Maybe someone also figured out how to correclty use “SpriteShapeController.BakeMesh()”. We were not able to use it since it always throws Memory leak exceptions. We would like to blast through all Prefabs which contain lots and lots of Sprite Shapes and then bake the meshes rather then doing it all manualy.

Not my area but sounds like you should perhaps read this .

@Cybertiger_1

  1. If you are modifying/animating SpriteShape at Runtime (with no overlapping control points) then please enable Fill Tessellation C# Job in SpriteShapeController. This prevents any GC allocations for the SpriteShape Geometry Generator and make Geometry Generation faster.

  2. If you are using Static shapes, please enable Cache Geometry. This not only prevents GC but also removes the need for generating geometry during Runtime.

The 1.2 kb GC noticed in the screenshot is basically GC due to GetComponent API and Editor Only DisposeSentinel handles. Note: GC allocations due to GetComponent have been completely removed and will be published in an upcoming version. DisposeSentinel is Editor only GC allocations and does not happen on Player. Will post an update as soon as possible. Thanks.

Thanks for your reply @Venkify ,

In our game we have a ton of prefabs that have hundreds of SpriteShape controllers in time. We don’t need any runtime modification whatsoever. Would there be any way for us to “Cache Geometry” from Editor Code and also set “Fill Tessellation” Unfortunately those fields are not exposed.

SpriteShapeController.BakeMesh()

Is supposed to do “Cache Geometry” from what i understand, but it throws Memory leak errors when i iterated over all the sprite shapes. I am aware that it returns a JobHandle and then calling:
JobHandle.Complete()
Throws the same error.

Many Thanks
Rainer

Agreed. We will provide API to access Fill Tessellation in the upcoming version.

BakeMesh().Complete() is the right API to CacheGeometry for SpriteShpes. However if you want to enforce generation of geometry and caching it, please use the following sequence.

Please use the following code :
SpriteShapeController.RefreshSpriteShape();
SpriteShapeController.BakeMesh().Complete();
SpriteShapeController.BakeCollider();

You can also take a look at BakeMeshForced function in SpriteShapeController which also ensures that the generated geometry is used to render once. However its an internal function, but can be easily copied to and run from a custom MonoBehavior or Editor script too.

Please let us know if this helps. Thanks for reporting

Could you kindly provide a reproducible case/scene/project and file a bug report ? We are aware of GC allocations when Fill Tessellation in C# job is not active, however we do not have any known issues of memory leaks. Thanks.
.

1 Like