question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BlazorServer doesn't release circuits/component instances after 25mins+forced GC2

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Blazor component instances and circuit objects/instances don’t seem to be released from memory after 25+ minutes and forced 2nd generation garbage collection using the basic out of the box Blazor Server template. I tried to follow the recommendations from @javiercn in this post https://github.com/dotnet/aspnetcore/issues/30210#issuecomment-779849309 to test this.

Expected Behavior

The amount of Counter instances & Circuits should go to zero after 10/20/30 minutes and a forced garbage collection ( GC.Collect(2, GCCollectionMode.Forced);).

Steps To Reproduce

  1. Create a default Blazor Server template app in Visual Studio 17.4.0 Preview 2.
  2. Go to the Counter.razor component and change the method to

private void IncrementCount() { GC.Collect(2, GCCollectionMode.Forced); }

as recommended by @javiercn here https://github.com/dotnet/aspnetcore/issues/30210#issuecomment-779849309 so we can force a GC collect after 20 minutes.

  1. Set the build mode to *Release
  2. Execute the application
  3. Click on the Counter tab button so we navigate to the counter page.
  4. Now on the counter page, press F5 (refresh page) 10 times.
  5. Open the Diagnostics Tool in VS and take a memory snapshot.
  6. Search in the snapshot for *Counter and *Circuits.circuit

image and image

So you can see there’re 11 instances (1 initial request + 10 refreshes) of the component as expected in memory, and the related circuits.

  1. Wait 10 minutes.

  2. Take a new snapshot. image image Component instances & circuits still there after 10 minutes.

  3. Wait another 10 minutes (just in case)

  4. Take a new snapshot

image image

  1. So we waited 20 minutes (the double recommended by javier), all the instances are still there… So now let’s trigger a forced GC collect using the Counter button for which we changed the code:

image

We can see a GC has occured.

  1. Take another snapshot to see if after 25+ minutes and a forced GC, Counter components & circuit instances have been released. image

image

So nothing has changed, 11 instances are still there after almost an hour, and a forced GC collection.

Just in case I forced GC a couple more times. image

Still nothing is released.

Exceptions (if any)

No response

.NET Version

7.0.100-rc.1.22431.12

Anything else?

What I’ve tried to see if this gets fixed:

 builder.Services.AddServerSideBlazor(options => {
                options.DisconnectedCircuitMaxRetained = 0;
                options.DisconnectedCircuitRetentionPeriod = TimeSpan.FromSeconds(0);
            });
  1. Changing the GC mode to Workstation.

  2. Debug & Release modes.

  3. Closed the browser completely (but left the server running).

Nothing seems to make .NET to release Counter component instances & circuits after half an hour.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
chrdlxcommented, Oct 3, 2022

You’re welcome! Blazor is great!

2reactions
willdeancommented, Oct 3, 2022

Is this behaviour which has arisen in 7.0 because of new STJ caching JsonSerializerOptions? It does seem a bit unexpected that serializer options would become a root for potentially large/numerous application data structures.

Whoever carefully used WeakReference<> on that cache to prevent it blocking GC must feel particularly galled that someone’s already found a way to trap huge amounts of data via the cache key… 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET Core Blazor state management
The server must release a disconnected circuit after a timeout or when the server is under memory pressure. In multi-server, load-balanced ...
Read more >
Preserving State in Server-Side Blazor applications
Server -side Blazor is a stateful application framework. ... It must release disconnected circuits after some timeout or when under memory pressure.
Read more >
why this error generated on blazor and how to solve it
I work on blazor app server side . i found my app stuck i don't kno why it ... Object reference not set...
Read more >
Blazor Server Update all component instances
I have multiple components that have data being fed to them, when updated in the back end right now I have a Cascading...
Read more >
Unhandled Exceptions in Blazor Server with Error ...
Let's look at error boundaries in Blazor, a new feature that allows you to handle Blazor Server unhandled exceptions more easily.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found