Minimizing string GC in Unity

An easy trap to fall into when you’re starting out in Unity is creating strings each frame in your update loop. Say you have score that can change from a bunch of things: eliminating foes, jumping on a block, collecting a ring, or just existing. Whatever floats your boat. Then in your score updater component you just do:

  void Update(){
    textComponent.text = "Score: " + score.ToString();

Easy and done. Problem is, you’re now creating a string (which are immutable in C# and allocate memory) each frame. This will really add up and create lots of garbage which will need collecting.

The Unity docs have a little bit on this subject if you search for “strings”. However it leaves much to the reader to figure out in their own sweet time.

There are a couple approaches I’ve thought of that each have some pros and cons.

Solution 1

Use images or some other render method instead of text. For example if you have a countdown timer that only uses a single digit you can make images for 0-9 and then swap them out based on the timer value.

Pros: Doesn’t need to allocate memory at all.

Cons: Tedious to implement, only works well for fixed length and known strings.

Solution 2

Use events to trigger a function to update the text when the value changes. For example if you have an OnScoreChange event that you wire up to an UpdateScoreText function.

Pros: Only update the text (and allocate memory) when the value changes

Cons: Cumbersome to wire up all these events and handlers for every piece of text that has this pattern.

Solution 3

Manually keep track of the previous value and only change the text when it changes.

  int previousScore = -1;

  void Update(){
    if(score != previousScore){
      previousScore = score;
      tmp.text = "Score: " + score.ToString();

Pros: Only update the text when the value changes

Cons: Annoying amount of boilerplate for each string to update, even though it’s less than the event binding solution.

Solution 4 - Preferred

Create a class to keep track of all the previous values and only update the strings when the value changes. In the example below there is a ScoreChanger component that arbitrarily increases a score and then uses this method to only update the string when the score changes.

Pros: Only update the text when the value changes, and a single point to store previous values

Cons: Overloads for each type of data you want to track have to be created to avoid allocations from boxing that would happen if you stored the values as objects.

This way is great for most of my use cases and since I primarily have int and float values it’s not too clunky to create the overloads. Let me know in the comments if you think of any other ways to tackle this problem or have suggestions for improvement!

Liked this post?

Sign up and be the first to see new posts!

No spam, no nonsense, no guarantees, no rights reserved, probably.


comments powered by Disqus