Skip to content

fix(iOS): release previous UIImage on source prop change to prevent memory leak (#12220)#57096

Open
MuhammadSuleman97 wants to merge 1 commit into
facebook:mainfrom
MuhammadSuleman97:fix/ios-image-memory-leak-source-change
Open

fix(iOS): release previous UIImage on source prop change to prevent memory leak (#12220)#57096
MuhammadSuleman97 wants to merge 1 commit into
facebook:mainfrom
MuhammadSuleman97:fix/ios-image-memory-leak-source-change

Conversation

@MuhammadSuleman97
Copy link
Copy Markdown

@MuhammadSuleman97 MuhammadSuleman97 commented Jun 6, 2026

Summary

Fixes #12220 — Memory leak when programmatically changing source prop of Image on iOS.

Problem

When the Image component's source prop changes, the UIImageView retains the previous UIImage until the new image loads. On rapid source changes (e.g., image carousels, slideshows), this causes unbounded memory accumulation.

Fix

Added _imageView.image = nil in _setStateAndResubscribeImageResponseObserver: to explicitly release the previous UIImage when the state changes. This is consistent with the existing cleanup in prepareForRecycle.

Changelog

[iOS][Fixed] - Release previous UIImage on source prop change to prevent memory leak

Test Plan

  1. Render an <Image> with a source that changes on each button press
  2. Run in Release mode on iOS
  3. Profile with Xcode Memory Graph — memory should not grow unboundedly
  4. Verify images still display correctly after source changes

…emory leak

When the Image component's source prop changes, the UIImageView retains
the previous UIImage until the new image loads. On rapid source changes
(e.g., image carousels, animations), this causes memory accumulation
since old images are not released promptly.

Explicitly setting _imageView.image = nil in
_setStateAndResubscribeImageResponseObserver: ensures the previous
UIImage is released when the state (and thus source) changes,
preventing unbounded memory growth.

Fixes facebook#12220
Copilot AI review requested due to automatic review settings June 6, 2026 14:09
@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Jun 6, 2026

Hi @MuhammadSuleman97!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR aims to reduce memory growth in Fabric RCTImageComponentView by releasing the currently displayed UIImage when resubscribing to a new image response (notably during rapid source changes).

Changes:

  • Clear _imageView.image before storing the new _state to allow the previous UIImage to be deallocated sooner.
  • Minor formatting adjustment at end of file.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +143 to 149
// Release the previous image to prevent memory accumulation
// when the source prop changes rapidly (e.g., in animations/slideshows).
// The UIImageView retains the UIImage, preventing deallocation until
// the next image arrives, which can cause significant memory growth.
_imageView.image = nil;

_state = state;
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 6, 2026

Caution

Missing Changelog

Please add a Changelog to your PR description. See Changelog format

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak when programmatically changing 'source' prop of Image

2 participants