Workflow: How to find memory leaks
Introduction
More often than not, developers encounter memory leaks within their applications. Memory leaks can slow down your application and ultimately lead to crashes.
Memory leaks typically occur due to one of two issues:
- An object that is not released manually from memory through the code.
- An object stays in memory because of an unintentional reference.
The Memory Profiler can be used to track down these leaks in both managed and native memory.
The goal of this workflow will be to capture multiple memory snapshots over a specific timeframe and compare them inside the Memory Profiler window using the Diff mode.
For more information, see Workbench Diff snapshots.
Inspect memory usage and find memory leaks
Allocated objects after Scene unload
There are multiple patterns in which memory leaks occur. A common one is when resources, or user allocated objects, are not cleaned up after unloading a Scene. The following steps depict a basic workflow for attempting to identify such issues.
Make sure the Memory Profiler is attached to your running Player.
In the Player, load an empty Scene.
Create a memory snapshot of the empty Scene.
Load the Scene that you wish to test for leaks.
Play through the Scene. E.g., if the Scene contains a level of a game, play through a bit of that level.
Unload the Scene or switch to an empty Scene.
Note: To fully unload Assets in the last opened scene; you will need to either call Resources.UnloadUnusedAssets or load into two new scenes (e.g., Load Empty Scene twice).
Take another snapshot.
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.
In the Memory Profiler window, open the first snapshot. You’ll find it in the Workbench, second from the bottom of the list. Opening a snapshot might take a few moments, depending on the size of the snapshot file.
Then, open the second snapshot from the bottom of the list in the Workbench.
At the bottom of the Workbench, the two open snapshots are side by side. Click the Diff button in-between them, this might take a few moments, depending on the size of the snapshot file. You will then see the Diff table for all objects.
Click the table header for the Diff column; this presents you with a drop-down menu:
Select the Group option. You’ll see the data entries grouped by Deleted, New, and Same.
Under New, you’ll find a list of all objects that are only present in the second snapshot. These are potential memory leaks.
Small continuous allocations during application lifetime
Another typical pattern occurs when we have small continuous allocations that arise after a particular event in the lifetime of the Unity application. The following steps show a workflow in identifying such leaks.
Make sure the Profiler is attached to your running Player.
Load the scene you’d like to check for leaks.
Create a snapshot.
Play through the Scene, observe the memory footprint growing steadily in the Profiler window (under the Memory tab).
Create another snapshot.
Continue playing the Scene.
Create another snapshot.
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.
In the Memory Profiler window, open the second snapshot you’ve taken. Opening a snapshot might take a few moments, depending on the size of the snapshot file.
Open the third snapshot you've taken. Then click the Diff button in the Open Snapshots view, this might also take a few moments, depending on the size of the snapshot file. You will then see the Diff table.
Click the table header of the Diff column; this presents you with a drop-down menu:
Click on the Owned Size table header and select Group; this filters objects based on their presence in the snapshots and their size.
Click again on the Owned Size table header and select Sort Descending this sorts all the groups based on their size.
Under Same and New, attempt to identify the larger groups of allocations present and check if they are present in both Same and New categories. Make a note of any you find.
Repeat steps 11-14 for the first and second snapshots. By repeating the process, we will gain an idea of what the potential leaks or problems are within your system.