Profiling in Unity

Matt Buckley
Nice Things | iOS + Android Development
4 min readDec 1, 2018

Like this post? You might like BentoBlox too — check out the game here on the App Store.

This week I’m taking a break from active prototyping to learn about Unity’s built-in profiling capabilities.

The Unity profiler enables you to monitor CPU performance, rendering, memory allocation, physics, and more. Using the profiler, you can:

  • Identify where and when FPS drops suddenly
  • Tell how much time your program takes to render an individual frame
  • Identify which function calls in your code are taking up the most CPU
  • Tell how much time those function calls take to execute
  • Isolate performance issues on individual devices/platforms (iPhone, Android, etc)

With the profiler, you can zero-in on bottlenecks and get to work optimizing your code.

The Basics

You can find the profiler in Window -> Analysis -> Profiler or open it simply by hitting ⌘7. With the profiler open, click Play and you’ll see the profiler tab populate with data:

There are a number of important elements to make note of here, so I’ll go through each piece individually.

CPU Usage

Selecting CPU Usage in the top panel of the profiler tab surfaces data in the bottom panel. The line graph in the top panel visualizes CPU usage over time (by individual frame). Clicking on any point in this timeline isolates an individual frame and shows the function calls taking place during that frame in the bottom panel.

The columns in the bottom panel to the right of each function call are self-explanatory, but it’s worth highlighting the two you’ll find especially useful:

  • Total- shows the percentage of total CPU being utilized by a given function call
  • Time- shows the time (in milliseconds) taken to execute a given function call

For even more detail, you can enableDeep Profile mode.

This ensures that all function calls are recorded individually. Deep Profile allows for, well, a “deeper”, level of insight into where time is being spent in your game code, but uses a great deal of memory and can cause gameplay to visibly slow down and should be enabled with that caveat in mind.

Memory

The Memory tab shows, as you might expect, memory allocation during each frame. Irregularities here are a sign that you might have a memory leak, or otherwise be able to improve memory management and keep your app/game’s footprint small by making a code change.

Selecting the Simple view in the dropdown menu of the bottom panel surfaces a high-level overview of memory usage for a given frame. Toggling to Detailed allows you to inspect the memory footprint of individual objects and assets, which can prove very useful in identifying any large objects that might be loaded into memory unnecessarily for a given frame.

Rendering

The rendering tab shows the number of batches, draw calls, triangles and vertices at a given frame. Clicking on a single frame surfaces more detailed information and statistics in the bottom panel.

Physics and Physics2D

The Physics and Physics2D tabs track the number of bodies (components) active during each frame. Comparing peaks and valleys here against any spikes you see in theCPU timeline view can help you identify physics-intensive operations that adversely impact performance.

The Profiler Can Do Even More

The profiler also includes tabs for audio, video, networking, and much more. I may check these out in a future post, but we’ve covered a few of the big-ticket items here already.

How do you use the Unity Profiler? Are there tools/capabilities that you find particularly useful? I’d love to hear about your experience profiling apps and games in Unity in the comments below.

Like this post? You might like BentoBlox too — check out the game here on the App Store.

--

--