- Home /
Loading Scene causes lag spikes due to garbage collecting
I am on my videogame screen and when I click to go back on the menu this happens. It happens only if I go back to the Main Menu too quickly. Maybe I am not giving enough time to the level to load and everything gets dumped when I go back to the Main Menu? Sometimes I get even 5 seconds of frozen screen, which is worrying. It has to do with the Garbage Collector, if I load the MainMenu in addictive mode this doesn't happen as garbage doesn't need to be collected. I really don't know what to think.
Create less garbage, look into pooling and different things for this.
Answer by rh_galaxy · May 27, 2021 at 04:07 AM
In Unity 2019.2 or later you can use incremental garbage collection. It spreads out the task over many frames instead of doing it all the same frame... But when you do scene transitions, it may be impossible to have it load a new scene without some spikes in the profiler. When framerate is very important to maintain (like in a VR game), it is safest to fade to a single color during scene changes.