How to find memory leaks
If you find a memory leak in your application, it might slow it down and ultimately lead to crashes.
Memory leaks typically happen because of one of two issues:
- An object is not released manually from memory through the code.
- An object stays in memory because of an unintentional reference.
You can use the Memory Profiler to track down these leaks in both managed and native memory.
This workflow example describes how to capture multiple memory snapshots over a specific timeframe and compare them inside the Memory Profiler window with the Diff mode.
For more information, see Workbench Diff snapshots.
Find and fix memory leaks that happen after Scene unload
There are multiple ways that memory leaks happen. A common way is when resources, or user allocated objects, are not cleaned up after unloading a Scene. The following steps outline the basic workflow to attempt to identify such issues.
- Attach the Memory Profiler to the running Player. For information on how to do this see Capture a memory snapshot documentation.
- In the Player, load an empty Scene.
- Create a memory snapshot of the empty Scene.
- Load the Scene you want to test for leaks.
- Play through the Scene. For example, if the Scene contains a level of a game, play through a part of that level.
- Unload the Scene or switch to an empty Scene. Note: To fully unload Assets in the last opened Scene, you need to either call Resources.UnloadUnusedAssets or load into two new Scenes (e.g., Load Empty Scene twice).
- Take another snapshot.
- Close the Player. This is optional but recommended if 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, in the Open Snapshots pane the two open snapshots are side by side. Select the Diff button. The Diff table for all objects appears in the Main View.
- Select the Diff table header for the Diff column and choose Group. The Memory Profiler groups the data entries by Deleted, New, and Same.
- The objects under New are only present in the second snapshot. These are potential memory leaks.
Find and fix small continuous allocations during application lifetime
Another typical pattern occurs when your application has small continuous allocations that happen after a particular event in its lifetime. The following steps outline how you can identify these type of leaks.
- Attach the Profiler to your running Player.
- Load the Scene you want to check for leaks.
- Create a memory snapshot.
- Play through the Scene, and watch the memory footprint grow steadily in the Profiler window (under the Memory module).
- Create another snapshot.
- Continue playing the Scene.
- Create another snapshot.
- Close the Player. This is optional but recommended if 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 select the Diff button in the Open Snapshots view. This might take a few moments, depending on the size of the snapshot file. You can then see the Diff table in the Main View.
- Select the Diff table header for the Diff column and choose Group. The Memory Profiler groups the data entries by Deleted, New, and Same.
- Select the Owned Size table header and choose Group. This filters objects based on their presence in the snapshots and their size.
- Select the Owned Size table header again and choose Sort Descending. This sorts all of the groups based on their size.
- Under Same and New in the Diff column, 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, you can understand what the potential leaks or problems are within your system.