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:
- What is this mysterious Unity Memory Profiler?
- How you can start profiting from the Unity Memory Profiler right away.
- Steps to find memory optimization opportunities for your game to reduce crashes and loading times
Quick Navigation
1. What's the Unity Memory Profiler?
2. How to Start Using the Unity Memory Profiler Right Away
3. How to Find Memory Optimization Opportunities in Your Game
1. What's the Unity Memory Profiler?
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:
- Reduce crashes due to oversized memory allocation.
- Decrease loading times
- Find memory leaks
- Fit your game in weaker devices → increase your sales
You can use this tool in a variety of ways:
- You can debug a single memory snapshot or compare/diff two snapshots.
- You can visualize the memory representation in a tree map (shown below), as a memory map or in tabular mode.
Unity Memory Profiler Snapshot: Tree View
Let's see how you can start using this tool right away.
2. How to Start Using the Unity Memory Profiler 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).
Unity 2020.2+: Enabling Preview Packages
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?).
3. How to Find Memory Optimization Opportunities in Your Game
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.
Unity Memory Profiler Snapshot: Tree View
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:
Unity Memory Profiler Snapshot: Opening a Tree Section
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?
Unity Texture: Uncompressed Format
Whooops, Ruben sloppily "forgot" to turn on the texture compression for this blog post.
Easy fix.
Unity Texture: DXT1 Format
Let me recap the process:
- Install the (experimental) memory profiler package.
- Open it and capture the game state you want.
- Click on the bigger sections to analyze which assets are taking more memory than you'd expect.
- Diagnose what's going on with these assets (mostly import settings).
- Fix them.
- Ship it.
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:
- Memory Map
- Table Mode
Why not today?
Because they are advanced and I think you and I should start it with a fresh mind.
So, What's Next?
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