react-native-maps: Possible memory leak in Android

Summary

In our production app we’re constantly receiving app crash reports in Crashlytics with “out of memory” and traces of maps. I tried to reproduce the problem in a fairly simple app, but not too trivial. https://github.com/vsheyanov/react-native-maps-memory

Real use case: you have someone who needs to visit multiple addresses a day, so you have a list of visits and when you click on a specific visit, you’re redirected to a Screen with a map.

Video of a problem recorded on Android Emulator with Telemetry: https://drive.google.com/file/d/1pYOPre3yjZ8LpQCQWqw_B59QbP2JTOpV/view?usp=sharing It’s fairly long, so feel free to scroll through it, just check the memory consumed in the beginning if the video and in the end. 00:00 - 02:40 - clicking on list items, opening maps 02:40 - 04:20 - waiting if consumed memory goes down Memory consumption is doubled within 2.5 minutes.

There is a built in case just to make sure that GC does it’s job - you can render ~1000 random images instead of a map. https://drive.google.com/file/d/18K2fIKLMIny18dL0bT3rONrL176eJaIb/view?usp=sharing In this case you can see how memory consumption goes UP, but it always goes down to a level where it was before navigating to a Screen.

Main question

Is it a memory leak in the library that causes GC not to work or is it something else?

Reproducible sample code

import './gesture-handler';

import React, {  useEffect } from 'react';
import { Button, FlatList, Image, SafeAreaView, StyleSheet, Text } from 'react-native';
import { createStackNavigator } from '@react-navigation/stack';
import { NavigationContainer } from '@react-navigation/native';
import MapView, { PROVIDER_GOOGLE } from 'react-native-maps';
import { ScrollView } from 'react-native-gesture-handler';

function App({ navigation }): React.JSX.Element {
  return (
    <SafeAreaView>
      <FlatList
        data={new Array(100).fill(0).map((_, index) => index)}
        renderItem={(value) => (
          <Button title={`Item ${value.item}`} onPress={() => navigation.navigate('MemoryLeak', { value: value.item })} />
        )}/>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  map: {
    ...StyleSheet.absoluteFillObject,
    marginTop: 60,
  },
});

const Map = ({ route, navigation }) => {
  useEffect(() => {
    console.log('Map mount');
    return () => {
      console.log('Map unmount');
    };
  }, []);

  return (
    <>
      <Button title="Reset stack" onPress={() => navigation.reset({ routes: [{ name: 'Home'}]})} />
      <Text>{route.params.value}</Text>
      <MapView
        provider={PROVIDER_GOOGLE}
        style={styles.map}
        region={{
          latitude: 37.78825,
          longitude: -122.4324,
          latitudeDelta: 0.015,
          longitudeDelta: 0.0121,
        }}/>
    </>
  );
};

const ImageList = () => (
  <ScrollView>
    {
      new Array(1000).fill(0).map((_, index) => (
        <Image
          key={index}
          source={{ uri: `https://picsum.photos/200/300?random=${Math.round(Math.random() * 1000)}` }}
          style={{ width: 300, height: 400}} />
      ))
    }
  </ScrollView>
);


const Stack = createStackNavigator();

const renderMap = true;

const MainApp = () => {
  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName="Home">
        <Stack.Screen name="Home" component={App} />
        <Stack.Screen name="MemoryLeak" component={renderMap ? Map : ImageList} />
      </Stack.Navigator>
    </NavigationContainer>
  );
};

export default MainApp;

Steps to reproduce

Expected result

Consumed memory goes back when GC does it’s job.

Actual result

Consumed memory stays on the same level

React Native Maps Version

1.18.0

What platforms are you seeing the problem on?

Android

React Native Version

0.75.4

What version of Expo are you using?

Not using Expo

Device(s)

Android Emulator Pixel 7, API 33

Additional information

Updated: on the real phone this App crashes after ~1.5 minutes of clicking.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 14
  • Comments: 18

Most upvoted comments

@emaxedon we do have the same thing in our app - markers make the memory problem worse. In my reproduction steps and video I did not add markers just to isolate map only case. This a memory behavior of the app when a map is rendered with a marker(s) and I’m not even touching the app: image

For markers I’ll submit a separate issue with reproduction steps later (if no one does it yet).

I’ve included a bug report of the memory leak for Android specifically (we are not experiencing it for iOS). We believe the issue is related to custom map markers for Android.

java.lang.OutOfMemoryError: Failed to allocate a 200 byte allocation with 1052984 free bytes and 1028KB until OOM, target footprint 268435456, growth limit 268435456; giving up on allocation because <1% of heap free after GC.
        at m.eia.g(:com.google.android.gms.policy_maps_core_dynamite@243625205@243625202025.679729749.679729749:79)
        at m.eia.<init>(:com.google.android.gms.policy_maps_core_dynamite@243625205@243625202025.679729749.679729749:43)
        at m.eib.a(:com.google.android.gms.policy_maps_core_dynamite@243625205@243625202025.679729749.679729749:32)
        at m.eic.j(:com.google.android.gms.policy_maps_core_dynamite@243625205@243625202025.679729749.679729749:60)
        at m.dmq.<init>(:com.google.android.gms.policy_maps_core_dynamite@243625205@243625202025.679729749.679729749:7)
        at m.dsa.a(:com.google.android.gms.policy_maps_core_dynamite@243625205@243625202025.679729749.679729749:19)
        at com.google.maps.api.android.lib6.phoenix.cp.B(:com.google.android.gms.policy_maps_core_dynamite@243625205@243625202025.679729749.679729749:174)
        at com.google.maps.api.android.lib6.phoenix.cp.n(:com.google.android.gms.policy_maps_core_dynamite@243625205@243625202025.679729749.679729749:6)
        at com.google.maps.api.android.lib6.phoenix.cp.h(:com.google.android.gms.policy_maps_core_dynamite@243625205@243625202025.679729749.679729749:198)
        at com.google.maps.api.android.lib6.impl.es.c(:com.google.android.gms.policy_maps_core_dynamite@243625205@243625202025.679729749.679729749:24)
        at com.google.maps.api.android.lib6.impl.ej.Z(:com.google.android.gms.policy_maps_core_dynamite@243625205@243625202025.679729749.679729749:8)
        at com.google.maps.api.android.lib6.impl.ej.z(:com.google.android.gms.policy_maps_core_dynamite@243625205@243625202025.679729749.679729749:98)
        at com.google.android.gms.maps.model.internal.u.bt(:com.google.android.gms.policy_maps_core_dynamite@243625205@243625202025.679729749.679729749:356)
        at m.axu.onTransact(:com.google.android.gms.policy_maps_core_dynamite@243625205@243625202025.679729749.679729749:21)
        at android.os.Binder.transact(Binder.java:1219)
        at com.google.android.gms.internal.maps.zza.zzc(zza:2)
        at com.google.android.gms.internal.maps.zzab.zzt(zzab:3)
        at com.google.android.gms.maps.model.Marker.setIcon(Marker:2)
        at com.rnmaps.maps.MapMarker.updateMarkerIcon(MapMarker:295)
        at com.rnmaps.maps.MapMarker.updateCustomForTracking(MapMarker:287)
        at com.rnmaps.maps.ViewChangesTracker.update(ViewChangesTracker:64)
        at com.rnmaps.maps.ViewChangesTracker$1.run(ViewChangesTracker:22)
        at android.os.Handler.handleCallback(Handler.java:958)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:230)
        at android.os.Looper.loop(Looper.java:319)
        at android.app.ActivityThread.main(ActivityThread.java:8919)
        at java.lang.reflect.Method.invoke(Method.java:-2)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)

I’m also experiencing the same issue, using the default marker.

RN: 0.76.3 Expo: 52.0.7 react-native-maps: 1.20.1

 ERROR  Your app just crashed. See the error below.
java.lang.OutOfMemoryError: Failed to allocate a 4194316 byte allocation with 3668304 free bytes and 3MB until OOM
  dalvik.system.VMRuntime.newNonMovableArray(Native Method)
  android.graphics.Bitmap.nativeCreate(Native Method)
  android.graphics.Bitmap.createBitmap(Bitmap.java:879)
  android.graphics.Bitmap.createBitmap(Bitmap.java:856)
  android.graphics.Bitmap.createBitmap(Bitmap.java:823)
  com.google.maps.api.android.lib6.gmm6.vector.gl.k.a(:com.google.android.gms.dynamite_mapsdynamite@202414048@20.24.14 (040400-0):6)
  com.google.maps.api.android.lib6.gmm6.vector.gl.ai.a(:com.google.android.gms.dynamite_mapsdynamite@202414048@20.24.14 (040400-0):3)
  com.google.maps.api.android.lib6.gmm6.vector.gl.ai.a(:com.google.android.gms.dynamite_mapsdynamite@202414048@20.24.14 (040400-0):17)
  com.google.maps.api.android.lib6.gmm6.vector.gl.ai.b(:com.google.android.gms.dynamite_mapsdynamite@202414048@20.24.14 (040400-0):2)
  com.google.maps.api.android.lib6.gmm6.vector.gl.drawable.af.a(:com.google.android.gms.dynamite_mapsdynamite@202414048@20.24.14 (040400-0):10)
  com.google.maps.api.android.lib6.gmm6.vector.gl.drawable.ao.a(:com.google.android.gms.dynamite_mapsdynamite@202414048@20.24.14 (040400-0):86)
  com.google.maps.api.android.lib6.gmm6.vector.ca.a(:com.google.android.gms.dynamite_mapsdynamite@202414048@20.24.14 (040400-0):82)
  com.google.maps.api.android.lib6.gmm6.vector.bt.a(:com.google.android.gms.dynamite_mapsdynamite@202414048@20.24.14 (040400-0):179)
  com.google.maps.api.android.lib6.gmm6.vector.av.run(:com.google.android.gms.dynamite_mapsdynamite@202414048@20.24.14 (040400-0):59)

Still relevant

Hi, is there any news about this?