Repository navigation
Open
Labels
Issue: Author Provided ReproThis issue can be reproduced in Snack or an attached project.This issue can be reproduced in Snack or an attached project.
Description
Description
Facing Memory Leak with React Native app integrated with existing native app.
I am currently on React Native 0.71.1 and tried to update to React Native 0.74.2.After upgrade facing memory leak. Memory usage keeps on increasing. With version 0.72 also there is no leak faced. But from version 0.73 facing memory leak.
Following this react native callstack brownfield architecture to load react native inside existing app as we have multiple entry points from native app to load react native in Fragment and also in Activity.
Steps to reproduce
- Clone the repo -
https://github.com/callstack/react-native-brownfield - Do
yarn install - Try to open the example kotlin app
- Open profiler from android studio
- Perform Action like Click on Open React Native Button
- Capture the Heapdump
React Native Version
0.74.2
Affected Platforms
Runtime - Android
Output of npx react-native info
System:
OS: macOS 14.5
CPU: (10) arm64 Apple M1 Pro
Memory: 125.97 MB / 16.00 GB
Shell:
version: "5.9"
path: /bin/zsh
Binaries:
Node:
version: 18.20.1
path: ~/.nvm/versions/node/v18.20.1/bin/node
Yarn:
version: 1.22.22
path: ~/.nvm/versions/node/v18.20.1/bin/yarn
npm:
version: 10.5.0
path: ~/.nvm/versions/node/v18.20.1/bin/npm
Watchman:
version: 2024.05.06.00
path: /opt/homebrew/bin/watchman
Managers:
CocoaPods:
version: 1.15.2
path: /opt/homebrew/bin/pod
SDKs:
iOS SDK:
Platforms:
- DriverKit 23.5
- iOS 17.5
- macOS 14.5
- tvOS 17.5
- visionOS 1.2
- watchOS 10.5
Android SDK: Not Found
IDEs:
Android Studio: 2024.1 AI-241.15989.150.2411.11948838
Xcode:
version: 15.4/15F31d
path: /usr/bin/xcodebuild
Languages:
Java:
version: 17.0.10
path: /Users/satheshrangasamy/.jenv/shims/javac
Ruby:
version: 2.7.4
path: /Users/satheshrangasamy/.rbenv/shims/ruby
npmPackages:
"@react-native-community/cli": Not Found
react:
installed: 18.2.0
wanted: 18.2.0
react-native:
installed: 0.74.2
wanted: 0.74.2
react-native-macos: Not Found
npmGlobalPackages:
"*react-native*": Not Found
Android:
hermesEnabled: true
newArchEnabled: false
iOS:
hermesEnabled: true
newArchEnabled: false
Stacktrace or Logs
Attached Memory Leak Profiler ScreenShot
Reproducer
https://github.com/satheshrgs-rksv/memoryleakreproducer
Screenshots and Videos
Reacted by Sathesh Rgs, Harsh Jain, Kannan3400, Oskar Kwaśniewski, Miguel Daipré, KingAmo, Abhishek Singh, Natesh Bhat, ottor-o, Chanphirom Sok and 5 more
Metadata
Metadata
Assignees
Labels
Issue: Author Provided ReproThis issue can be reproduced in Snack or an attached project.This issue can be reproduced in Snack or an attached project.


Activity
satheshrgs-rksv commentedon Jun 30, 2024
@cortinico can you please look into this issue.
AbhiGojoko commentedon Jul 1, 2024
Facing the same issue
harsh-rksv commentedon Jul 1, 2024
We are facing the same issue since 0.73.0. do we have any solution to this?
cortinico commentedon Jul 1, 2024
Can you try to update incrementally (first to 0.72 and then 0.73) and mention where you notice this regression?
satheshrgs-rksv commentedon Jul 1, 2024
Facing this issue from 0.73 version. 72.x works fine
12 remaining items
chanphiromsok commentedon Aug 19, 2024
I faced issue Android memory leak in pure react-native (not integration app) when running foreground still not resolve serviceshttps://github.com//issues/45001
samrombswm commentedon Aug 19, 2024
Same here
boxman0617 commentedon Aug 19, 2024
We have a hybrid app where we in our ApplicationController we are:
but then use a
ReactActivity:we use that
startActivitymethod to actually start everything.We see this memory leak come up when we go in/out of that activity
yzhe554 commentedon Aug 23, 2024
@satheshrgs-rksv May I ask if you are using new arch? We are experiencing the same issue and we use new arch.
satheshrgs-rksv commentedon Aug 23, 2024
@yzhe554 No, not using new arch
yzhe554 commentedon Aug 23, 2024
@cortinico Any update on this? I believe the issue blocks many brownfield projects.
cortinico commentedon Aug 27, 2024
Nope there are no updates here. Once we have updates, we'll post them.
At this stage, the best you folks can do is keep on investigating where the leak is.
Mentioning that "multiple leaks" are happening is not really helpful for us to debug.
Any investigation now is more than welcome.
Also better reproducers would help (like simpler apps rather than https://github.com/callstack/react-native-brownfield)
satheshrgs-rksv commentedon Aug 27, 2024
@cortinico
https://github.com/satheshrgs-rksv/memoryleakreproducer
tranhoangduong1994 commentedon Sep 19, 2024
Hi - I'm also working on a brownfield app and this memory leak has also impacted my app since upgrading to React Native 0.73.1. In my case, the problem is so severe that my app got flagged by Google Play for reaching the
App Not Respondingbad behavior threshold, which may impact the app's search results in the store.I'd like to share my research results that hopefully can help those who are experiencing the problem.
TL;DR
Reverting this commit from the React Native source seems to resolve the memory leak issue but might have an impact on apps using
ReactInstanceManager#recreateReactContextInBackground. Applying this fix may require Build from source.MY FINDINGS
I created a minimal brownfield project in order to investigate the issue. The app has only two screens/activities:
Activity#startActivityto start a new activity with the embedded RN view.I tried two RN versions 0.72.17 and 0.75.3 and noticed some important things when entering and leaving the RN screen:
createViewInstance(when entering the screen) andonDropViewInstance(when leaving the screen) are triggered for the Image component which is expected.createViewInstanceis triggered when entering the screen.onDropViewInstanceis not triggered when leaving the screen. This is clearly the sign of a memory leak.Then I investigated the code changes between 0.72.17 and 0.73.0 and I found this commit that seems very likely be the cause of the leak. While the purpose of the commit is to clean up the views in the screen's root view, it somehow prevents the cleanup process of native components inside the screen from happening. So I tried reverting the changes in the commit and could see that the cleanup process happened as expected,
onDropViewInstancewas called, and the memory leak disappeared.Since I don't understand the codebase well enough, it's hard for me to come up with a reasonable fix for the memory leak. While reverting the mentioned commit could resolve the issue, it would defeat the purpose of the commit.
I hope my research results help the React Native team more easily figure out and fix this problem. Feel free to tell me if there is anything more I can help with and I'm happy to have further discussion on this topic.
yzhe554 commentedon Sep 20, 2024
@tranhoangduong1994 Thank you! I can also verify the issue is gone after patching a revert.
Update: I just profile this again with new arch, I can still see memory leaks but just less.
chanphiromsok commentedon Sep 20, 2024
my issue still remain when running foreground service -> clear the app -> open app again MainActivity still leak
satheshrgs-rksv commentedon Nov 27, 2024
@cortinico Is #41678 related to this ?
yzhe554 commentedon Apr 16, 2025
Hi @satheshrgs-rksv , have you got this resolved?
satheshrgs-rksv commentedon Apr 17, 2025
Haven't tried updating yet
tarouboy commentedon May 12, 2025
Although I am not using brownfield but react-native-navigation, I am getting more and more ANR reports after upgrading to 0.75, I suspect the root cause is similar.