react-native: Memory leak at JavaModuleWrapper.mModuleHolder

I got a memory leak on the Android platform when I launch a ReactActivity and finish it.

Description

LeakCanary report a memory leak when i launch a ReactActivity and finish it subsequently.

leakcanary

React Native version:

react-native info
F:\AwesomeProject>yarn react-native info
yarn run v1.5.1
$ F:\AwesomeProject\node_modules\.bin\react-native info
info Fetching system and libraries information...
System:
    OS: Windows 10 10.0.18362
    CPU: (4) x64 Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz
    Memory: 6.43 GB / 15.89 GB
  Binaries:
    Node: 12.18.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.5.1 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 6.14.5 - C:\Program Files\nodejs\npm.CMD
    Watchman: Not Found
  SDKs:
    Android SDK: Not Found
  IDEs:
    Android Studio: Version  3.6.0.0 AI-192.7142.36.36.6308749
  Languages:
    Java: 1.8.0_201 - C:\Program Files\Java\jdk1.8.0_201\bin\javac.EXE
    Python: 3.8.3 - C:\Python38\python.EXE
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.11.0 => 16.11.0
    react-native: 0.62.2 => 0.62.2
  npmGlobalPackages:
    *react-native*: Not Found
Done in 4.36s.

Steps To Reproduce

Provide a detailed list of steps that reproduce the issue.

  1. Create a AwesomeProject via npx react-native init AwesomeProject;
  2. Create a new activity called LauncherActivity as the launcher activity of App, and activity’s layout contains a button for click jump to MainActivity, of cause need delete the MainActivity’s intent-filter in AndroidManifest.xml;
  3. Click LauncherActivity’s button jump to MainActivity and then back to LauncherActivity and then back to system launcher, a memory leak occurs.

Expected Results

don’t leak.

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

LauncherActivity.java
package com.awesomeproject;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class LauncherActivity extends AppCompatActivity {
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_launcher);

    findViewById(R.id.button).setOnClickListener(v ->
        startActivity(new Intent(this, MainActivity.class)));
  }
}
activity_launcher.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context=".LauncherActivity">

  <Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="jump to MainActivity" />

</LinearLayout>
MainActivity.java
package com.awesomeproject;

import androidx.annotation.Nullable;

import com.facebook.react.ReactActivity;

public class MainActivity extends ReactActivity {
  @Nullable
  @Override
  protected String getMainComponentName() {
    return "AwesomeProject";
  }
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.awesomeproject">

  <uses-permission android:name="android.permission.INTERNET" />

  <application
    android:name=".MainApplication"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:theme="@style/AppTheme">
    <activity
      android:name=".LauncherActivity"
      android:label="@string/app_name"
      android:launchMode="singleTask"
      android:windowSoftInputMode="adjustResize">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity
      android:name=".MainActivity"
      android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
      android:label="@string/app_name"
      android:windowSoftInputMode="adjustResize" />
    <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
  </application>

</manifest>

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 15

Most upvoted comments

Still need to fix… exists in latest version as well.

Just adding my Analysis : I just found when we start React native app after first time then it is trying to remove the re What I found react-native view keep activity ref in mTagsToViews and trying to remove reference of last time opened React activity when we opened it subsequently next time. Ideally it should be removed when we close the application last time itself.

image