Caliburn.Micro icon indicating copy to clipboard operation
Caliburn.Micro copied to clipboard

Attached events and memory profiling

Open coder89 opened this issue 9 years ago • 15 comments

Hi,

I'm doing some memory profiling of my UWP (with a ShellView) app and I discovered that app leaks. I tried to isolate the problem by adding an empty View/VM with below lines in XAML:

<cm:Message.Attach>
    [Event SizeChanged] = [OnViewSizeChanged($eventArgs)];
</cm:Message.Attach>

When I navigate to that page and then back to the previous one (and doing so several times, forcing GC after all) both View and ViewModel seem to stay in memory forever (something that references Behaviors/Interaction SDKs keeps references). Removing above lines from XAML and replacing them with standard XAML SizeChanged="ThisPage_SizeChanged" attribute problem is gone.

On the other hand using x:Name attribute for VIew/VM binding seems to work just fine. Only the attached events seem to be the problem.

Any idea what can be wrong?

Thanks, Lukasz

coder89 avatar Dec 16 '15 20:12 coder89

I could confirm this issue. Temporary removed all Message.Attach from views because of that.

sevenate avatar Dec 16 '15 21:12 sevenate

Seems like x:Name binding to the Button causes the same issue (RoutedEnevtHandler seems to remain there forever and it is kinda random.. 1-2 buttons seems alright but adding more makes GC completely freak out). Doesn't make any sense :D

BTW is it actually a good idea to keep Behaviors SDK reference in CM? This break the entire .Net native compiler and prevents good optimization. Wasn't that the whole reason why Behavios are removed from UWP?

coder89 avatar Dec 16 '15 21:12 coder89

EDIT: x:Name binding in TextBlock adds +1 to refcount of the VM (View can be collected though). Seems like I have to roll back to x:Bind / Binding in XAML instead.

coder89 avatar Dec 16 '15 21:12 coder89

Thanks for the info. Can you rerun your tests but against a build of the UWP Behaviours SDK from source. There was a bug Microsoft/XamlBehaviors#55 that caused EventTriggerBehavior to not detach from the event correctly. I'm wondering if this is part of the problem.

Regarding using the Behaviors SDK. Caliburn.Micro has been built on to of behaviors from the beginning (something like 2009). Removing them now would be awkward to say the least.

nigel-sampson avatar Dec 16 '15 22:12 nigel-sampson

Surprised about the view model reference count being added to by the TextBlock binding this should simply be adding a Text="{Binding PropertyName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" to the control. Can you verify in your tests that replacing the x:Name with the above doesn't add to the reference count?

nigel-sampson avatar Dec 16 '15 23:12 nigel-sampson

I'll take a look at it when I get back from work. Anyways it feels like the problem is much more complex than that. It feels like the main cause could be somewhere in the INavigationService implementation. Having just a simple Binding causes memory profiler mark View/VM as "Cycle Detected". I do not store VM/View as properties anywhere so it much be the "Screen" + "FrameAdapter" implementation doing something funny.

coder89 avatar Dec 16 '15 23:12 coder89

If the view model is IViewAware it will hold a weak reference to the view. Also if you're using CachingFrameAdapater then naturally this hold a reference to the view model.

nigel-sampson avatar Dec 16 '15 23:12 nigel-sampson

Not using a CachingFrameAdapater and weak reference should be removed when forcing GC

coder89 avatar Dec 17 '15 00:12 coder89

Excellent, just wanted to document all assumptions, I probably won't be able to dig into this till the weekend.

nigel-sampson avatar Dec 17 '15 00:12 nigel-sampson

@nigel-sampson hi! Any progress on this issue?

winnie1pooh avatar Feb 26 '16 15:02 winnie1pooh

@winnie1pooh it was announced to be released soon http://compiledexperience.com/blog/posts/behaviors-1.1.0

altso avatar Feb 26 '16 16:02 altso

No progress on this specific issue, happy to take some help from people with experience in this.

In terms of the 3.0.0 release, plan was for yesterday, but was passed word of a potential UWP bug, so holding off till I get a repro of it,

nigel-sampson avatar Feb 27 '16 08:02 nigel-sampson

Do we know if this problem has been fixed or if there is any workaround that helps mitigate the number of RoutedEventHandlers being held onto?

JSmyth886 avatar Apr 08 '16 08:04 JSmyth886

There hasn't been anything done for this specific issue, happy to take any help offered here.

nigel-sampson avatar Apr 13 '16 23:04 nigel-sampson

Hello, is there any fix or a work around for this issue yet? I'm running into a similar problem.

zez9787 avatar Oct 12 '17 10:10 zez9787