May 26 2020
In this post, you will show you how to use the experimental Unity Memory Profiler package to find exactly where you are wasting memory on your game.
Here’s what we will cover in part 1:
The Unity Memory Profiler is an experimental package that will let you find where exactly you’re spending the memory of your game.
In other words, it will let you:
You can use this tool in a variety of ways:
Let’s see how you can start using this tool right away.
As with every package, the installation is straight forward.
Open the package manager (window → package manager) and enable the experimental packages in the advanced section.
* Note: on Unity 2020.2 and above, you enable the experimental packages in a different place. Find this option in project settings → package manager. I guess this is Unity’s way to make it harder to find these packages, so people stop complaining about their instability (too much PR noise).
Once you installed this package, open the Memory Profiler (Window → Analysis).
In the Unity Memory Profiler window, select the target player running the project you want to analyze, e.g. editor, android, SNES, Gameboy, etc..
And then, you know what to do. Hit the capture button.
Note: avoid capturing on the editor, as it introduces tons of noise. In my case, Unity wouldn’t capture a player snapshot so I had to go for the editor (did I mention it might be unstable?).
So you clicked on the capture button and went to grab a coffee.
After a few seconds (or minutes (or hours (or never))), you’ll be able to open the snapshot you just captured.
Unity will show you by default the tree map of your game’s memory layout at the time of the capture.
Each segment displays a single asset type plus its size in RAM, as you can see below.
Now, here’s the cool thing: you can click on the that segment and it’ll instantly flip to show the specific assets that are stealing your memory.
Here, take a closer look at the Texture2D section that I just opened:
You see? This is a very straight-forward way of seeing how well (or badly) you’re distributing the memory of your game.
In my case, it seems like there’s something weird with one of my textures: PlasticRidges_Albedo_MemoryGreedy.
It’s somehow taking 85MB…
I wonder… what’s going on here?
I think I’ve seen this asset before. Let me double check…
So I navigate to that asset and instantly find what’s wrong.
REALLY?
Whooops, Ruben sloppily “forgot” to turn on the texture compression for this blog post.
Easy fix.
Let me recap the process:
By following this process, you will be able to spot memory optimization opportunities.
Here are other thingies you can find: excessive shader variants, scripts with huge heap data, oversized render textures, memory leaks… the list goes on.
In part 2 of the Unity Memory Profiler series, I’ll go in more detail about the different visualization modes that we didn’t cover today:
Why not today?
Because they are advanced and I think you and I should start it with a fresh mind.
Honestly, repeating “get my free Unity Performance Checklist” is getting a bit old.
Oops, I just said it again.
Anyway, just for today I’ll make an exception and say…
Go play with the Unity Memory Profiler and have some fun.
You’ll be surprised to see what it reveals of your game 😉
~Ruben