Crash (ios): high watermark memory limit exceeded
Which packages are you using?
stream_chat, stream_chat_flutter, stream_chat_flutter_core
On what platforms did you experience the issue?
iOS
What version are you using?
stream_chat_flutter-7.2.2
What happened?
Hello! My app crashes when there are multiple images in the channel GalleryAttachmentBuilder seems to be problem
Relevant log output
[ServicesDaemonManager] interruptionHandler is called. -[FontServicesDaemonManager connection]_block_invoke
* thread #8, name = 'io.flutter.1.ui', stop reason = EXC_RESOURCE (RESOURCE_TYPE_MEMORY: high watermark memory limit exceeded) (limit=1450 MB)
frame #0: 0x000000010d40248c
-> 0x10d40248c: stur x2, [x0, #0x27]
0x10d402490: stur x4, [x0, #0x2f]
0x10d402494: ret
0x10d402498: stp x29, x30, [x15, #-0x10]!
Target 0: (Runner) stopped.
Lost connection to device.
Flutter doctor output
[!] Flutter (Channel stable, 3.24.5, on macOS 15.4 24E248 darwin-arm64, locale fr-US)
• Flutter version 3.24.5 on channel stable at /Users/nicolas/flutter
! Warning: `dart` on your path resolves to /opt/homebrew/Cellar/dart/3.5.4/libexec/bin/dart, which is not inside your current Flutter SDK checkout at /Users/nicolas/flutter. Consider adding
/Users/nicolas/flutter/bin to the front of your path.
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision dec2ee5c1f (5 months ago), 2024-11-13 11:13:06 -0800
• Engine revision a18df97ca5
• Dart version 3.5.4
• DevTools version 2.37.3
• If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
• Android SDK at /Users/nicolas/Library/Android/sdk
• Platform android-35, build-tools 35.0.0
• ANDROID_HOME = /Users/nicolas/Library/Android/sdk
• Java binary at: /opt/homebrew/opt/openjdk@17/bin/java
• Java version OpenJDK Runtime Environment Homebrew (build 17.0.14+0)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
• Xcode at /Applications/Xcode-16.2.0.app/Contents/Developer
• Build 16C5032a
• CocoaPods version 1.16.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[!] Android Studio (not installed)
• Android Studio not found; download from https://developer.android.com/studio/index.html
(or visit https://flutter.dev/to/macos-android-setup for detailed instructions).
[✓] VS Code (version 1.99.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.109.20250401
[✓] Connected device (4 available)
...
[✓] Network resources
• All expected network resources are available.
! Doctor found issues in 2 categories.
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
@renefloor any idea, what can be the issue here?
@nbourdin are these images really large? After (approximately) how many images does the app crash? Is it on specific iOS devices, or the simulator?
Hello I had to remove about twenty images to prevent my channel from crashing on certain devices (e.g., iPhone 15). However, I’m still experiencing crashes on devices like the iPhone SE. These crashes only occur on physical iPhones, not on the simulator. The images are approximately 2–3 MB each. As for the number of images that cause the app to crash, I can’t say for sure at this point.
This might be an issue in Flutter itself
Is there a simple way to intercept image loading to convert them to JPEG?
Ok, i also try the sample app, uploading a lot of heic images and got a crash too
Hey @nbourdin , Can you also share those images here in order to test? Thanks
i uploaded the same image again and again in the sample app here is the cid : !members-Yhb4mXHsSWaSDbM1qTiJmb5IpcO05ew7JeB_3VVbHic
the crash occurs on iphone SE for exemple when i scroll in the channel
Hey @nbourdin, I tried scrolling the channel in the iPhone 16 Pro simulator and it didn't crashed. I will check later on a iphone SE to confirm.
https://github.com/user-attachments/assets/0e4a94dc-2c23-4ca6-ab8f-efb2b79bc7c4
yes, this only crashes on certain (physical) models of iphone like SE, iphone 12, etc.
Hey, have you been able to test with one of these devices?
Hi @nbourdin we've reproduced that there is a memory issue by testing on a physical iPhone SE, but it's hard to debug what exactly is going wrong. We'll try to find the root issue and keep you posted.
The root cause of this issue is a broken thumbnail mechanism for media attachments. If you inspect the app’s network traffic, images are fetched at their maximum resolution.
Older iOS devices simply can’t handle displaying multiple (e.g., more than 10) high-resolution images due to to implementation of CachedNetworkImage and hardware constraints (limited RAM)
The thumbnail mechanism doesn’t work because the StreamImageAttachmentThumbnail class has a confusing API. It exposes two properties that appear to control the thumbnail size:
/// Width of the attachment image thumbnail.
final double? width;
/// Height of the attachment image thumbnail.
final double? height;
and
/// Size of the attachment image thumbnail.
final Size? thumbnailSize;
In practice, only thumbnailSize is used when constructing the resized image URL:
if (thumbnailSize != null) {
imageUrl = imageUrl.getResizedImageUrl(
width: thumbnailSize.width,
height: thumbnailSize.height,
resize: thumbnailResizeType,
crop: thumbnailCropType,
);
}
So when other parts of the code create StreamImageAttachmentThumbnail with only width and height (for example, in GalleryAttachmentBuilder):
StreamMediaAttachmentThumbnail(
media: attachment,
width: constraints.maxWidth,
height: constraints.maxHeight,
fit: BoxFit.cover,
),
that’s not enough to make thumbnails work. There are a few other places that need to pass thumbnailSize as well (for example, StreamQuotedMessageWidget). If you update all of these call sites to provide thumbnailSize, the app should no longer crash - even with hundreds of images in a chat. Or at least to make it crash you would need to scroll more extensively and have more images in chat.
Side note 1. When testing thumbnail functionality please delete the app each time as CachedNetworkImage uses a disk cache which may interfere with results.
Side note 1. When calculating thumbnailSize it is necessary to multiply logical points with device pixel ratio obtained via MediaQuery.of(context).devicePixelRatio.
Hey @shorbenko, thanks for digging a bit deeper. We will try and fix this to see if it helps. It might take some time as i am a bit occupied with the FeedsV3 product.
Hey @nbourdin , can you try this PR and see if it solves your issue?