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
- checkout sample repository (https://github.com/vsheyanov/react-native-maps-memory)
- add Map API Key
- run the app
- run telemetry
- start clicking list items
- observe how memory consumption grows
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
@emaxedon we do have the same thing in our app -
markersmake the memory problem worse. In my reproduction steps and video I did not add markers just to isolatemap onlycase. This a memory behavior of the app when a map is rendered with a marker(s) and I’m not even touching the app: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.
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
Still relevant
Hi, is there any news about this?