I’m wondering the best approach to reducing the time spent in this method. I’ve managed to reduce it somewhat by destroying objects in the current scene before activating the one I’ve async loaded, which reduces time spend in UnloadScene. But I’m still seeing three big hits that I’m not entirely sure how to address:
GC.Collect is called and eats up the most amount of time with 8% of frame time, but not sure if this is being called explicitly by the this method or if all of the memory allocation here just happens to consistently trigger the garbage collector.
UnloadScene > CollectGameObjects > FindObjectsOfType is taking 5% of frame time.
FindObjectsOfType, at the same level as UnloadScene is taking 5% of frame time.
Loading.AwakeFromLoad, which is where I’d expect the majority of time to go, is only 2% in my test.
What Type of objects is FindObjectsOfType trying to find here? And does that method inspect all active objects in the C# runtime?
GC.Collect is the garbage collection running. Allocating large amounts of memory can cause that, you should check what allocates memory (during your scene loads).
FindObjectsOfType is a Unity method that only looks up Unity based objects (things that derive from UnityEngine.Object), so it does not inspect all active runtime objects (in the C# runtime).
Can’t the new incremental GC in Unity take care of this - just be leaving the memory to the GC…
From the game log:
“Unloading 144 unused Assets to reduce memory usage. Loaded Objects now: 2246.
Total: 82.161980 ms (FindLiveObjects: 0.188295 ms CreateObjectMapping: 0.040623 ms MarkObjects: 81.442011 ms DeleteObjects: 0.491050 ms)”