Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Other I can't see what is making my editor leak this much in the Memory Profiler

Discussion in 'Editor & General Support' started by LucasForcinaSD, Dec 15, 2022.

  1. LucasForcinaSD

    LucasForcinaSD

    Joined:
    Sep 13, 2021
    Posts:
    18
    Unity 2021.3.0 & 2021.3.15, URP 12

    Hi,
    Rencently, our team has been facing random critical leaks in Unity Editor while doing absolutely nothing. Memory usage remains flat until it starts to spike at a rate of approx 200MB/s.

    We tried figuring out what was causing the leak using the memory profiler, but it seems it is linked to Native Allocations, and we don't understand what it means, nor how we can fix this.

    Here is the table diff between 2 snapshots 23s seconds appart:
    It shows a lot of Single Large Allocation Memory Block of + 20MB that are new in the second one, sorted by size.
    upload_2022-12-15_17-26-26.png

    upload_2022-12-15_17-26-40.png

    +5.3GB where allocated while doing nothing.

    What we know so far:
    - We have a custom editor window (created using OdinInspector) that seems to be kind of linked to this; having it open will result in the leak happening sometimes, but not always.
    - Closing or hiding this will generally stop the leak
    - Leak can also rarely happen when this window is closed, in that case, opening it and closing it will stop the leak.
    - Leak is generally triggered by losing focus on the Unity Editor and taking it back.
    - Leak can be stopped and memory can be freed by entering Playmode while having custom editor window hidden
    - Not everyone in the team seem to have witnessed this issue

    Here is the graph from windows task manager. The last flat line is when we close the editor window.
    upload_2022-12-15_17-32-53.png

    If anyone could help us with this issue that would be very nice, this is causing multiple unexpected crashes to some people and making working really difficult.

    Thanks a lot.
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,240
    Maybe this is the URP memory leak (editor only, or does it also happen at runtime?). Check the release notes of the latest 2021 unity version
     
  3. LucasForcinaSD

    LucasForcinaSD

    Joined:
    Sep 13, 2021
    Posts:
    18
    Thanks for the tip,
    I am pretty confident this is a problem that could be fixed on our side. We are aware of the memory leak of the URP but we've been using different instances of 2021.3 for months now (.0, .2, and .15 to be precise) and it never really bothered us until now. It's only been a couple days that 20s into the editor would result in an out of memory crash.

    [Edit]
    Forgot half of the answer, but it doesn't happen at runtime if our custom editor windows isn't visible. Even better, launching playmode generally results in memory being freed by a lot.
     
    Last edited: Dec 16, 2022
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,593
    There are some instances when using particularly custom Odin stuff that can result in a memory leak. Are you doing anything with Odin's property tree in particular?

    What does the Editor window do in particular? Any chance at seeing it's code?

    If you think it's linked to Odin a post to their discord might be worthwhile.
     
  5. LucasForcinaSD

    LucasForcinaSD

    Joined:
    Sep 13, 2021
    Posts:
    18
    Thanks for your answer.
    Our Custom Editor Window's purpose is to reference multiple parameters to allow playing the game in editor in different condition (Starting Scene, debug toggles, starting Player Info).
    Thoses parameters are serialized in scriptable objects we call GameFlowSettings that are added to the odin menu tree. We can have multiple GameflowSettings and play using the selected but we only use two of them actively (Editor, modified at need, and Build, persistent, used when in standalone)

    This is our BuildMenuTree override:

    Code (CSharp):
    1. protected override OdinMenuTree BuildMenuTree()
    2.     {
    3.         OdinMenuTree tree = new OdinMenuTree();
    4.         tree.Selection.SupportsMultiSelect = true;
    5.         tree.Add<GameflowSettings>(GameflowSettingsPath);
    6.  
    7.         return tree;
    8.     }
    And this is our BeginDrawEditors override:
    Code (CSharp):
    1. protected override void OnBeginDrawEditors()
    2.     {
    3.         OdinMenuTreeSelection selected = MenuTree.Selection;
    4.  
    5.         SirenixEditorGUI.BeginHorizontalToolbar();
    6.         {
    7.             GUILayout.FlexibleSpace();
    8.  
    9.             if (SirenixEditorGUI.ToolbarButton("Delete Current"))
    10.             {
    11.                 ScriptableObject asset = selected.SelectedValue as ScriptableObject;
    12.                 string path = AssetDatabase.GetAssetPath(asset);
    13.                 AssetDatabase.DeleteAsset(path);
    14.                 AssetDatabase.SaveAssets();
    15.             }
    16.  
    17.         }
    18.         SirenixEditorGUI.EndHorizontalToolbar();
    19.     }
    As for the GameflowSettings code, all the function are either called by Buttons or in PlayMode, which I doubt would affect the memory in editor, or I am missing something.

    I'll try this aswell, thanks!
     
  6. LucasForcinaSD

    LucasForcinaSD

    Joined:
    Sep 13, 2021
    Posts:
    18
    Hello @MartinTilo
    Sorry for tagging you but in my research on the forums I have seen you've been very active on memory issues.
    Could you help us by any chance ?
     
  7. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,111
    Hi,
    I don't really have enough insights into OdinInspector to make much of a qualified comment. Normally, Non Rooted objects point at something in Unity's backend where a native allocation isn't properly registered with the Memory Manager. I'm therefore somewhat surprised to see that a C# plugin managed to leak these, but we did have an issue with AssetBundles that had a similar effect. And no, I don't think this is the same issue, just that it's not completely unheard of. So it could be a mixture of a bug in Odin and in Unity.

    The fact that Object allocations are made and the remapper grows, speaks to some constantly recreated instances of a type inheriting from UnityEngine.Object. And this is where my lack of context for Odin kicks in. But maybe with that knowledge you can check the code and scrutinize every
    new
    and check if the type is a Unity Object. Then check if that happens over and over, e.g. because it registered to EditorApplication.update. And then, divide and conquer. Comment those bits out and see if that solves it.
     
  8. bjarkeck

    bjarkeck

    Joined:
    Oct 26, 2014
    Posts:
    301
    I'd be happy to help on the Odin side if I can.

    I'd love to see a recording of a couple of "loose focus / gain focus" using Superluminal. There's a good chance you'd gain more insight and perhaps pinpoint the issue using that. If you give it a go you're welcome to send me a recording. It'll also give you an idea of what Unity is doing - it provide symbols for Unity's native assemblies too.
     
  9. bjarkeck

    bjarkeck

    Joined:
    Oct 26, 2014
    Posts:
    301
    Also thought I'd mention - we do very few things in Odin that involves natively allocating things. We have some dynamically created textures, and materials and such. But those only get created ones when the project start (we also preserve them cross domain reload). However, we'll re-create them if they suddenly become "destroyed" by Unity, but we have managed pointers to every dynamically created resource, so that shouldn't happen, but if they for some reason didn't want to be created, or became destroyed for other reasons. Something like that could result in Odin constantly generating textures every frame. (They don't exceed 20mb in size though)

    Unlikely, but issues like that would be easy to discover using Superluminal :p
     
    Last edited: Dec 21, 2022
    MartinTilo likes this.
  10. LucasForcinaSD

    LucasForcinaSD

    Joined:
    Sep 13, 2021
    Posts:
    18
    Thank you both for your answers!
    Our studio will be off for the winter holidays so we're going to try to profile the Editor using Superluminal once we get back to work, and will keep you updated.
     
    MartinTilo and bjarkeck like this.