Search Unity

How Much Do New Instances Affect Garbage Collection?

Discussion in 'Scripting' started by looytroop, Sep 28, 2018.

  1. looytroop

    looytroop

    Joined:
    Jun 22, 2017
    Posts:
    70
    Hello, I have a question, that I feel could vary depending the specific implementation, so I think I will ask this question in two pieces, one that is more general, and one that is more specific to me. Really I am looking to discover information about how creating new variable/struct/class instances affects Garbage Collection time.

    I will start with the more general question. I know that the amount of space needed to allocate a variable is very different depending on the variable/struct/class. I guess I would like to know more about how the GC works and how long it takes to free 8 bytes of memory for example. Is the GC time linear, or is there some function to describe it's performance? Please let me know any information that could be helpful.

    As for my more specific situation, I was looking to improve performance on a script in my game. This is the basic overview of the structure of what it WAS doing: Allocate 500 index Mesh Array and 500 index GameObject Array. On update, it would grab Meshes and GameObjects and populate the arrays if the arrays do not have that specific GameObject or Mesh yet, this array will then get cleared at the end of every update as well.

    The spot that is sub-optimal, is I am checking to see if the Mesh exists in the array. To fix this problematic area, I thought I would make it into a Dictionary for instant lookup times. Easy! I converted the Mesh Array into an Index, however, there is also the Game Object Array, I could create two dictionaries, one for the Mesh and one for the Game Objects, however, I thought it would be a lot cleaner if I held the both of them in a single dictionary.

    To get these into one value in a dictionary I created a simple ObjectInformation struct, it has the two attributes as well as the constructor. I have now formatted the dictionary's value to be this ObjectInformation struct. However, the problem in doing this is that when assigning the value, I have to create a new ObjectInformation() which means that the GC is going to have to come by and clean up it's mess that it is making every frame.

    The real question at this point is, how much should this affect the performance? Would it be better to just have two Dictionaries? Is there any other method that could be better than these two ideas?

    Thank you so much for reading through this post! Thank you to anyone who tries to be of any kind of help :)
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Garbage is allocated each time you're using new keyword on a reference type object.

    structs doesn't allocate any. Hence they're value types.

    There's a million and one examples on the net how to avoid GC in C#. Google heap allocation, reference vs value types.

    To get the idea on what does heap allocations in your code, I'd suggetst using something like Heap Allocation Viewer plugin (for Rider), but something similar for your IDE.

    Profiler comes handy as well, because it shows which method calls perform GC allocs.

    GC collection time hard to estimate, because it is completely different per platform. Few bytes won't be a trouble on any platform, where as ~mb would cause stutters almost on any.

    Here's a link to the basics you need to know:
    https://docs.unity3d.com/Manual/UnderstandingAutomaticMemoryManagement.html
     
    looytroop likes this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    In this post:
    https://forum.unity.com/threads/understanding-boxing-allocations-performance.541385/#post-3569965

    I share a very lengthy break down of the 'heap', 'garbage collection', and boxing. (a lot is in the spoiler tag since the topic was boxing... so I left the heap/gc stuff in spoiler since it was slightly off topic).

    If you could share this code, it would definitely help us in giving you better assistance.

    No its not. Structs only will create garbage if you box it (see my post in the other thread).

    If you still think garbage is being created... share your code, you might be boxing somewhere and not realize it.

    This question has way to many "buts" and "ifs" to it. Again, sharing your code would facilitate us in assisting you far better.
     
    looytroop likes this.