Garbage Collection (StackTraceUtility) - Profiler Spikes

Hello everyone.

Whenever I profile my game I keep getting Garbage spikes. I checked and the most Garbage comes from something called ‘StackTraceUtility’. And I also get huge random spikes from ‘Others’. Is it also part of Garbage Collection?

I checked about this on the internet, but haven’t found much useful information. Can someone please give some tips on how can I fix / avoid this annoying issue? Thank you!

Profiler:


After the Deep Profile:

I’m sure you know what a stack trace is, so you might have heard that it’s expensive to gather all the information for the stack trace from the current call stack and so on. That’s also what Debug.Log does, thus the huge performance impacts of Debug.Logs when used too often in one frame.

My assumption is that you get some frequent messages on the console which is (for whatever reason) caused by the renderer, which internally calls LogStringToConsole at some point.

The name itself suggests it’s probably building some strings using the stack trace utility, which - like mentioned earlier - suggests that it also gathers alot of information and provides it to the calling LogStringToConsole. That one might build a string with lots of information in it. Depending of how the information is cached and concatenated, there’s potentially an average to huge amount of gargabe that could be produced - not only from strings, but all the informational objects that are probably no longer used once this call is done.

1 Like

Thank you for the answer! :slight_smile:

So as far as I understand, the problem is inside the script? I haven’t called any Debug.Log. The console is empty. So, I have no idea how to track the source of this issue…
Could you maybe show some examples on what would cause this problem?

Again thank you for the response and sorry for these stupid questions I’m asking… I’m still learning…

Could also be logging to the Editor log: Profiler showing LogStringToConsole, what is that?

Might want to try turning on full logging in Player Settings to see if anything else comes up in the console.

2 Likes

Can’t show an example, but the links posted by @KelsoMRK provide some useful information to get started.

Something appears to log some significant amount of information including stack trace information to some logging destination, if it’s not the console, it’ll probably go somewhere else - like suggested by Kelso, log files.

Perhaps its one of the reasons mentioned in the linked threads, but it could also be just another tiny detail that’s causing these.
Before you turn off anything, try to figure out what’s being logged. Even if these are just some non-error, non-warning informational messages, it can still be useful enough to go ahead and fix/improve/rethink some implementation details or settings.

2 Likes

This problem is caused because unity is logging messages to the console every frame and because the message is a string type which is an immutable type, everyframe memory is getting allocated and is then garbage collected when GC cycle run. It should not be a problem when u build the game as at that time no message will be logged to the console. Anyway if u want to get rid of this for now u can click on menu icon on very top left of console window and select stack trace logging and then in error select none, because this can be also happening because of warnings, or logs etc so select none in them too. But in majority of cases this is because of error and warnings.

2 Likes

Many thanks for all these gentlemen, I spent a couple of days to figure it out, I fixed all of the errors but my VR games still lagging. I finally found this thread and figured out that there is thousand warning about physic detection, and after I turned the warning logs off, the games much more stable!