fix(iOS): release previous UIImage on source prop change to prevent memory leak (#12220)#57096
Conversation
…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
|
Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
There was a problem hiding this comment.
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.imagebefore storing the new_stateto allow the previousUIImageto be deallocated sooner. - Minor formatting adjustment at end of file.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // 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; |
Summary
Fixes #12220 — Memory leak when programmatically changing
sourceprop of Image on iOS.Problem
When the Image component's
sourceprop changes, theUIImageViewretains the previousUIImageuntil the new image loads. On rapid source changes (e.g., image carousels, slideshows), this causes unbounded memory accumulation.Fix
Added
_imageView.image = nilin_setStateAndResubscribeImageResponseObserver:to explicitly release the previous UIImage when the state changes. This is consistent with the existing cleanup inprepareForRecycle.Changelog
[iOS][Fixed] - Release previous UIImage on source prop change to prevent memory leak
Test Plan
<Image>with a source that changes on each button press