dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.45k stars 10.03k forks source link

[Blazor] Better diagnostics for Blazor server #43513

Open javiercn opened 2 years ago

javiercn commented 2 years ago

We get a lot of questions/issues opened about Blazor Server memory management. It is clear that this is a confusing aspect for customers and that they run into issues when they inadvertently introduce problematic patterns in their apps, like components rendering a large set of elements which can cause the memory to grow significantly.

When a customer finds itself in this situation, they generally open an issue with us that suggest there is a memory leak in the framework (which has not been the case so far) and we get into a discussion where we explain how Blazor deals with circuits, how memory works in Server GC and how to troubleshoot memory issues. The general points that people miss are:

It will be extremely helpful if we can add additional diagnostics to help bring visibility into some of the internal aspects of how Blazor server operates. Specifically:

This makes it trivial to prove that we are handling the circuit lifetime correctly (or to spot when we are not) and since all the allocated framework memory is rooted by a circuit, it makes it easy to prove that the memory issue is not on the framework.

Further diagnostics to help identify problematic circuit instances using clrMD to identify circuits consuming a large amount of memory. We can do this as follows:

With this data, we should be able to point out what components rendered a large number of elements currently or in the past as well as what circuit they belong(ed) to and its current state (whether the circuit is active, disconnected, or waiting to be collected) and it should also help discard the framework or the component rendering as the source of the memory issues.

Note that this could also be extended to use something similar to !objsize on a component instance to track how much memory that component is allocating, which in turn would help identify components in the app that are holding on to a lot of memory (think for example a component that has a large list in memory).

This last bit is not only useful for Blazor Server but it might be useful too for Blazor Webassembly and Desktop.

Another way to identify issues with the app this issue would be to check the ArrayPool<RenderTreeFrame> to identify large buffers that might have been allocated by the app in the past.

Examples of issues in this area in the past (there are likely more): https://github.com/dotnet/aspnetcore/issues/43221 https://github.com/dotnet/aspnetcore/issues/39238 https://github.com/dotnet/aspnetcore/issues/39156 https://github.com/dotnet/aspnetcore/issues/33547 https://github.com/dotnet/aspnetcore/issues/33077 https://github.com/dotnet/aspnetcore/issues/32588 https://github.com/dotnet/aspnetcore/issues/30210 https://github.com/dotnet/aspnetcore/issues/21991 https://github.com/dotnet/aspnetcore/issues/18556 https://github.com/dotnet/aspnetcore/issues/17945 https://github.com/dotnet/aspnetcore/issues/14545 https://github.com/dotnet/aspnetcore/issues/10623

ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

mkArtakMSFT commented 10 months ago

Leaving this here for now as it may help customers who come across issues where they need to investigate Blazor Server related issues: https://github.com/javiercn/BlazorDumpAnalysisTool/tree/main/BlazorDumpAnalysis

ghost commented 10 months ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.