react-native: Memory leak when programmatically changing 'source' prop of Image
Description
If you use an Image component in render and use the parent component’s state to manage the source prop, each time the source is changed the previous source is not deallocated. Even when the parent component is unmounted, the previous images still use memory.
Reproduction
I’ve made a simple ImageTest project that demonstrates the bug. (IMPORTANT: run app in release mode to experience the issue)
Code:
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
TouchableHighlight,
Dimensions,
} from 'react-native';
import image0 from './images/0.png'
import image1 from './images/1.png'
import image2 from './images/2.png'
import image3 from './images/3.png'
import image4 from './images/4.png'
const images = [
image0,
image1,
image2,
image3,
image4,
]
export default class ImageTest extends Component {
constructor() {
super()
this.state = ({
counter: 0,
})
}
increment = () => {
this.setState({
counter: this.state.counter + 1
})
}
render() {
const imageSource = images[this.state.counter]
return (
<View>
<Image
source={imageSource}
style={styles.image}
/>
<TouchableHighlight onPress={this.increment} style={styles.highlight}>
<Text style={styles.text}>NEXT IMAGE</Text>
</TouchableHighlight>
</View>
);
}
}
const window = Dimensions.get('window')
const styles = StyleSheet.create({
highlight: {
backgroundColor: 'yellow',
position: 'absolute',
bottom: 0,
left: 0,
},
text: {
fontSize: 50,
},
image: {
width: window.width,
height: window.height,
},
});
AppRegistry.registerComponent('ImageTest', () => ImageTest);
Solution
Presumably when the source is changed the underlying UIImage/UIImageView needs to be destroyed.
Additional Information
- React Native version: 0.41.0
- Platform: iOS
- Operating System: MacOS 10.12.3 / xcode 8.2
About this issue
- Original URL
- State: open
- Created 10 years ago
- Reactions: 17
- Comments: 18 (7 by maintainers)
I spent some time investigating this. The issue seems to be related to React Native’s usage of
[UIImage imageNamed:]to load the file.I came to this conclusion by modifying
RCTImageFromLocalAssetURLby commenting out the following lines:This way, the image loading falls back to
[UIImage imageWithContentsOfFile:]. Using this mechanism, the memory profile of cycling through the images is mostly flat as the previous image is deallocated when the next image is loaded:The difference here is that
[UIImage imageNamed:]has an internal, system managed cache of images. You can verify that you’re seeing a cached image by cycling back through to the first image: the memory usage no longer increases.The cache should release in low memory conditions. I tried simulating this using the Simulate Memory Warning in the simulator but this did not free up the memory.
According to the official Apple documentation:
Further investigation is required to determine if this counts as ‘not currently being used’ or not and whether these images (now off-screen) should be purged.
Also interesting to consider:
This suggests that images that will be displayed once, such as the background slides of an app-intro, which may well be high-resolution, should not be loaded using
[UIImage imageNamed:]because we do not want to cache them. I’m unsure how or if React Native should be able to make a decision like this and whether it makes sense for the developer to give a hint as to the purpose of the image?Looks like it. I’ve got it reproducing with this React Native
0.76.4app: https://github.com/coolsoftwaretyler/react-native-image-memory-leak-reproSeems there’s been some discussion here about the right way to handle this, but not a good resolution on a path forward.
I’d be happy to write up a PR with a proposed prop on the
Imagecomponent to allow developers to opt out of[UIImage imageNamed:]if they want to deallocate transient images. If it’s still a non-starter, no worries, I am just interested in hacking around on it a little.Still reproducible for me in RN 0.61.3. No way to clear the cache or prevent caching once loaded means inevitable crash of the device after loading a handful of images. It is pretty shocking that this is still an issue.
Thank you @jdmunro for the excellent, informative investigation, and @tomchambers2 for the demo project.
IMHO I think it should be up to the developer to decide if an image should be cached or not as I think, simply put, there’s no way for React Native to know if application is likely to use an
<img>with the same source again.For the high-res ‘app-intro’ example @jdmunro mentions, perhaps you could pass a property to
imglike so:<img noSystemCache={true} />, as we specifically know that we (as the developer) will never show that image again outside of the app-into screen in the app again.RN 0.64 and still happening. Edit: I think it’s more related to components than image only. The same issue happens with audio file.
I would love to see a PR for this.
I never heard back from anyone about whether or not this is the direction we want to go in. I don’t have the time at the moment to spin up a PR if it might not get merged, but I’d still be happy to work on it if we got some signal that it’s what the project wants.
Can anyone confirm if this is still an issue on latest RN version 0.75.4?