Workflow: Understanding memory
Introduction
Developers often discuss CPU performance when talking about issues that slow down games. What is often not considered is how the wrong memory usage can affect performance and lead to severe problems.
One example of such an issue is memory fragmentation. It slows down code execution and can lead to situations where it is impossible to allocate memory even if a global summary (the total amount of free memory in the system) shows that it should be possible.
Memory Map allows you to have a peek into the memory layout of your game, to understand how it works, and how it changes between snapshots. This knowledge can then be utilized to write better code that will take advantage of underlying memory systems inside of Unity.
For more information on memory fragmentation, see Understanding the managed heap.
Inspect memory
- Make sure the Memory Profiler is attached to your running Player.
- Create a memory snapshot of your Scene.
- You can now close the Player (optional). It is a recommended step when you are taking the snapshot on the same device as the Player. As snapshot files can get quite large, this step is advisable to avoid the snapshot and Player competing for memory on low-end machines.
- Open the newly created snapshot in the Memory Profiler.
- Switch to the Memory Map view. This option displays a view of all allocations in your Project mapped out along the virtual address space.
View memory changes over the time
- Make sure the Memory Profiler is attached to your running Player.
- Create a memory snapshot of a situation that interests you.
- Continue to Play through the Scene.
- Create a second memory snapshot of a situation that interests you.
- You can now close the Player (optional). It is a recommended step when you are working with large snapshot files, to avoid the snapshot and Player competing for memory on low-end machines.
- Open the first snapshot in the Memory Profiler window.
- Open the second snapshot, then click the Diff button in the Open Snapshots view. Opening a snapshot might take a few moments, depending on the size of the snapshot file. You will then see the Diff table.
- Switch to the Memory Map Diff view. This option displays the Memory Map in a mode that highlights the differences between the two snapshots.