Search Unity

GC.Collect lag spikes

Discussion in 'Scripting' started by serbusfish, Sep 13, 2018.

  1. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    223
    When testing out one of my scenes every now and again I experience numerous large but brief fps drops, and when I check in the profiler what causes the spike it says 'GC.Collect'. I have no idea what this is, I have never used this in my code and this is the first time I have come across the term. The only thing I can say is that I destroy a large collection of game objects when the first lag spike hits so I suppose this is somehow connected to GC.Collect? If so can someone explain what is happening and how I can get around this problem?
     
  2. GeorgeCH

    GeorgeCH

    Joined:
    Oct 5, 2016
    Posts:
    222
    Unity uses Garbage Collection (GC) to periodically clear out, well, garbage generated by your code at runtime. The faster the garbage accumulates, the more frequency the garbage collector will run, and the more work it will have to do.

    You usually cannot eliminate it, but you can reduce it. Read this for more information. Generally speaking, use the Deep Profiler to figure out where garbage is coming from and work to reduce it. Additionally, you can call GC.Collect() yourself at appropriate moments - for example, when the user is in a menu, they probably wouldn't care about an FPS drop.
     
    ihgyug and Joe-Censored like this.
  3. Quingu

    Quingu

    Joined:
    Jan 30, 2013
    Posts:
    636
    Learn about memory management in Unity/Mono. It's a delicate thing, requiring skilful handling.
     
  4. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    223
    Thanks this is very helpful, I have a lot of functions in update that aren't cached so it's possible this is the cause of my problems, I will read up on this more and try to cache my functions.
     
  5. GeorgeCH

    GeorgeCH

    Joined:
    Oct 5, 2016
    Posts:
    222
    Generally speaking, you should never, ever make GetComponent calls every frame.
     
    ihgyug likes this.
  6. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    223
    I dont understand what my scripts could be generating that would be considered garbage. My game is a fairly simple side scrolling shooter, there isn't that much going on that could be considered advanced or overally complex.

    I tested out using the profiler with a development build and I was getting constant spikes related to 'behaviour update', but none of the scripts within show huge performance usage:

     
  7. GeorgeCH

    GeorgeCH

    Joined:
    Oct 5, 2016
    Posts:
    222
    Break it down using Deep Profiling and you'll find out.
     
  8. WhendricSo

    WhendricSo

    Joined:
    Jan 1, 2011
    Posts:
    168
    Make sure to cache instances in your classes, don't destroy (or create!) things unless absolutely necessary. Especially object references! If you're developing for mobile, it's best to implement object pooling so that you can spawn & destroy stuff without invoking the garbage collector.