Massive (>1sec) lag spikes in game, within 10 seconds of starting
I have a simple spawn platform for the player, and when they want to start, they jump down, passing through a collider that disables/destroys the platform and itself. For some reason, whenever I start the game, within the next 10 seconds or so of starting, I will get a massive lag spike tat freezes my PC so badly even Ctrl+Alt+Delete does nothing until the PC decides to respond again. Sometimes this will happen 5 times in a row for a few minutes total, sometimes it will only be a few seconds. Also, Unity sometimes just randomly crashes with an 'Unrecoverable error' appearing. It appears to be a GUI script that is the issue, but I am not 100% sure
Here is the link to my editor log (renamed to txt)
Answer by Doctor_ED · May 18, 2019 at 11:45 AM
Your scripts are creating 270 KB of data for C# Garbage Collector. It may not seem like a lot, till you realise that this is the ammount created every frame. So after 10 seconds, at about 90 frames per second, it's 238 MB of garbage data. That is what most likely causes the issue.
Now bad news. To fix it you'll probably need to redesign your code a bit. It's hard to tell something without a code sample, but I can see that a lot of trash comes from LateUpdate(), so you may be creating and destroying lots of objects in that function. If that's the case, then you basicly have to implement Object Pooling.
Also to find out which script exacly you should blame for it, try disabling them and starting game one by one.
I think this was it. I disabled my only two OnGUI scripts (One to show keypresses, one for transform info) and the issue seems to have mostly dissapeared. Still get it, but far less often. Now when I look, there are fewer spikes, but they still show Apply material property
as using over 90%. Why is this?
I mean, without knowing what your code is doing, I can't tell where is the problem.