Find memory leaks
Memory leaks cause your application to perform worse over time and might eventually lead to a crash.
Memory leaks typically happen for one of two reasons:
- Your project lacks code to release an object from memory, which leads to the object remaining in memory permanently.
- An object stays in memory because of an unintentional reference.
You need to capture and compare multiple snapshots to identify memory leaks. To learn how to do this, see Compare two snapshots.
Find memory leaks that happen after Scene unload
There are multiple ways that memory leaks happen. A common cause of leaks is user-allocated objects or resources that your code doesn't release after your application unloads a Scene.
To identify this type of leak with the Memory Profiler package:
- Attach the Memory Profiler to a running Player. For instructions on how to attach the Memory Profiler to a Player, see Capture a snapshot.
- Load an empty Scene in the Player.
- Create a snapshot of the empty Scene.
- Load the Scene you want to test for leaks.
- Play partway through the Scene.
- Unload the Scene or switch to an empty Scene. 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. You can optionally close the Player after you take this snapshot.
- Follow the instructions in Compare two snapshots to open and compare both snapshots.
- Use any of the three Memory Profiler window tabs to evaluate the two snapshots. Any increase in memory use in the second snapshot is potentially due to a memory leak.