Version: Unity 6.2 (6000.2)
Language : English
Optimize the physics system for memory usage
Optimize raycasts and other physics queries

Optimize collision callbacks

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:

  1. Go to Edit > Project Settings to open the Project Settings window.
  2. Select the Physics > Settings tab.
  3. Select the GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
    See in Glossary
    tab.
  4. Make sure that Reuse Collision Callbacks is enabled.

To reduce GC allocations from these callbacks in script, set Physics.reuseCollisionCallbacks = true.

Additional resources

Optimize the physics system for memory usage
Optimize raycasts and other physics queries