Blazor server mode: calling jsinterop to cleanup clientside before disposing component causes memory leak in page close/refresh #33535
Labels
area-blazor
Includes: Blazor, Razor Components
✔️ Resolution: Answered
Resolved because the question asked by the original author has been answered.
Status: Resolved
Description
while developing a custom blazor component, some clientside object need to be cleared before the component being disposed. below sample is a simplified version taken from blazor-university:
https://blazor-university.com/javascript-interop/calling-dotnet-from-javascript/lifetimes-and-memory-leaks/
Dispose method on the page tries to invoke a js method to clear its clientside objects, then DotnetReference is disposed.
what we have noticed is the jsinterop call throws exception while page refresh/close, because jsruntime at that moment is already closed. this exception cause all objects on serverside from the closed circuit to stay in memory. (Take memory snapshot using diagnostic tool, and check ViewModel object, the viewmodel object created from previous circuits stays in memory after page refresh, and with jsinterop call removed from Dispose method, there is always just one Viewmodel in memory.
Configuration
refer this script block in _host.html
index.razor
Regression?
I have also tried in .net 3.1 and got the same issue.
Other information
what is the correct way to dispose client and server side resources? if this is a razor component, the dispose is called from both partial render when the component is removed from render tree (in this case, page is still active so this jsinterop call works fine), or when page closed (in this case, jsinterop call throw exception, even with try catch, we still observe the locking of all the closed page's object locked by renderer root object.
The text was updated successfully, but these errors were encountered: