cfug / dio

A powerful HTTP client for Dart and Flutter, which supports global settings, Interceptors, FormData, aborting and canceling a request, files uploading and downloading, requests timeout, custom adapters, etc.
https://dio.pub
MIT License
12.44k stars 1.51k forks source link

Download large file using get crashes app #279

Closed c-mendoza closed 5 years ago

c-mendoza commented 5 years ago

Issue Info

Info Value
Platform Name flutter / Android
Platform Version 1.5.3 / 9
Dio Version latest
Repro rate all the time (100%)
Repro with our demo prj Use your download example with the download2 method

Issue Description and Steps

Dio crashes any app that downloads large files using Dio.get. The Dio.download method works. I'm currently trying to download a ~350MB file and it consistently crashes at ~78% download completion.

It is reproducible using your download example, just use the download2(...) method and point it to a large enough file.

The crash never produces a stack trace while testing on my hardware, but the simulator outputs many of these errors before crashing:

E/DartVM  (16998): Exhausted heap space, trying to allocate 536870920 bytes.
E/flutter (16998): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Out of Memory
E/flutter (16998): #0      new _List (dart:core-patch/array.dart)
E/flutter (16998): #1      List._allocateData (dart:core-patch/growable_array.dart:240:16)
E/flutter (16998): #2      List._grow (dart:core-patch/growable_array.dart:247:19)
E/flutter (16998): #3      List.addAll (dart:core-patch/growable_array.dart:178:9)
E/flutter (16998): #4      DefaultTransformer.transformResponse.<anonymous closure> (package:dio/src/transformer.dart:113:27)
E/flutter (16998): #5      _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter (16998): #6      _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (16998): #7      _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (16998): #8      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
E/flutter (16998): #9      _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
E/flutter (16998): #10     DefaultTransformer.transformResponse.<anonymous closure> (package:dio/src/transformer.dart:55:29)
E/flutter (16998): #11     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart)
E/flutter (16998): #12     _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter (16998): #13     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (16998): #14     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (16998): #15     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
E/flutter (16998): #16     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
E/flutter (16998): #17     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:132:11)
E/flutter (16998): #18     _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter (16998): #19     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (16998): #20     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (16998): #21     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
E/flutter (16998): #22     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
E/flutter (16998): #23     _StreamController._add (dart:async/stream_controller.dart:764:19)
E/flutter (16998): #24     _HttpParser._doParse (dart:_http/http_parser.dart:662:35)
E/flutter (16998): #25     _HttpParser._parse (dart:_http/http_parser.dart:318:7)
E/flutter (16998): #26     _HttpParser._onData (dart:_http/http_parser.dart:810:5)
E/flutter (16998): #27     _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter (16998): #28     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (16998): #29     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (16998): #30     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
E/flutter (16998): #31     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
E/flutter (16998): #32     _StreamController._add (dart:async/stream_controller.dart:764:19)
E/flutter (16998): #33     _Socket._onData (dart:io-patch/socket_patch.dart:693:33)
E/flutter (16998): #34     _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter (16998): #35     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (16998): #36     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (16998): #37     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
E/flutter (16998): #38     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
E/flutter (16998): #39     _StreamController._add (dart:async/stream_controller.dart:764:19)
E/flutter (16998): #40     _StreamController.add (dart:async/stream_controller.dart:586:5)
E/flutter (16998): #41     new _RawSocket.<anonymous closure> (dart:io-patch/socket_patch.dart:1323:33)
E/flutter (16998): #42     _NativeSocket.issueReadEvent.issue (dart:io-patch/socket_patch.dart)
E/flutter (16998): #43     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (16998): #44     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
c-mendoza commented 5 years ago

Also, the issue does not show up in Dio 2.0.4

wendux commented 5 years ago

Downloading large files should use dio.download() instead of download2(), because download2() will save the whole file content in RAM before the file downloading end, which may exhaust heap space.

c-mendoza commented 5 years ago

Good to know, closing the issue, thanks.

tva77 commented 2 years ago

I'm using dio.download() and the app crashes downloading about 25% of a 300MB file. dio.download( link, filePath, onReceiveProgress: showDownloadProgress, deleteOnError: true, cancelToken: cancelToken, )

OutOfMemory error loading stream E/LoadTask(15348): java.lang.OutOfMemoryError: Failed to allocate a 65552 byte allocation with 64672 free bytes and 63KB until OOM, target footprint 536870912, growth limit 536870912