Reduce garbage collector (GC) load and improve performance by optimizing physics collisionA collision occurs when the physics engine detects that the colliders of two GameObjects make contact or overlap, when at least one has a Rigidbody component and is in motion. More info
See in Glossary callback allocations.
Unity provides physics collision callback messages like:
By default, each time these callbacks are invoked, Unity allocates new Collision
(or Collider
for triggers) data objects in memory to hold information about the collision. These frequent small allocations can increase the load on the garbage collector.
To reduce the number of allocations from these callbacks, you can enable the Reuse Collision Callbacks setting, to make Unity reuse a smaller pool of Collision
data instances. This reduces memory overhead and improves performance, especially in scenarios with many collision events per frame. Note that if you reuse collision callbacks, the Collision
data passed to the callbacks is then only valid during that specific callback.
To learn more about garbage collection, refer to Garbage collector overview.
To make sure that you are reusing collision callbacks and reducing GC allocations from these callbacks in the Editor:
To reduce GC allocations from these callbacks in script, set Physics.reuseCollisionCallbacks = true
.