software-mansion / react-native-reanimated

React Native's Animated library reimplemented
https://docs.swmansion.com/react-native-reanimated/
MIT License
9.09k stars 1.32k forks source link

fix: Break circular reference in `NativeProxy` and `AndroidUIScheduler` #6697

Open lukmccall opened 1 week ago

lukmccall commented 1 week ago

Summary

Breaks circular reference in NativeProxy and AndroidUIScheduler. Needs https://github.com/software-mansion/react-native-reanimated/pull/6671 to work.

NativeProxy and AndroidUIScheduler is a HybridClass with a C++ counterpart holding a global reference to the Java object. This structure creates a circular reference between C++ and Java, which the garbage collector cannot clean up. To resolve this issue, I manually removed the reference between C++ and Java by resetting the global ref during the invalidation of the NativeProxy and AndroidUIScheduler.

Test plan

I tested it with a new project using Expo (SDK 52) and with Fabric enabled. I reloaded the app several times and performed a heap dump to verify that everything was removed correctly.