Detecting Memory Pressure/GC on Javascript side #741
Unanswered
SerratedSharp
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Thanks for the feedback! There aren't many ways to track memory pressure on javascript and aside from "FinalizationRegistry" (Which is already being used in .NET 8), the .NET runtime would probably need to add some sort of browser-specific tracking since there's no standard way to do determine if memory should be collected. Still, this would be a very good issue to open in https://github.com/dotnet/runtime! Let us know when you've done it, it'll be very interesting to track. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm wondering if y'all have had to deal with/tackle detecting JS memory pressure or garbage collections on the javascript side. I've looked around at some of the memory related APIs and discussions and didn't see anything that was an actual event or way to detect that JS is close to memory pressure triggering a GC.
When thinking about C# objects wrapping JS objects which are stored in the activeInstances array, at any given time you might have a large number of C# objects that are no longer referenced and are eligible for garbage collection, but due to a lack of memory pressure on the C# side then a GC may not be triggered. This means JS activeInstances that aren't really needed anymore cannot be GC'd, and could cause JS side to run out of memory.
This is really the classic problem of C# objects owning unmanaged resources. I recognize a better practice is to use
using
blocks and deterministic disposal, but that imposes alot of restrictions on how you approach coding things, and is usually only necessary because there aren't good mechanisms to detect "pressure" on unmanaged resources such as sockets, file handles, DB connections, etc..Since we have pretty good memory management on both sides it seems like triggering a C# GC whenever there is JS memory pressure will ensure unused C# objects are deallocated, and thus remove the activeInstances references of JS objects no longer needed, making them eligible for GC on the javascript side. It would basically communicate from the JS side "hey, looks like I'm needing memory soon" and C# would say "let me try freeing up some of my objects because that might free up some JS activeInstances". Forcing C# GC's is generally frowned upon because it pushes objects into lower generations, but I think if we are linking it to specifically memory pressure then it seems like that fits within the intended usage.
Not necessarily suggesting this be part of Uno Bootstrap, but hoping if you have pre-existing knowledge you can point me in the right direction.
Beta Was this translation helpful? Give feedback.
All reactions