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()

  1. 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

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.