unity gc allocation spikes every few seconds in mobile build
Unity 2021.3.29f1, building for Android. Profiler shows a 4ms GC spike about every 3 seconds. Deep Profile points at a script that does string concatenation in Update to build an FPS counter: text = "FPS: " + (1/Time.deltaTime).ToString("F0") + " ms: " + Time.deltaTime.ToString("F2"); That runs every frame and allocates strings, so garbage collects. I know I should use StringBuilder but it still allocates the final string. What's the standard fix here - update the text every 0.5s and reuse a StringBuilder? Also the FPS math itself is wrong, 1/deltaTime gives an instantaneous value that jumps around, but that's a separate issue.
4 replies