The Memory Profiler is a new package for Unity 2018.3 or higher that allows you to analyze the memory usage of a project to find memory leaks and fragmentation. Memory leaks can occur when memory is allocated but is never deallocated for reuse after the memory is no longer needed. In this tutorial, you will learn how to use the Memory Profiler to check for memory leaks.
The memory profiler
With the memory profiler you can analyze the memory usage of a project in order to find leaks and fragmentation. Memory leaks can occur when memory is allocated but not released after it is no longer needed. Program performance degrades when memory runs out, which ultimately leads to the program (and possibly even the host device) crashing. When memory is allocated in many small blocks rather than in smaller contiguous blocks, it is called memory fragmentation. As a result, the small, unallocated memory areas between the allocated blocks are no longer available for use.
Installation of the memory profiler
The Memory Profiler works with the Unity Editor as well as with development builds and is available in the Package Manager as a preview package.
How to install the Memory Profiler:
1. In the Unity Editor, select Package Manager from the Window drop-down list.
2. Click on Advanced and select Show Preview Packages.
3. Find Memory Profiler and click Install in the lower right corner of the Package Manager window.
4. After installation, you can access the Memory Profiler by selecting “Analysis”> “Memory Profiler” from the “Window” drop-down list.
Record a memory snapshot
The storage profiler can either target a currently running development build on the local computer or assign it a specific IP address for a networked computer.
1. Before you can take a dump, you need to select a destination. By default, the target is the Unity editor.
2. To select a new target, click the drop-down menu in the upper left corner of the Memory Profiler.
Click the Capture button to capture the current storage profile of the selected target. Note that the capture itself takes up some memory. In some cases, this can cause the target to freeze or crash when running out of memory.
Import a snapshot of the captured memory
1. Click the Import button at the top of the Memory Profiler window.
2. Use the File Format drop-down menu in the lower right corner of the Import Snapshot File dialog box to select the format of the storage snapshot you want to import. Options include .snap files captured with the Memory Profiler and .memsnap * files created with the Bitbucket Memory Profiler.
3. Navigate to your snapshot and select it. Click the Open button.
4. The first time you take a memory snapshot, a folder named MemoryCaptures will be created (at the same level as the Assets folder, not in this folder). You can also save memory snapshots in this folder to make them accessible.
5. View the memory dump
After capturing or importing (including moving the snapshot to the MemoryCaptures folder), the snapshot will appear in the left column of the Memory Profiler window. Click Open to view the storage profile.
The standard and most intuitive view is the tree structure (Figure 06). This view shows the amount of memory used by each element type proportionally. In our example, 126 shaders occupy a total of 80 MB of RAM.
To view a category in more detail, click it to view all of the items and their respective memory footprints. More detailed information is displayed in a table below the tree structure. Click the headings to sort the table by value or group.
The table view, which is located both under the tree structure and under the memory structure, contains more detailed information on the memory usage of individual elements.
A more direct view, the Memory Map, shows the memory layout in different colors depicting different memory types. This can be helpful in locating memory fragmentation.
Working with the Memory Profiler
Find a memory leak
How to find a memory leak:
1. Start the player, set it as the save profile target and take a snapshot of an empty scene.
2. Load the scene you want to test. Play part of the scene to load the necessary assets.
3. Unload the scene by typing Resources.UnloadUnusedAssets () or by loading / creating an empty scene twice
4. Take another snapshot and exit playback mode.
5. Load the first and the second screenshot in the Workbench and click on Diff. Unity calculates the difference between the two screenshots. This could take a few seconds depending on the size of the snapshot.
6. In the table view, click on the heading of the Diff column and select Group.
7. Entries are grouped: the same, in both snapshots, from the first deleted snapshot and new. New entries are possible memory leaks.
The Memory Profiler is a great way to monitor the memory usage of your project.
Not only can you see what is using the most storage space, but also how your project is using or abusing storage space. Memory leaks and fragmentation are two of the most common problems that affect the project
and the Memory Profiler troubleshoots it.
Originally posted on 2020-03-16 16:46:00.