Non-asset images, e.g. loaded remotely using uri
, can cause memory not to be released when the Image component is unmounted
#51198
Labels
Component: Image
Needs: Triage 🔍
Type: New Architecture
Issues and PRs related to new architecture (Fabric/Turbo Modules)
Description
Images loaded from a remote location, or using an alternative loader, e.g. via the Photo Roll, can cause memory usage to increase rapidly. This isn't an issue with images loaded via the
require
method. This was first discovered when using an alternative (react-native-camera-roll
) loader, but it's also an issue with images on the web, so in the core part of React Native. As far as I have seen this isn't an issue on the old architecture.After a lot of digging into the image loading mechanisms it seems that
ImageResponseObserverCoordinator
class is retaining a pointer to the image data as a cache viaimageData_
which causes the memory not to be released when the image his hidden. I could "fix" the issue by adding the following code.I don't fully understand how the
ImageResponseObserverCoordinator
so I'm sure this isn't the ultimate way to fix the problem but I just wanted to highlight the issue and that it can be improved. Here's a comparison of the two runs with and without my "fix". You can also these runs live in the linked videos below.With fix:

Without fix:

I am currently blocked from updating my app to use the new architecture because of this issue. I need to load quite a lot of high resolution images and since updating it very quickly runs out of memory.
Steps to reproduce
React Native Version
0.79.2
Affected Platforms
Runtime - iOS
Areas
Fabric - The New Renderer
Output of
npx @react-native-community/cli info
Stacktrace or Logs
MANDATORY Reproducer
https://github.com/alexfoxy/ImageMemoryTest
Screenshots and Videos
With fix: https://youtu.be/yAZ3FbNGbJs
Typical behaviour: https://youtu.be/ca2EiBO-C9I
The text was updated successfully, but these errors were encountered: