Search Issue Tracker
By Design
Votes
2
Found in
2020.3.44f1
2021.3.17f1
2022.1.24f1
2022.2.3f1
2023.1.0a26
Issue ID
UUM-24892
Regression
No
GC Alloc spikes when a collision between 2 GameObjects happens
Reproduction steps:
1. Open the attached “BugProject.zip” project
2. Open the “Assets/Scenes/SampleScene.unity” Scene
3. Open the Profiler window (Window > Analysis > Profiler)
4. Enter the Play Mode
5. Press “A” or “D” on the keyboard and pause once the blue ball hits one of the red balls
6. Observe the Profiler
Expected result: No Memory (GC Alloc) spikes are present
Actual result: Memory (GC Alloc) suddenly spikes
Reproducible with: 2020.3.44f1, 2021.3.17f1, 2022.1.24f1, 2022.2.3f1, 2023.1.0a26
Reproducible on: Windows 10 (21H2)
Note: The GC Alloc comes from either Physics2D.SendTriggerContactCallbacks() or Physics2D.ConvertCollision2DForScript()
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- Articulation Body with 'Revolute' Joint Type has erratic behavior when Upper Limit is set to above 360
- WebGL Player fails to render Scene when Terrain with Detail Mesh is added and WebGPU Graphics API is used
- Inconsistent errors are logged when different types are passed into the Query "Q<>" method in UIToolkit and the ancestor VisualElement is null
- Crash on GetMaterialPropertyByIndex when opening a specific Scene
- Discrepancies in the styling are present when using a TSS file instead of a USS file in custom EditorWindow
Resolution Note:
An allocation of 48 bytes during the OnTriggerEnter2D comes from Mono, not 2D physics. AFAIK this is the mono "wrapper" to the object being passed back and is only ever allocated once. You get this in all things passed back to the C# managed side the first time they are referenced in the Editor. It's totally automatic and nothing to do with physics as you'd get this for all sorts of other callbacks that pass back an engine object the first time.
In the OnCollisionEnter2D, the allocation of 304 bytes is the Collision2D object which we have to create. You'd also get this (different size) in OnCollisionEnter too for the Collision object. Because it's a class, it needs to be allocated on the heap. This is why there's been the following options (defaults to on) which reuses those objects for both 2D/3D physics:
https://docs.unity3d.com/ScriptReference/Physics2D-reuseCollisionCallbacks.html
https://docs.unity3d.com/ScriptReference/Physics-reuseCollisionCallbacks.html