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

May 28, 2020
protect

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

So, What's Next?

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.

JikGuard.com, a high-tech security service provider focusing on game protection and anti-cheat, is committed to helping game companies solve the problem of cheats and hacks, and providing deeply integrated encryption protection solutions for games.

Read More>>