I made a modification to Memory Profiler to make it easier to find memory leaks. Here’s the code in case it’s helpful to anyone.
When I’m looking for memory leaks, what works best for me is to take a snapshot in the main menu, play the game a bit, go back to the main menu, take a snapshot, and repeat that process, each time playing the game in exactly the same way, until I have 7 snapshots. Then I open the last one (snapshot 7) and look at which allocations were new in snapshots 3-6 and yet still in snapshot 7. These are the leaks.
The allocs in snapshot 7 that were new in snapshot 1 are the game’s global managers, which are fine. The allocs that were new in snapshot 2 (the first gameplay) are probably legitimate caching. The allocs that are new in snapshot 7 include legitimate things like the main menu. But the allocs that were new in snapshots 3-6 should have been deleted. If there’s a leak, even a small one, I’ll see allocs that are from snapshot 3, and probably see similar ones from snapshot 4, 5, and 6, since probably the same leak happens every time I play.
Unity’s Memory Profiler (I’m using 0.2.9-preview.3) comes very close to providing this with the Diff feature. So I only had to write a bit of code to extend it to get the functionality I needed. Thank you, Unity, for allowing us to see and modify the source.
I’ve included my modifications here in case anyone else is interested. My installation procedure is crude:
If you have Memory Profiler 0.2.9-preview.3 installed:
Unzip LeakAnalysisModification-MemoryProfiler-0.2.9-preview.3.zip on top of your Library\PackageCache\com.unity.memoryprofiler@0.2.9-preview.3 folder.
If you have a different version of Memory Profiler installed:
Unzip LeakAnalysisModification-MemoryProfiler-0.2.9-preview.3.zip and Base-MemoryProfiler-0.2.9-preview.3.zip into a temp folder and compare them to see the changes I made. Then apply the changes by hand to your Library\PackageCache\com.unity.memoryprofiler@x.x.x code.
How to use it:
- In the Editor, open Window/Analysis/Memory Profiler.
- Delete your existing snapshots.
- Start your game
- In your game, go to the main menu or somewhere constant where you want to take your snapshots. I’ll call it the main menu.
- In the Memory Profiler take a snapshot. Rename it “Snapshot-1” or something that will indicate which one it is in the sequence of snapshots.
- Play the game a bit in a way that you can repeat later. I play two battles with the random seed forced to 0 so it plays the same every time.
- Return to the main menu.
- Repeat steps #5-7 until you have at least 5 snapshots, ideally 7-12 snapshots.
- Choose Leak Analysis/Generate.
- Wait a few minutes while it opens each snapshot and gets data out of it. It’s done when it logs “Generate Leak Data done” in the Editor console. It created the file LeakData.json in your snapshots directory.
- Open the last snapshot and go to the Table/AllObjects view.
- Find the “Snapshot” column (at the far right), right-click on it, and choose “Group”.
- Look in any of the groupings that aren’t the first two snapshots or the last one. These are the potential memory leaks.
7198741–864217–LeakAnalysisModification-MemoryProfiler-0.2.9-preview.3.zip (23.7 KB)
7198741–864220–Base-MemoryProfiler-0.2.9-preview.3.zip (18.2 KB)