samc
Topic Author
Posts: 74
Joined: 21 Aug 2019, 19:22

question about memory leaks in editor

03 Mar 2023, 00:45

I was doing some work with the memory profiler in Unity.. Basically, I'd launch unity, play in editor in a simple level, stop playing, take a memory snapshot, then play in editor in the same level again, exit, and take a second snapshot. Then I'd compare the two snapshots using the memory profiler.
NoesisMemoryProfile.png
What I'm seeing in the memory profile is it seems like a lot of Noesis objects are leaking each time I play in editor. For example, in my initial snapshot there were 4,483 instances of "NoesisApp.DataTrigger", and in the second snapshot there were 8,966.

I'm probably just doing something stupid and not shutting things down properly, so these aren't getting cleaned up -- but I'm not sure what exactly I'm missing... or if it's more just a problem with properly disposing my data model or something else.

have y'all ever seen this before, or have suggestions?

thanks,
sam
 
User avatar
maherne
Site Admin
Posts: 42
Joined: 01 Jul 2022, 10:10

Re: question about memory leaks in editor

03 Mar 2023, 15:18

Leaks like this can be caused by references to unmanaged objects in C#.

WeakReferences are needed to avoid this behaviour, you can read about that in our Events Tutorial: https://www.noesisengine.com/docs/Gui.C ... vents-in-c

If you'd like to report an issue, with an attached sample which reproduces this issue, I will look into it.
 
samc
Topic Author
Posts: 74
Joined: 21 Aug 2019, 19:22

Re: question about memory leaks in editor

03 Mar 2023, 20:22

I think the problem is with this static "_extends" dictionary defined in "Extends.cs":
        private static Dictionary<long, ExtendInfo> _extends = new Dictionary<long, ExtendInfo>();
It looks like this is cleared on domain reload, but I don't see that it is being cleared after play in editor mode, which will cause all the objects in it to leak.

Am I missing something?

sam
 
User avatar
sfernandez
Site Admin
Posts: 3027
Joined: 22 Dec 2011, 19:20

Re: question about memory leaks in editor

06 Mar 2023, 10:58

Hi Sam,

It looks like there is some circular reference that produces those memory leaks. In 3.1.6 we did some changes to try to break those circular references that usually occur when a child dependency property stores a reference to a parent, for example if I do:
<Grid x:Name="grid" Background="Red">
  <Rectangle Fill="Blue" Margin="20">
    <b:Interaction.Triggers>
      <b:EventTrigger SourceObject="{Binding ElementName=grid}" EventName="MouseEnter">
        <b:ChangePropertyAction PropertyName="Fill" Value="Green"/>
      </b:EventTrigger>
    </b:Interaction.Triggers>
  </Rectangle>
</Grid>
Are you already using 3.1.6+ version, or still on an older version?

That _extends dictionary is cleared on domain reload as you mentioned, that will happen every time you hit Play if you have the default option selected in Unity (Edit > Project Settings > Editor and Enter Play Mode Options). Anyway, if there are circular references like the ones I described, our native library won't be able to release those objects.

I have access to your repository, so if you point me to a sample scene where I can reproduce this I can take a look to find out the cause of the leak. Please create a private ticket in our bugtracker to better handle this.
 
samc
Topic Author
Posts: 74
Joined: 21 Aug 2019, 19:22

Re: question about memory leaks in editor

06 Mar 2023, 20:12

That _extends dictionary is cleared on domain reload as you mentioned, that will happen every time you hit Play if you have the default option selected in Unity (Edit > Project Settings > Editor and Enter Play Mode Options). Anyway, if there are circular references like the ones I described, our native library won't be able to release those objects.
We are on 3.1.6 in this test case.

We don't do domain reloads when entering play mode because of the performance hit we incur.

Is there a way I could manually clean things up after playing in editor? Would it be safe for me to just call Extend.ClearTables() or would that cause memory leaks on the unmanaged side? If there is some way to shutdown / clean up everything gracefully I can easily add that.

thanks,
sam
 
User avatar
sfernandez
Site Admin
Posts: 3027
Joined: 22 Dec 2011, 19:20

Re: question about memory leaks in editor

09 Mar 2023, 11:38

We don't do domain reloads when entering play mode because of the performance hit we incur.
If there is no assembly reload then it makes sense that some objects stay alive until the GC collects them, if the objects are not referenced in C++ too. That will mean that there is a circular reference somewhere. As I mentioned before I would like to debug that scenario if you point me how to reproduce it.
Is there a way I could manually clean things up after playing in editor?
If you want to cleanup all the instances created in C# you can call Noesis.GUI.UnregisterNativeTypes() to restore everything.

Who is online

Users browsing this forum: Ahrefs [Bot], HateDread and 8 guests