docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    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:

    1. Attach the Memory Profiler to a running Player. For instructions on how to attach the Memory Profiler to a Player, see Capture a snapshot.
    2. Load an empty Scene in the Player.
    3. Create a snapshot of the empty Scene.
    4. Load the Scene you want to test for leaks.
    5. Play partway through the Scene.
    6. 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).
    7. Take another snapshot. You can optionally close the Player after you take this snapshot.
    8. Follow the instructions in Compare two snapshots to open and compare both snapshots.
    9. 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.
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)