mrpmorris / Fluxor

Fluxor is a zero boilerplate Flux/Redux library for Microsoft .NET and Blazor.
MIT License
1.27k stars 147 forks source link

Possible memory leak? #355

Closed cerkoid closed 1 year ago

cerkoid commented 1 year ago

Hi.

Is it possible that Fluxor Store instances are not garbage collected? I tested large blazor server app and instances of Store, App, etc. are kept in memory, so I started investigating where is the problem... Maybe problem is in my test, since I monitored memory while pressing F5 in browser, just to check if there is some potential problem :)

How to reproduce: Create new blazor server project, in VS profile memory. While pressing F5 and comparing memory snapshots, there is always only 1 instance of App class and memory looks clean.

Now add Fluxor in the mix like: builder.Services.AddFluxor(o => { o.ScanAssemblies(/main assembly/); } And add to App.razor

Retry VS memory profile while hitting F5 and Store instances are not GC-ed even though Dispose methods are being called (2x per store instance?). I couldn't find what is holding reference to instances, it looks like sth in DI system..., it could be that blazor is not recycling connections / instances, but don't know why app without fluxor is not showing such symptoms maybe because fluxor is using JS calls and blazor connection lifecycle is not GCed instantly...

I looked all of the source code of Fluxor but couldn't find some obvious problem... maybe problem even does not exist, but adding fluxor to an app changes memory footprint / consumption quite a bit...

Does anyone have an idea, why memory would behave like that absent some obvious memory leak?

Regards.

mrpmorris commented 1 year ago

Blazor Server holds onto the state in case the user reconnects.

cerkoid commented 1 year ago

Ok, I see what you mean. But why then DI system calls Dispose on store but GC keeps instance in memory?

cerkoid commented 1 year ago

Ok I found out what is the root cause of my observations. It's not about user reconnecting, but System.Text.Json caching some stuff in .NET 7. That's why behaviour is totally unexpected...

https://github.com/dotnet/aspnetcore/issues/44062 https://github.com/dotnet/runtime/issues/76548