Basic

May 26 2020

0 comments

Unity Memory Profiler: Where Are You Wasting Your Game’s Memory? (Part 1)

By Rubén Torres Bonet

May 26, 2020


__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"6c4de":{"name":"Main Accent","parent":-1},"67ed2":{"name":"Accent Dark","parent":"6c4de","lock":{"saturation":1}}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"6c4de":{"val":"var(--tcb-skin-color-0)"},"67ed2":{"val":"rgb(59, 65, 63)","hsl_parent_dependency":{"h":160,"l":-0.05,"s":0.04}}},"gradients":[]},"original":{"colors":{"6c4de":{"val":"rgb(51, 190, 127)","hsl":{"h":152,"s":0.57,"l":0.47}},"67ed2":{"val":"rgb(59, 65, 63)","hsl_parent_dependency":{"h":160,"s":0.04,"l":0.24}}},"gradients":[]}}]}__CONFIG_colors_palette__

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

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

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

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

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

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

Unity Texture: Uncompressed Format

Whooops, Ruben sloppily "forgot" to turn on the texture compression for this blog post.

Easy fix.

Unity Texture: DXT1 Format

Unity Texture: DXT1 Format

Let me recap the process:

  1. Install the (experimental) memory profiler package.
  2. Open it and capture the game state you want.
  3. Click on the bigger sections to analyze which assets are taking more memory than you'd expect.
  4. Diagnose what's going on with these assets (mostly import settings).
  5. Fix them.
  6. 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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Rubén Torres Bonet

About the author

Born Game Developer, now ready to help you develop better games. Primary programmer on Star Trek Bridge Crew (Oculus Quest), Diamond Dash. Programmer on Time Stall, Catan Universe, Anne Frank House VR, Jelly Splash, Blackguards Definitive Edition. I also worked in minor XR experiences for HoloLens and Vive for clients such as Audi and Volkswagen.

You might also like

The Bright Side of Ray-Traced Global Illumination in Unity
>