Optimize collision callbacks
Reduce memory allocations caused by frequent collision events.
Read time 1 minuteLast updated 14 days ago
Reduce garbage collector (GC) load and improve performance by optimizing physics collision callback allocations.
Unity provides physics collision callback messages like:
By default, each time these callbacks are invoked, Unity allocates new (or for triggers) data objects in memory to hold information about the collision. These frequent small allocations can increase the load on the garbage collector.
CollisionColliderTo reduce the number of allocations from these callbacks, you can enable the Reuse Collision Callbacks setting, to make Unity reuse a smaller pool of 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 data passed to the callbacks is then only valid during that specific callback.
CollisionCollisionTo learn more about garbage collection, refer to Garbage collector overview.
By default, Reuse Collision Callbacks is enabled in the Project Settings. To make sure that you are reusing collision callbacks and reducing GC allocations from these callbacks in the Editor:
- Go to Edit > Project Settings to open the Project Settings window.
- Select the Physics > Settings tab.
- Select the tab.GameObject
?
- Make sure that Reuse Collision Callbacks is enabled.
To reduce GC allocations from these callbacks in script, set .
Physics.reuseCollisionCallbacks = true