There are two ways of analyzing memory usage in your application in Unity:
This page covers information on the built-in Memory Profiler module. For more information on the Memory profiler package, see the Memory Profiler documentation.
The Memory Profiler module visualizes counters that represent the total allocated memory in your application. You can use the memory module to see information like the number of loaded objects, and the memory that they take in total per category. You can also see the number of GC allocations per Profiler frame.
When you profile your application in the Editor, the Memory Profiler module reports higher data use than a similar profile of the application built on a target device would. This is because the Unity Editor uses specific objects that take up memory, and the Editor window itself uses extra memory.
Part of the extra memory use is because Unity treats objects like textures as read/write enabled in the Editor and keeps an extra copy of each texture on the CPU. This effectively doubles the reported memory use of textures in the Editor; for a more accurate idea of memory use by textures, profile a built version of your application running on the target platform.
Also, because Unity can’t cleanly separate the memory that the Profiler itself takes up from the Play mode’s memory, memory that the Profiler uses is displayed in the Profiler window. To remind you of this, a warning displays at the top of the Memory Profiler module details pane whenever you have the Profiler target set to Play Mode or Editor. For more precise numbers and memory usage for your application, you should profile your application on the target device and operating system you intend it to run on. For more information, see the documentation on Profiling your application.
The Memory module is divided into six charts that display detailed information on where your application spends memory. You can change the order of the categories in the chart; to do this, drag and drop them in the chart’s legend. You can also click a category’s colored legend to toggle its display.
Category | Description |
---|---|
Total Allocated | The total memory your application has used. |
Texture Memory | How much memory the TexturesAn image used when rendering a GameObject, Sprite, or UI element. Textures are often applied to the surface of a mesh to give it visual detail. More info See in Glossary in your application have used. |
MeshThe main graphics primitive of Unity. Meshes make up a large part of your 3D worlds. Unity supports triangulated or Quadrangulated polygon meshes. Nurbs, Nurms, Subdiv surfaces must be converted to polygons. More info See in Glossary Memory |
How much memory the Meshes in your application have used. |
Material Count | The number of MaterialAn asset that defines how a surface should be rendered. More info See in Glossary instances in your application. |
Object Count | The number of native object instances in your application. |
GC Used Memory | The amount of memory used by the GC heap. |
GC Allocated in Frame | The amount of memory allocated per frame on the GC heap. |
There are two views available in the module details pane, located at the bottom of the Profiler window: Simple and Detailed. To change the view, select it from the dropdown in the top left of the module details pane.
The Simple view displays an overview of how Unity uses memory in real-time on a per-frame basis.
Unity reserves memory pools for allocations to avoid asking the operating system for memory too often. The Profiler displays how much memory is reserved, and how much Unity is using.
This information is also available via the ProfilerRecorder API and in the Profiler Module Editor so you can add them to a custom Profiler module.
Statistic | Description | Available in Release Players |
---|---|---|
Total Used Memory | Total value of memory that Unity uses and tracks. | Yes |
Total Reserved Memory | Total reserved memory that Unity uses for tracking purposes and pool allocations. | Yes |
GC Used Memory GC Reserved Memory |
The used heap size and total heap size that managed code uses. This memory is garbage collected. | Yes |
Gfx Used Memory Gfx Reserved Memory |
The estimated amount of memory the driver uses on Textures, render targets, Shaders, and Mesh data. | No |
Audio Used Memory Audio Reserved Memory |
The Audio system’s estimated memory usage. | Yes |
Video Used Memory Video Reserved Memory |
The Video system’s estimated memory usage | Yes |
Profiler Used Memory Profiler Reserved Memory |
The memory the Profiler functionality uses and reserves from the system. | Yes |
System Used Memory | The values in the Profiler are different to those displayed in your operating system’s task manager, because the Memory Profiler does not track all memory usage in your system. This includes memory that some drivers and plug-ins use, and memory used for executable code. On platforms that support getting the total memory size of the application from the operating system, the System Memory Usage is over 0 and is the same size in a task manager. |
Yes |
Texture Count Texture Memory |
The total count of loaded textures and memory they use. | No |
Mesh Count Mesh Memory |
The total count of loaded meshes and memory they use. | No |
Material Count Material Memory |
The total count of loaded materials and memory they use. | No |
AnimationClip Count AnimationClip Memory |
The total count of loaded AnimationClips and memory they use. | No |
Asset Count | The total number of loaded assets. | No |
GameObject Count | The total number of GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info See in Glossary instances in the sceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info See in Glossary. |
No |
Scene Object Count | The total number of dynamic UnityEngine.Object s. This number includes the GameObject Count, plus the total number of components, and everything which is not asset in the scene. |
No |
Object Count | The total number of native UnityEngine.Object s that your application creates or loads. This number includes the Scene Object Count, plus the total number of all assets. If this number rises over time, your application is creating GameObjects or other assets and never destroying or unloading them. |
No |
GC Allocation In Frame Count GC Allocated In Frame |
Amount of managed allocations and its total size in bytes. | No |
You can use the ProfilerRecorder API to access the Memory Profiler module’s counters in Players. The following example contains a simple script that collects “Total Reserved Memory”, “GC Reserved Memory” and “System Used Memory” metrics, and displays those as a GUI.TextArea. The Memory Profiler module information belongs to the ProfilerCategory.Memory Profiler categoryIdentifies the workload data for a Unity subsystem (for example, Rendering, Scripting and Animation categories). Unity applies color-coding to categories to visually distinguish between the types of data in the Profiler window.
See in Glossary.
using System.Text;
using Unity.Profiling;
using UnityEngine;
public class MemoryStatsScript : MonoBehaviour
{
string statsText;
ProfilerRecorder totalReservedMemoryRecorder;
ProfilerRecorder gcReservedMemoryRecorder;
ProfilerRecorder systemUsedMemoryRecorder;
void OnEnable()
{
totalReservedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "Total Reserved Memory");
gcReservedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "GC Reserved Memory");
systemUsedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "System Used Memory");
}
void OnDisable()
{
totalReservedMemoryRecorder.Dispose();
gcReservedMemoryRecorder.Dispose();
systemUsedMemoryRecorder.Dispose();
}
void Update()
{
var sb = new StringBuilder(500);
if (totalReservedMemoryRecorder.Valid)
sb.AppendLine($"Total Reserved Memory: {totalReservedMemoryRecorder.LastValue}");
if (gcReservedMemoryRecorder.Valid)
sb.AppendLine($"GC Reserved Memory: {gcReservedMemoryRecorder.LastValue}");
if (systemUsedMemoryRecorder.Valid)
sb.AppendLine($"System Used Memory: {systemUsedMemoryRecorder.LastValue}");
statsText = sb.ToString();
}
void OnGUI()
{
GUI.TextArea(new Rect(10, 30, 250, 50), statsText);
}
}
The following screenshot shows the result of adding the script to the Tanks! tutorial project.
This information is available in Release Players, as are the other high level counters in the table above. If you want to see the selected Memory counters in a custom module in the Profiler window, use the Module Editor to configure the chart.
The Detailed view provides a snapshot of your application’s current state. Click the Take Sample button to capture detailed memory usage for the current target. It takes the Profiler time to get this data, so the Detailed view does not give you real-time details. After the Profiler takes a sample, the Profiler window displays a list view where you can explore your application’s memory usage in more detail.
Enable the Gather object references setting at the top of the module details pane to collect information about what is referencing an object at the time of the snapshot. The Profiler displays this information in the right-hand pane of the window.
The list view divides objects that use memory into the following categories:
When you click on a GameObject in the Assets or Scene Memory list, it’s highlighted in the Project or Scene viewAn interactive view into the world you are creating. You use the Scene View to select and position scenery, characters, cameras, lights, and all other types of Game Object. More info
See in Glossary.
Note: In the Other category, memory reported under System.ExecutableAndDlls
is read-only memory. The operating system might discard these pages as needed and later reload them from the file system. This generates lower memory usage, and usually does not directly contribute to the operating system’s decision to close your application if it uses too much memory. Some of these pages might also be shared with other applications that are using the same frameworks.