Open nichamp opened 2 years ago
Potential candidate for backport to 0.68 depending on the fix. But definitely a high priority fix.
@vmoroz Is this something you could look into?
@vmoroz Any ideas here?
Is this bug resolved?
is this fixed?
This has been bumped a few times, I don't think this needs to be tied to a specific release but seems like a high priority bug. Do we want to move this to Next?
Problem Description
If one attempts to destruct a ReactRootView and/or call
ReactNativeHost::UnloadInstance
, a significant amount of memory (several megabytes with either Debug or Release RNW) is not freed and accumulate each time one repeats this. This is indicative of memory leaks and can be proven by adding breakpoints to destructors in components likeHermesRuntimeHolder
.In my preliminary investigation I can see the problem is that
DevSettings
holdsstd::shared_ptr<jsi::RuntimeHolderLazyInit> jsiRuntimeHolder
andHermesRuntimeHolder
unlikeChakraRuntimeHolder
has astd::shared_ptr<facebook::react::DevSettings> m_devSettings
. With a simple fix to change the latter tostd::weak_ptr
, it will get freed properly on callingUnloadInstance
and memory usage appears to stop increasing significantly on reload.However, there is a second issue: If one doesn't explicit call
ReactNativeHost::UnloadInstance
and merely frees the last pointer to ReactRootView, neither it or ReactNativeHost (or the JS runtime of course) will destruct either. So there is also a circular references with those objects. This is likely becauseUIManager::removeRootView
doesn't get called to free the strong ref toReactRootView
held be a shadow node and consequently, itsReactNative::ReactNativeHost m_reactNativeHost
doesn't get cleared. I am not certain where the best blace to break the circular reference chain with a winrt::weak_ref or etc. would be. If is isn't possible, documentation and examples should make it very clear that the expectation is to callUnloadInstance
.Steps To Reproduce
Expected Results
All RNW and JSI resources should be freed on destructing the last references to the WinRT COM pointers (e.g. by removing from XAML tree) or by explicitly attempting to destruct with UnloadInstance
If possible, RNW should have automated tests to try to catch memory leaks by consecutively reloading one or more different bundles.
CLI version
6.3.1
Environment
Target Platform Version
10.0.19041
Target Device(s)
Desktop
Visual Studio Version
Visual Studio 2019
Build Configuration
Release
Snack, code example, screenshot, or link to a repository
No response