-
Notifications
You must be signed in to change notification settings - Fork 24.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leak when programmatically changing 'source' prop of Image #12220
Comments
We're cutting down on the number of outstanding issues, in order to allow us to focus. I'm closing this issue because it has been open for over 60 days with no activity. If you think it should still be opened let us know why. PRs are always welcome. |
I have this same issue. I'm trying to create an animated image sequence, but previous frames don't get deallocated resulting in massive memory issues. Additional Information: React Native version: 0.47.0 |
I spent some time investigating this. The issue seems to be related to React Native's usage of I came to this conclusion by modifying
This way, the image loading falls back to The difference here is that 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 |
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 For the high-res 'app-intro' example @jdmunro mentions, perhaps you could pass a property to |
I agree that we should not introduce any kind of heuristic algorithm here because |
Something such as @joshyhargreaves suggested could work, if we can agree on an API. I'd be happy to give this a go. I haven't confirmed whether this is an issue on Android and so this may end up being an iOS-only prop. Perhaps this could be part of the image source prop types as these already have more advanced options relating to network caching behaviours for remote images: https://github.com/facebook/react-native/blob/1e8f3b11027fe0a7514b4fc97d0798d3c64bc895/Libraries/Image/ImageSourcePropType.js |
Maybe the native iOS caching @jdmunro mentions should be enabled when the user explicitly declares the |
Was also running into this issue with remote images saved locally (to reduce Image flickering on mount/unmount). Made a reproducible demo here: snack link |
Hey, I run app in release mode and run profiler to check, if any leak occurs. Unfortunately I did not notice any unexpected behaviour. To reproduce it, I used images from RN assets, so maybe that's why I could not see any leak here. Can you provide an images, which you used to reproduce this issue ? |
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. |
RN 0.64 and still happening. |
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days. |
this is still an issue. EDIT: for anyone facing this issue in the future, react-native-fast-image does not have this memory leak. |
Can anyone confirm if this is still an issue on latest RN version 0.75.4? |
Looks like it. I've got it reproducing with this React Native ![]() Seems 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 |
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:
Solution
Presumably when the source is changed the underlying UIImage/UIImageView needs to be destroyed.
Additional Information
The text was updated successfully, but these errors were encountered: