Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detected memory leak while integrating RN app with existing android app #37105

Closed
AnandKuhar1100 opened this issue Apr 26, 2023 · 11 comments
Closed
Labels
Needs: Triage 🔍 Newer Patch Available Platform: Android Android applications. Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@AnandKuhar1100
Copy link

AnandKuhar1100 commented Apr 26, 2023

Description

I have integrated a simple RN app with android by following these steps.

I have created an android app and on its first screen I have created two CTA's to load two different bundles.
(Bundles are created using react-native bundle command).

To load bundle by clicking on any CTA, I use this code:
reactInstanceManager = ReactInstanceManager.builder() .setApplication(application) .setCurrentActivity(this) .setBundleAssetName("index.android.bundle") .setJSMainModulePath(modulePath) .addPackages(packages) .setUseDeveloperSupport(BuildConfig.DEBUG) .setInitialLifecycleState(LifecycleState.RESUMED) .setJavaScriptExecutorFactory(HermesExecutorFactory()) .build()

reactRootView?.startReactApplication(reactInstanceManager, registry, null)

setContentView(reactRootView)

This loads RN app inside android app.
I have also implemented the code inside onResume, onPause, and onDestroy methods
override fun onDestroy() { super.onDestroy() if (reactInstanceManager != null) { reactInstanceManager?.onHostDestroy(this) } if (reactRootView !== null) { reactRootView?.unmountReactApplication() } }

    override fun onPause() {
    super.onPause()
    if (reactInstanceManager != null) {
        reactInstanceManager?.onHostPause(this)
    }
}

    override fun onResume() {
    super.onResume()
    if (reactInstanceManager != null) {
        reactInstanceManager?.onHostResume(this, this)
    }
}

React Native Version

0.71.6

Output of npx react-native info

System:
OS: macOS 11.4
CPU: (8) x64 Apple M1
Memory: 132.62 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 14.19.0 - ~/.nvm/versions/node/v14.19.0/bin/node
Yarn: 1.22.19 - /opt/homebrew/bin/yarn
npm: 8.6.0 - /opt/homebrew/bin/npm
Watchman: 2022.03.21.00 - /opt/homebrew/bin/watchman
Managers:
CocoaPods: 1.11.3 - /opt/homebrew/bin/pod
SDKs:
iOS SDK:
Platforms: DriverKit 21.2, iOS 15.2, macOS 12.1, tvOS 15.2, watchOS 8.3
Android SDK:
API Levels: 23, 26, 28, 29, 30, 31, 32, 33
Build Tools: 29.0.2, 30.0.2, 30.0.3, 31.0.0, 32.0.0, 33.0.0
System Images: android-29 | Intel x86 Atom_64, android-29 | Google Play ARM 64 v8a, android-32 | Google APIs ARM 64 v8a
Android NDK: 22.1.7171670
IDEs:
Android Studio: 2022.1 AI-221.6008.13.2211.9619390
Xcode: 13.2.1/13C100 - /usr/bin/xcodebuild
Languages:
Java: 11.0.16.1 - /usr/bin/javac
npmPackages:
@react-native-community/cli: Not Found
react: 18.2.0 => 18.2.0
react-native: 0.71.7
react-native-macos: Not Found
npmGlobalPackages:
react-native: Not Found

Steps to reproduce

Simply create an android app and integrate it with RN app using steps described in official doc: Doc Link

And install LeakCanary Library to check memory Leak.
It will show a memory leak.

Snack, code example, screenshot, or link to a repository

When I run LeakCanary to check memory leaks then it show 1 memory leak:

D/LeakCanary: ┬───
D/LeakCanary: │ GC Root: Global variable in native code
D/LeakCanary: │
D/LeakCanary: ├─ com.facebook.react.bridge.JavaModuleWrapper instance
D/LeakCanary: │ Leaking: UNKNOWN
D/LeakCanary: │ Retaining 4.3 kB in 160 objects
D/LeakCanary: │ ↓ JavaModuleWrapper.mModuleHolder
D/LeakCanary: │ ~~~~~~~~~~~~~
D/LeakCanary: ├─ com.facebook.react.bridge.ModuleHolder instance
D/LeakCanary: │ Leaking: UNKNOWN
D/LeakCanary: │ Retaining 52 B in 2 objects
D/LeakCanary: │ ↓ ModuleHolder.mModule
D/LeakCanary: │ ~~~~~~~
D/LeakCanary: ├─ com.facebook.react.uimanager.UIManagerModule instance
D/LeakCanary: │ Leaking: UNKNOWN
D/LeakCanary: │ Retaining 393.2 kB in 10524 objects
D/LeakCanary: │ mReactApplicationContext instance of com.facebook.react.bridge.ReactApplicationContext, wrapping com.example.
D/LeakCanary: │ nativernintegration.MyReactApplication
D/LeakCanary: │ ↓ UIManagerModule.mUIImplementation
D/LeakCanary: │ ~~~~~~~~~~~~~~~~~
D/LeakCanary: ├─ com.facebook.react.uimanager.UIImplementation instance
D/LeakCanary: │ Leaking: UNKNOWN
D/LeakCanary: │ Retaining 169.1 kB in 3535 objects
D/LeakCanary: │ mReactContext instance of com.facebook.react.bridge.ReactApplicationContext, wrapping com.example.
D/LeakCanary: │ nativernintegration.MyReactApplication
D/LeakCanary: │ ↓ UIImplementation.mOperationsQueue
D/LeakCanary: │ ~~~~~~~~~~~~~~~~
D/LeakCanary: ├─ com.facebook.react.uimanager.UIViewOperationQueue instance
D/LeakCanary: │ Leaking: UNKNOWN
D/LeakCanary: │ Retaining 168.1 kB in 3520 objects
D/LeakCanary: │ mReactApplicationContext instance of com.facebook.react.bridge.ReactApplicationContext, wrapping com.example.
D/LeakCanary: │ nativernintegration.MyReactApplication
D/LeakCanary: │ ↓ UIViewOperationQueue.mNativeViewHierarchyManager
D/LeakCanary: │ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
D/LeakCanary: ├─ com.facebook.react.uimanager.NativeViewHierarchyManager instance
D/LeakCanary: │ Leaking: UNKNOWN
D/LeakCanary: │ Retaining 167.5 kB in 3501 objects
D/LeakCanary: │ ↓ NativeViewHierarchyManager.mTagsToViews
D/LeakCanary: │ ~~~~~~~~~~~~
D/LeakCanary: ├─ android.util.SparseArray instance
D/LeakCanary: │ Leaking: UNKNOWN
D/LeakCanary: │ Retaining 166.3 kB in 3484 objects
D/LeakCanary: │ ↓ SparseArray.mValues
D/LeakCanary: │ ~~~~~~~
D/LeakCanary: ├─ java.lang.Object[] array
D/LeakCanary: │ Leaking: UNKNOWN
D/LeakCanary: │ Retaining 165.9 kB in 3482 objects
D/LeakCanary: │ ↓ Object[22]
D/LeakCanary: │ ~~~~
D/LeakCanary: ├─ com.facebook.react.ReactRootView instance
D/LeakCanary: │ Leaking: YES (View.mContext references a destroyed activity)
D/LeakCanary: │ Retaining 1.3 kB in 21 objects
D/LeakCanary: │ View is part of a window view hierarchy
D/LeakCanary: │ View.mAttachInfo is null (view detached)
D/LeakCanary: │ View.mID = R.id.null
D/LeakCanary: │ View.mWindowAttachCount = 1
D/LeakCanary: │ mContext instance of com.example.nativernintegration.RActivity with mDestroyed = true
D/LeakCanary: │ ↓ View.mContext
D/LeakCanary: ╰→ com.example.nativernintegration.RActivity instance
D/LeakCanary: ​ Leaking: YES (ObjectWatcher was watching this because com.example.nativernintegration.RActivity received
D/LeakCanary: ​ Activity#onDestroy() callback and Activity#mDestroyed is true)
D/LeakCanary: ​ Retaining 16.0 kB in 276 objects
D/LeakCanary: ​ key = b1835ca3-333a-4368-9e8e-0c0308be47e2
D/LeakCanary: ​ watchDurationMillis = 58303
D/LeakCanary: ​ retainedDurationMillis = 53303
D/LeakCanary: ​ mApplication instance of com.example.nativernintegration.MyReactApplication
D/LeakCanary: ​ mBase instance of androidx.appcompat.view.ContextThemeWrapper

@github-actions
Copy link

⚠️ Newer Version of React Native is Available!
ℹ️ You are on a supported minor version, but it looks like there's a newer patch available. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.

@github-actions github-actions bot added the Platform: Android Android applications. label Apr 26, 2023
@kshitijGulati93
Copy link

facing the same issue, any updates?

@jsonhu
Copy link

jsonhu commented Apr 27, 2023

same issue, mark

@AnandKuhar1100
Copy link
Author

does anyone able to resolve it?

@smile-sxl
Copy link

Are there any solutions to the same problem?

@vincentmegia
Copy link

vincentmegia commented Jun 15, 2023

we are using an old version 0.64.4. we see the same problem

[Update]: Maybe as a workaround, use reactInstanceManager.destroy()

Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Dec 14, 2023
Copy link

This issue was closed because it has been stalled for 7 days with no activity.

@KingAmo
Copy link

KingAmo commented Jun 20, 2024

1
"main" prio=5 tid=1 Waiting
2
| group="main" sCount=1 ucsCount=0 flags=1 obj=0x736ebc28 self=0xb4000070ef8218c0
3
| sysTid=21519 nice=-4 cgrp=vip sched=1073741825/2 handle=0x7266a154f8
4
| state=S schedstat=( 65072994500 13543415523 87780 ) utm=5106 stm=1400 core=5 HZ=100
5
| stack=0x7fce129000-0x7fce12b000 stackSize=8188KB
6
| held mutexes=
7
at java.lang.Object.wait(Native method)
8
- waiting on <0x05eb4838> (a com.facebook.react.bridge.ModuleHolder)
9
at java.lang.Object.wait(Object.java:386)
10
at java.lang.Object.wait(Object.java:524)
11
at com.facebook.react.bridge.ModuleHolder.getModule(SourceFile:45)
12
- locked <0x05eb4838> (a com.facebook.react.bridge.ModuleHolder)
13
at com.facebook.react.bridge.NativeModuleRegistry.getModule(SourceFile:11)
14
at com.facebook.react.bridge.CatalystInstanceImpl.getNativeModule(SourceFile:5)
15
at com.facebook.react.bridge.CatalystInstanceImpl.getNativeModule(SourceFile:1)
16
at com.facebook.react.uimanager.UIManagerHelper.getUIManager(SourceFile:12)
17
at com.facebook.react.uimanager.UIManagerHelper.getUIManager(SourceFile:1)
18
at com.facebook.react.ReactInstanceManager.attachRootViewToInstance(SourceFile:34)
19
at com.facebook.react.ReactInstanceManager.attachRootView(SourceFile:33)
20
at com.facebook.react.ReactRootView.attachToReactInstanceManager(SourceFile:80)
21
at com.facebook.react.ReactRootView.onMeasure(SourceFile:153)

in my case , ANR happened with stack above, any help please?

React-Native: 0.73.8
i also integrating RN app with existing android app

@NetPumi2
Copy link

NetPumi2 commented Jul 9, 2024

we have absolutely the same issue with RN 0.69 I created StackOverflow issue

@samrombswm
Copy link

Same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Triage 🔍 Newer Patch Available Platform: Android Android applications. Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests

8 participants