Skip to content
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

[firebase_storage]: Memory management issue when calling putData()/putFile() on iOS #13532

Open
1 task done
CloudMountain opened this issue Oct 22, 2024 · 0 comments
Open
1 task done
Labels
Needs Attention This issue needs maintainer attention. platform: ios Issues / PRs which are specifically for iOS. plugin: storage type: bug Something isn't working

Comments

@CloudMountain
Copy link

Is there an existing issue for this?

  • I have searched the existing issues.

Which plugins are affected?

Storage

Which platforms are affected?

iOS

Description

The now closed issue #13385, presented an issue on both Android and iOS whereby repeatedly calling

Uint8List buffer = Uint8List(5000000);   
Reference ref =  FirebaseStorage.instance.ref();   
await ref.child(path/to/file.pcm).putData(buffer);

allocates the data to be uploaded on the relevant heap and then never cleans up, which on Android was leading to an OOM error after some number of uploads (around 30-50).

A fix #13508 has been published as of firebase_storage 12.3.4 for this issue on Android, however, similar behaviour occurs on iOS. Whilst crashes seem less likely on iOS (I am assuming this is because generally iOS grants an app more RAM than Android does, as the memory footprint grows equivalently) eventually there will be a crash for someone attempting to upload a sufficiently large amount of data.

Here is a screenshot of the xcode memory profiler during which 4 buffers of 5MB each were uploaded using putData() using firebase_storage 12.3.4:
Screenshot 2024-10-22 at 14 28 20

Relevant details from the previous issue raised here:

Each call to the putData() function is allocating memory that is never freed (just to be clear in the example in the screenshot I called the upload function 4 times, waiting each time until the TaskState was TaskState.success). The stacktrace is not readable, but stepping through with the debugger I see an equivalence with Android in that the data is allocated during a call to readValueOfType(), i.e. when sending the data over from Flutter to the native side.

On iOS the situation is similar [to Android,] in the file FLTFirebaseStoragePlugin.m there is a _streamHandlers Map. Calling putData() calls referencePutDataApp(), which calls setupTaskListeners() which adds FLTTaskStateChannelStreamHandler to the _streamHandlers Map which contains a task. When creating the task the buffer to be uploaded is malloced and then never freed, and the reference to that buffer in the task in the _streamHandlers Map is never cleaned up either. Again there is a cleanupWithCompletion method which is only ever called from detachFromEngineOnRegistrar() and didReinitializeFirebaseCore().

Reproducing the issue

Create storage tasks as follows

ElevatedButton(
  onPressed: () async {
  Uint8List buffer = Uint8List(5000000);
  Reference ref =  FirebaseStorage.instance.ref();
  await ref.child('flutter-tests/putdata').putData(buffer);
},
child: const Text('Storage test'),
),

Each task allocates data that is never freed.

Firebase Core version

3.6.0

Flutter Version

3.22.2

Relevant Log Output

No response

Flutter dependencies

Expand Flutter dependencies snippet
Dart SDK 3.4.3
Flutter SDK 3.22.2

dependencies:
- android_intent_plus 5.1.0 [flutter platform meta]
- async 2.11.0 [collection meta]
- audio_session 0.1.21 [flutter flutter_web_plugins rxdart meta]
- cloud_firestore 5.2.1 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta]
- community_material_icon 5.9.55 [flutter]
- connectivity_plus 5.0.2 [flutter flutter_web_plugins connectivity_plus_platform_interface js meta nm]
- convert 3.1.1 [typed_data]
- cupertino_icons 1.0.8
- firebase_analytics 11.2.1 [firebase_analytics_platform_interface firebase_analytics_web firebase_core firebase_core_platform_interface flutter]
- firebase_auth 5.1.4 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta]
- firebase_core 3.6.0 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_crashlytics 4.0.4 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace]
- firebase_storage 12.3.4 [firebase_core firebase_core_platform_interface firebase_storage_platform_interface firebase_storage_web flutter]
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine]
- flutter_blue_plus 1.14.11 [flutter]
- flutter_hooks 0.20.5 [flutter]
- flutter_localizations 0.0.0 [flutter intl characters clock collection material_color_utilities meta path vector_math]
- flutter_sound 9.2.13 [path_provider recase uuid provider path synchronized logger flutter flutter_sound_platform_interface flutter_sound_web]
- flutter_sound_platform_interface 9.2.13 [flutter meta plugin_platform_interface logger]
- flutter_svg 2.0.10+1 [flutter http vector_graphics vector_graphics_codec vector_graphics_compiler]
- flutter_widget_from_html_core 0.15.1 [csslib flutter html logging]
- hooks_riverpod 2.5.1 [collection flutter flutter_hooks flutter_riverpod riverpod state_notifier]
- html 0.15.4 [csslib source_span]
- html_unescape 2.0.0
- http 1.2.2 [async http_parser meta web]
- internet_connection_checker_plus 2.2.0 [connectivity_plus flutter http]
- logger 1.4.0
- path_provider 2.1.4 [flutter path_provider_android path_provider_foundation path_provider_linux path_provider_platform_interface path_provider_windows]
- permission_handler 11.3.1 [flutter meta permission_handler_android permission_handler_apple permission_handler_html permission_handler_windows permission_handler_platform_interface]
- sanitize_html 2.1.0 [html meta]
- shared_preferences 2.3.1 [flutter shared_preferences_android shared_preferences_foundation shared_preferences_linux shared_preferences_platform_interface shared_preferences_web shared_preferences_windows]
- stop_watch_timer 3.2.0 [rxdart]
- url_launcher 6.3.0 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows]
- uuid 3.0.7 [crypto]
- wakelock_plus 1.2.8 [flutter flutter_web_plugins meta wakelock_plus_platform_interface win32 dbus package_info_plus web]
- webview_flutter 4.8.0 [flutter webview_flutter_android webview_flutter_platform_interface webview_flutter_wkwebview]

dev dependencies:
- flutter_launcher_icons 0.13.1 [args checked_yaml cli_util image json_annotation path yaml]
- flutter_lints 4.0.0 [lints]
- flutter_test 0.0.0 [flutter test_api matcher path fake_async clock stack_trace vector_math leak_tracker_flutter_testing async boolean_selector characters collection leak_tracker leak_tracker_testing material_color_utilities meta source_span stream_channel string_scanner term_glyph vm_service]
- mockito 5.4.4 [analyzer build code_builder collection dart_style matcher meta path source_gen test_api]

transitive dependencies:
- _fe_analyzer_shared 67.0.0 [meta]
- _flutterfire_internals 1.3.44 [collection firebase_core firebase_core_platform_interface flutter meta]
- analyzer 6.4.1 [_fe_analyzer_shared collection convert crypto glob meta package_config path pub_semver source_span watcher yaml]
- archive 3.6.1 [crypto path]
- args 2.5.0
- boolean_selector 2.1.1 [source_span string_scanner]
- build 2.4.1 [analyzer async convert crypto glob logging meta package_config path]
- built_collection 5.1.1
- built_value 8.9.2 [built_collection collection fixnum meta]
- characters 1.3.0
- checked_yaml 2.0.3 [json_annotation source_span yaml]
- cli_util 0.4.1 [meta path]
- clock 1.1.1
- cloud_firestore_platform_interface 6.3.1 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- cloud_firestore_web 4.1.1 [_flutterfire_internals cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins]
- code_builder 4.10.0 [built_collection built_value collection matcher meta]
- collection 1.18.0
- connectivity_plus_platform_interface 1.2.4 [flutter meta plugin_platform_interface]
- crypto 3.0.3 [typed_data]
- csslib 1.0.0 [source_span]
- dart_style 2.3.6 [analyzer args collection path pub_semver source_span]
- dbus 0.7.10 [args ffi meta xml]
- fake_async 1.3.1 [clock collection]
- ffi 2.1.2
- file 7.0.0 [meta path]
- firebase_analytics_platform_interface 4.2.1 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface]
- firebase_analytics_web 0.5.9+1 [_flutterfire_internals firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins]
- firebase_auth_platform_interface 7.4.7 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- firebase_auth_web 5.13.2 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser meta web]
- firebase_core_platform_interface 5.3.0 [collection flutter flutter_test meta plugin_platform_interface]
- firebase_core_web 2.18.1 [firebase_core_platform_interface flutter flutter_web_plugins meta web]
- firebase_crashlytics_platform_interface 3.6.40 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- firebase_storage_platform_interface 5.1.31 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- firebase_storage_web 3.10.3 [_flutterfire_internals async firebase_core firebase_core_web firebase_storage_platform_interface flutter flutter_web_plugins http meta web]
- fixnum 1.1.0
- flutter_riverpod 2.5.1 [collection flutter meta riverpod state_notifier]
- flutter_sound_web 9.2.13 [flutter_sound_platform_interface flutter logger flutter_web_plugins meta js]
- flutter_web_plugins 0.0.0 [flutter characters collection material_color_utilities meta vector_math]
- glob 2.1.2 [async collection file path string_scanner]
- http_parser 4.0.2 [collection source_span string_scanner typed_data]
- image 4.2.0 [archive meta xml]
- intl 0.19.0 [clock meta path]
- js 0.6.7 [meta]
- json_annotation 4.9.0 [meta]
- leak_tracker 10.0.4 [clock collection meta path vm_service]
- leak_tracker_flutter_testing 3.0.3 [flutter leak_tracker leak_tracker_testing matcher meta]
- leak_tracker_testing 3.0.1 [leak_tracker matcher meta]
- lints 4.0.0
- logging 1.2.0
- matcher 0.12.16+1 [async meta stack_trace term_glyph test_api]
- material_color_utilities 0.8.0 [collection]
- meta 1.12.0
- nested 1.0.0 [flutter]
- nm 0.5.0 [dbus]
- package_config 2.1.0 [path]
- package_info_plus 8.0.2 [ffi flutter flutter_web_plugins http meta path package_info_plus_platform_interface web win32 clock]
- package_info_plus_platform_interface 3.0.1 [flutter meta plugin_platform_interface]
- path 1.9.0
- path_parsing 1.0.1 [vector_math meta]
- path_provider_android 2.2.9 [flutter path_provider_platform_interface]
- path_provider_foundation 2.4.0 [flutter path_provider_platform_interface]
- path_provider_linux 2.2.1 [ffi flutter path path_provider_platform_interface xdg_directories]
- path_provider_platform_interface 2.1.2 [flutter platform plugin_platform_interface]
- path_provider_windows 2.3.0 [ffi flutter path path_provider_platform_interface]
- permission_handler_android 12.0.8 [flutter permission_handler_platform_interface]
- permission_handler_apple 9.4.5 [flutter permission_handler_platform_interface]
- permission_handler_html 0.1.3+2 [flutter flutter_web_plugins permission_handler_platform_interface web]
- permission_handler_platform_interface 4.2.2 [flutter meta plugin_platform_interface]
- permission_handler_windows 0.2.1 [flutter permission_handler_platform_interface]
- petitparser 6.0.2 [meta]
- platform 3.1.5
- plugin_platform_interface 2.1.8 [meta]
- provider 6.1.2 [collection flutter nested]
- pub_semver 2.1.4 [collection meta]
- recase 4.1.0
- riverpod 2.5.1 [collection meta stack_trace state_notifier]
- rxdart 0.28.0
- shared_preferences_android 2.3.1 [flutter shared_preferences_platform_interface]
- shared_preferences_foundation 2.5.1 [flutter shared_preferences_platform_interface]
- shared_preferences_linux 2.4.1 [file flutter path path_provider_linux path_provider_platform_interface shared_preferences_platform_interface]
- shared_preferences_platform_interface 2.4.1 [flutter plugin_platform_interface]
- shared_preferences_web 2.4.2 [flutter flutter_web_plugins shared_preferences_platform_interface web]
- shared_preferences_windows 2.4.1 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface]
- sky_engine 0.0.99
- source_gen 1.5.0 [analyzer async build dart_style glob path source_span yaml]
- source_span 1.10.0 [collection path term_glyph]
- stack_trace 1.11.1 [path]
- state_notifier 1.0.0 [meta]
- stream_channel 2.1.2 [async]
- string_scanner 1.2.0 [source_span]
- synchronized 3.1.0+1
- term_glyph 1.2.1
- test_api 0.7.0 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph]
- typed_data 1.3.2 [collection]
- url_launcher_android 6.3.8 [flutter url_launcher_platform_interface]
- url_launcher_ios 6.3.1 [flutter url_launcher_platform_interface]
- url_launcher_linux 3.2.0 [flutter url_launcher_platform_interface]
- url_launcher_macos 3.2.0 [flutter url_launcher_platform_interface]
- url_launcher_platform_interface 2.3.2 [flutter plugin_platform_interface]
- url_launcher_web 2.3.3 [flutter flutter_web_plugins url_launcher_platform_interface web]
- url_launcher_windows 3.1.2 [flutter url_launcher_platform_interface]
- vector_graphics 1.1.11+1 [flutter http vector_graphics_codec]
- vector_graphics_codec 1.1.11+1
- vector_graphics_compiler 1.1.11+1 [args meta path_parsing xml vector_graphics_codec path]
- vector_math 2.1.4
- vm_service 14.2.1
- wakelock_plus_platform_interface 1.2.1 [flutter plugin_platform_interface meta]
- watcher 1.1.0 [async path]
- web 1.1.0
- webview_flutter_android 3.16.6 [flutter webview_flutter_platform_interface]
- webview_flutter_platform_interface 2.10.0 [flutter meta plugin_platform_interface]
- webview_flutter_wkwebview 3.14.0 [flutter path webview_flutter_platform_interface]
- win32 5.5.3 [ffi]
- xdg_directories 1.0.4 [meta path]
- xml 6.5.0 [collection meta petitparser]
- yaml 3.1.2 [collection source_span string_scanner]

Additional context and comments

No response

@CloudMountain CloudMountain added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Oct 22, 2024
@SelaseKay SelaseKay added plugin: storage platform: ios Issues / PRs which are specifically for iOS. labels Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Attention This issue needs maintainer attention. platform: ios Issues / PRs which are specifically for iOS. plugin: storage type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants