Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Collisions and garbage

Discussion in 'Physics' started by Robdon, Jun 13, 2018.

  1. Robdon

    Robdon

    Joined:
    Jan 10, 2014
    Posts:
    141
    Haven't looked at this for a while, but I believe that now collisions can be detected without garbage?

    I knew there was a problem with garbage if you wanted Contacts, and there is now a new command GetContracts() that you can pass a predefined array, and that all looks good.

    But, if I just have a simple OnCollisionEnter2D() and do a GetContacts in there, it still seems to generate garbage, specifically in the profiler as ConvertCollision2DForScript() and SendCollisionContractCallbacks().

    Am I supposed to used GetContacts in OnCollisionEnter2D or is there something I have to do to stop the 'old' garbage being allocated?

    I've also noted that even if the OnCollisionEnter2D() routine has no code in it, it still generates garbage on collisions. (I even tried to removed the Collision2D parameter, and it still did it)

    I've also tried doing my GetContacts in FixedUpdate() as a test, and this seemed to remove all garbage, but now obviously its doing the check every physics tick rather than a callback.

    Is that the correct way to use GetContacts now, in FixedUpdate to get zero garbage or is there something else?

    Using 2018.1.0f2
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    Did you profile a build or the editor? If you profiled the game running in the editor, I recommend to profile a build on the target hardware instead.

    The memory allocation and performance characteristics between a build and editor are often significant, like shown here. Many GCAlloc's that occur in the editor are completely gone in a build.
     
  3. Robdon

    Robdon

    Joined:
    Jan 10, 2014
    Posts:
    141
    Thanks. I profile in the build never in the editor as I know it gives completely different results in the editor.
     
    Peter77 likes this.
  4. samifruit514

    samifruit514

    Joined:
    Mar 27, 2015
    Posts:
    30
    Hello Robdon, do you still have GCAllocs on your targeted hardware? I still have a lot of GC.Alloc even if I use GetContacts inside oncollisionenter2d:
    upload_2018-9-2_8-1-45.png
     
  5. Robdon

    Robdon

    Joined:
    Jan 10, 2014
    Posts:
    141
    Yes, still getting them. I'm not sure if it's been fixed so that Unity shouldn't allocate for collisions, or if it hasn't been changed or fixed yet. I couldn't find any info about it, and no on 'official' has replied.
     
  6. samifruit514

    samifruit514

    Joined:
    Mar 27, 2015
    Posts:
    30
    I just discovered that we can define void OnCollisionXXX() without Collision2D argument (which is not documented anywhere!!). I was expecting that it would not alloc mem for Collision2D, but it seems that it does still alloc. Maybe you would be luckier if you try
     
  7. Robdon

    Robdon

    Joined:
    Jan 10, 2014
    Posts:
    141
    Yeah, I knew about that and had tried it before. It didn't help.
     
  8. DrDrimixar

    DrDrimixar

    Joined:
    Aug 13, 2014
    Posts:
    5
    Bump, I have the same problem with it even on Unity 2018.2.14f1.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,315
  10. Robdon

    Robdon

    Joined:
    Jan 10, 2014
    Posts:
    141
    @MelvMay Just downloaded b11, flicked the switch and it seems to be garbage free! Thanks :)

    Need to do more testing obviously, but it looks good on first looks.
     
    MelvMay likes this.
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,315
    That's good to hear, thanks for letting me know.
     
    Robdon likes this.