10

In the React Native documentation for integrating with Android, it includes this snippet for integrating with Android:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mReactRootView = new ReactRootView(this);
    mReactInstanceManager = ReactInstanceManager.builder()
            .setApplication(getApplication())
            .setBundleAssetName("index.android.bundle")
            .setJSMainModulePath("index")
            .addPackage(new MainReactPackage())
            .setUseDeveloperSupport(BuildConfig.DEBUG)
            .setInitialLifecycleState(LifecycleState.RESUMED)
            .build();
    mReactRootView.startReactApplication(mReactInstanceManager, "MyReactNativeApp", null);

    setContentView(mReactRootView);
}

However, when I use this as-is I get a 100% repro memory leak caused by a ThemedReactContext holding a reference to the ReactRootView which holds a reference to my custom activity.

This is because the Context argument passed to the constructor of ReactRootView is this, which is a reference to my custom activity.

Instead, if I do:

mReactRootView = new ReactRootView(getApplication());

I get no memory leaks.

Is it safe to change the source of my context for a new ReactRootView, and is this a bug that should either a) be fixed or b) should see the documentation changed?

5
  • Interesting question. Did you find anything on this? Commented Oct 13, 2017 at 0:36
  • Indeed an interesting question. I observe the same thing -- unmounting the RN view on activity destruction does not help. This smells like a bug to me; you should consider opening an issue. Using the app context as a work-around seems to have no ill effects. Commented Oct 23, 2017 at 11:03
  • 1
    Did you find anything on this? Commented May 9, 2019 at 10:59
  • @esilver How did you test for this memory leak? I don't see this happening in newer versions (.61+) but perhaps I am not testing properly. Commented Apr 3, 2020 at 4:49
  • Perhaps this was changed in newer versions. My experience has been that if/when your Android application starts leaking whole Activities, you won't have very much time before the whole app crashes, so that was my initial clue that I was leaking. Generally, to test, I put a breakpoint in onDestroy() in the Activity and see if it gets called when the activity is closed. Commented Apr 5, 2020 at 23:07

1 Answer 1

2

It looks like the context is only used to initialize the FrameLayout. Technically passing in an application context will work to avoid the memory leak, however the styling might be messed up since "the inflation will be done with the default theme for the system in which you are running, not what's defined in your application."

See this article on different context capabilities.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.