flutter_image_compress icon indicating copy to clipboard operation
flutter_image_compress copied to clipboard

Out memory crash in compress big image.

Open CaiJingLong opened this issue 4 years ago • 9 comments

@ROTGP (wrong ping, sorry :x @CaiJingLong ) Here's a minimal example. Select a big, wallpaper-size image, preferably several of them, hit multiply a couple times and then start the compression.

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:image_picker/image_picker.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<List<int>> _images = [];
  List<List<int>> _compressed = [];

  void _pickImage() async {
    File file = await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() => _images.add(file.readAsBytesSync()));
  }

  void _multiply() {
    setState(() => _images.addAll(List.of(_images)));
  }

  void _compress() async {
    for (List<int> image in _images) {
      List<int> compressed = await FlutterImageCompress.compressWithList(image,
          minHeight: 1000, minWidth: 1000);
      setState(() => _compressed.add(compressed));
    }
  }

  void _clear() {
    setState(() {
      _images = [];
      _compressed = [];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Selected: ${_images.length} Images',
            ),
            RaisedButton(
              onPressed: _pickImage,
              child: Text('Pick Image'),
            ),
            RaisedButton(
              onPressed: _multiply,
              child: Text('Multiply Selected'),
            ),
            Text(
              'Compressed: ${_compressed.length} Images',
            ),
            RaisedButton(
              onPressed: _compress,
              child: Text('Compress'),
            ),
            RaisedButton(
              onPressed: _clear,
              child: Text('Clear'),
            )
          ],
        ),
      ),
    );
  }
}

However, I'm not sure if this is the crash, or if this code just crudely forces the app to run out of memory.

Edit: If your image is big enough, you may raise the minWidth/minHeight values a bit. I had reliable results with 3-5 4000x6000 images, with minWidth/minHeight 3000. Note that the multiply button doesn't make deep copies, so it's not quite the same as selecting the same image multiple times.

Edit 2: I'm pretty sure now that this is not just a simple out of memory (on flutter's end). When running on 64 (shallow copied) small images, memory usage on my device can rise up to 500MB before crashing. When compressing 3-5 wallpaper-sized images, it crashes somewhere in the range of 300MB, when it still had memory left for the taking.

Originally posted by @MCOfficer in https://github.com/OpenFlutter/flutter_image_compress/issues/22#issuecomment-521560126

CaiJingLong avatar Aug 16 '19 00:08 CaiJingLong

to follow up on this, the proposed workaround for #22 (waiting for 300ms between compressions) didn't work for me, not even with 1000ms.

MCOfficer avatar Aug 19 '19 08:08 MCOfficer

I finally managed to get a crashlog using the code I gave you:

Date/Time:       2019-08-19 15:10:42 +0200
End time:        2019-08-19 15:10:51 +0200
OS Version:      iPhone OS 12.3.1 (Build 16F203)
Architecture:    arm64
Report Version:  28
Incident Identifier: 4AEF125C-FA0D-4D15-9793-EFCABE026451

Data Source:     Microstackshots
Shared Cache:    0x9aea0000 75FB4EB6-546C-3A07-9C55-CD2C30D1E9DE

Command:         Runner
Path:            /private/var/containers/Bundle/Application/694C0C3D-73DA-4D83-BC4F-3162D15EBF85/Runner.app/Runner
Identifier:      me.mcofficer.iosCompressCrashMrr
Version:         1.0.0 (1)
Beta Identifier: 58424409-414E-4492-9622-5F943995EBD5
PID:             6340

Event:           wakeups
Action taken:    none
Wakeups:         45001 wakeups over the last 188 seconds (240 wakeups per second average), exceeding limit of 150 wakeups per second over 300 seconds
Wakeups limit:   45000
Limit duration:  300s
Wakeups caused:  45001
Duration:        8.09s
Steps:           7

Hardware model:  iPhone7,2
Active cpus:     2
Boot args:        


Heaviest stack for the target process:
  4  ??? (libdyld.dylib + 6368) [0x21b7b68e0]
  4  ??? (Runner + 20544) [0x100c81040]
  4  ??? (UIKitCore + 9161080) [0x248527978]
  4  ??? (GraphicsServices + 42908) [0x21def179c]
  4  ??? (CoreFoundation + 671920) [0x21bcf10b0]
  2  ??? (CoreFoundation + 674644) [0x21bcf1b54]
  2  ??? (CoreFoundation + 695324) [0x21bcf6c1c]
  2  ??? (libdispatch.dylib + 57348) [0x21b753004]
  2  ??? (libdispatch.dylib + 395220) [0x21b7a57d4]
  2  ??? (libdispatch.dylib + 391736) [0x21b7a4a38]
  2  ??? (Photos + 693672) [0x22a6c45a8]
  2  ??? (<DF1BD73E-C223-36AE-B1C3-66818F6AAFD7> + 45052) [0x102baaffc]
  2  ??? (<DF1BD73E-C223-36AE-B1C3-66818F6AAFD7> + 45988) [0x102bab3a4]
  2  ??? (<DF1BD73E-C223-36AE-B1C3-66818F6AAFD7> + 32388) [0x102ba7e84]
  1  ??? (<DF1BD73E-C223-36AE-B1C3-66818F6AAFD7> + 33552) [0x102ba8310]
  1  ??? (<DF1BD73E-C223-36AE-B1C3-66818F6AAFD7> + 30412) [0x102ba76cc]
  1  ??? (ImageIO + 1283876) [0x21e07a724]
  1  ??? (ImageIO + 1386060) [0x21e09364c]
  1  ??? (ImageIO + 1383572) [0x21e092c94]
  1  ??? (AppleJPEG + 225740) [0x21df351cc]
  1  ??? (AppleJPEG + 21832) [0x21df03548]
  1  ??? (AppleJPEG + 20756) [0x21df03114]
  1  ??? (AppleJPEG + 15252) [0x21df01b94]


Powerstats for:  Runner [6340]
Bundle ID:       me.mcofficer.iosCompressCrashMrr
Adam ID:         0
Is first party:  No
App version:     1.0.0
Build version:   1
Is Beta:         No
Share with Devs: No
UUID:            7A4EC2FE-9334-3491-8222-1B615846FF47
Path:            /private/var/containers/Bundle/Application/694C0C3D-73DA-4D83-BC4F-3162D15EBF85/Runner.app/Runner
Architecture:    arm64
Parent:          UNKNOWN [1]
UID:             501
Footprint:       1740.34 MB -> 780.64 MB (-959.70 MB)
Pageins:         42 pages
Start time:      2019-08-19 15:10:42 +0200
End time:        2019-08-19 15:10:51 +0200
Num samples:     7 (100%)
CPU Time:        3.649s
Primary state:   3 samples Frontmost App, Non-Suppressed, User mode, Effective Thread QoS User Interactive, Requested Thread QoS User Interactive, Override Thread QoS Unspecified
User Activity:   0 samples Idle, 0 samples Active, 7 samples Unknown
Power Source:    0 samples on Battery, 0 samples on AC, 7 samples Unknown
  4  start + 3 (libdyld.dylib + 6368) [0x21b7b68e0]
    4  ??? (Runner + 20544) [0x100c81040]
      4  UIApplicationMain + 211 (UIKitCore + 9161080) [0x248527978]
        4  GSEventRunModal + 103 (GraphicsServices + 42908) [0x21def179c]
          4  CFRunLoopRunSpecific + 435 (CoreFoundation + 671920) [0x21bcf10b0]
            2  __CFRunLoopRun + 1923 (CoreFoundation + 674644) [0x21bcf1b54]
              2  __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 11 (CoreFoundation + 695324) [0x21bcf6c1c]
                2  _dispatch_main_queue_callback_4CF$VARIANT$mp + 1067 (libdispatch.dylib + 57348) [0x21b753004]
                  2  _dispatch_client_callout + 15 (libdispatch.dylib + 395220) [0x21b7a57d4]
                    2  _dispatch_call_block_and_release + 23 (libdispatch.dylib + 391736) [0x21b7a4a38]
                      2  __65-[PHImageManager requestImageDataForAsset:options:resultHandler:]_block_invoke_2 + 679 (Photos + 693672) [0x22a6c45a8]
                        2  ??? (<DF1BD73E-C223-36AE-B1C3-66818F6AAFD7> + 45052) [0x102baaffc]
                          2  ??? (<DF1BD73E-C223-36AE-B1C3-66818F6AAFD7> + 45988) [0x102bab3a4]
                            2  ??? (<DF1BD73E-C223-36AE-B1C3-66818F6AAFD7> + 32388) [0x102ba7e84]
                              1  ??? (<DF1BD73E-C223-36AE-B1C3-66818F6AAFD7> + 33552) [0x102ba8310]
                                1  ??? (<DF1BD73E-C223-36AE-B1C3-66818F6AAFD7> + 30412) [0x102ba76cc]
                                  1  IIOImageDestination::finalize + 1583 (ImageIO + 1283876) [0x21e07a724]
                                    1  IIO_Writer_AppleJPEG::write + 55 (ImageIO + 1386060) [0x21e09364c]
                                      1  AppleJPEGWritePlugin::writeOne + 1287 (ImageIO + 1383572) [0x21e092c94]
                                        1  applejpeg_encode_image_row + 579 (AppleJPEG + 225740) [0x21df351cc]
                                          1  aj_encode_row + 179 (AppleJPEG + 21832) [0x21df03548]
                                            1  aj_encode_buffers_baseline + 51 (AppleJPEG + 20756) [0x21df03114]
                                              1  aj_block_dct + 764 (AppleJPEG + 15252) [0x21df01b94]
                              1  ??? (<DF1BD73E-C223-36AE-B1C3-66818F6AAFD7> + 33492) [0x102ba82d4]
                                1  ??? (<DF1BD73E-C223-36AE-B1C3-66818F6AAFD7> + 30812) [0x102ba785c]
                                  1  _UIImageJPEGRepresentation + 643 (UIKitCore + 5517376) [0x2481ae040]
                                    1  IIOImageDestination::finalize + 1583 (ImageIO + 1283876) [0x21e07a724]
                                      1  IIO_Writer_AppleJPEG::write + 55 (ImageIO + 1386060) [0x21e09364c]
                                        1  AppleJPEGWritePlugin::writeOne + 1287 (ImageIO + 1383572) [0x21e092c94]
                                          1  applejpeg_encode_image_row + 579 (AppleJPEG + 225740) [0x21df351cc]
                                            1  aj_encode_row + 179 (AppleJPEG + 21832) [0x21df03548]
                                              1  aj_encode_buffers_baseline + 51 (AppleJPEG + 20756) [0x21df03114]
                                                1  aj_block_encode_ac + 292 (AppleJPEG + 218700) [0x21df3364c]
            2  __CFRunLoopRun + 1843 (CoreFoundation + 674564) [0x21bcf1b04]
              2  __CFRunLoopDoTimers + 247 (CoreFoundation + 695220) [0x21bcf6bb4]
                2  __CFRunLoopDoTimer + 863 (CoreFoundation + 697216) [0x21bcf7380]
                  2  __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 27 (CoreFoundation + 697936) [0x21bcf7650]
                    2  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 229820) [0x100da01bc]
                      2  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 210884) [0x100d9b7c4]
                        2  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 531788) [0x100de9d4c]
                          2  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 155800) [0x100d8e098]
                            2  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 139128) [0x100d89f78]
                              2  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 553704) [0x100def2e8]
                                2  ??? (<C31962C8-49A2-3318-8FCE-1D95D4146775> + 38564) [0x102b2d6a4]
                                  2  _dispatch_lane_barrier_sync_invoke_and_complete + 55 (libdispatch.dylib + 60440) [0x21b753c18]
                                    2  _dispatch_client_callout + 15 (libdispatch.dylib + 395220) [0x21b7a57d4]
                                      2  ??? (<C31962C8-49A2-3318-8FCE-1D95D4146775> + 38880) [0x102b2d7e0]
                                        2  ??? (<C31962C8-49A2-3318-8FCE-1D95D4146775> + 37500) [0x102b2d27c]
                                          2  ??? (<C31962C8-49A2-3318-8FCE-1D95D4146775> + 35560) [0x102b2cae8]
                                            1  ??? (<C31962C8-49A2-3318-8FCE-1D95D4146775> + 36132) [0x102b2cd24]
                                              1  ??? (<C31962C8-49A2-3318-8FCE-1D95D4146775> + 36648) [0x102b2cf28]
                                                1  _UIImageJPEGRepresentation + 643 (UIKitCore + 5517376) [0x2481ae040]
                                                  1  IIOImageDestination::finalize + 1583 (ImageIO + 1283876) [0x21e07a724]
                                                    1  IIO_Writer_AppleJPEG::write + 55 (ImageIO + 1386060) [0x21e09364c]
                                                      1  AppleJPEGWritePlugin::writeOne + 1287 (ImageIO + 1383572) [0x21e092c94]
                                                        1  applejpeg_encode_image_row + 579 (AppleJPEG + 225740) [0x21df351cc]
                                                          1  aj_encode_row + 179 (AppleJPEG + 21832) [0x21df03548]
                                                            1  aj_encode_buffers_baseline + 51 (AppleJPEG + 20756) [0x21df03114]
                                                              1  aj_block_encode_ac + 1335 (AppleJPEG + 219744) [0x21df33a60]
                                                                1  aj_ostream_write + 391 (AppleJPEG + 78332) [0x21df111fc]
                                                                  1  iioWriteCallback + 39 (ImageIO + 353184) [0x21df973a0]
                                                                    1  IIOImageWriteSession::putBytes + 183 (ImageIO + 515188) [0x21dfbec74]
                                                                      1  -[NSConcreteMutableData appendBytes:length:] + 435 (Foundation + 107592) [0x21c6d1448]
                                                                        1  _NSMutableDataGrowBytes + 315 (Foundation + 852800) [0x21c787340]
                                                                          1  __CFSafelyReallocate + 27 (CoreFoundation + 324268) [0x21bc9c2ac]
                                                                            1  realloc + 195 (libsystem_malloc.dylib + 67412) [0x21b948754]
                                                                              1  malloc_zone_realloc + 167 (libsystem_malloc.dylib + 65532) [0x21b947ffc]
                                                                                1  szone_realloc + 431 (libsystem_malloc.dylib + 29348) [0x21b93f2a4]
                                                                                  1  large_try_realloc_in_place + 155 (libsystem_malloc.dylib + 26796) [0x21b93e8ac]
                                                                                    1  vm_allocate + 35 (libsystem_kernel.dylib + 136712) [0x21b900608]
                                                                                      1  _kernelrpc_mach_vm_allocate_trap + 8 (libsystem_kernel.dylib + 98320) [0x21b8f7010]
                                                                                        1  <Kernel mode>
                                            1  ??? (<C31962C8-49A2-3318-8FCE-1D95D4146775> + 35992) [0x102b2cc98]
                                              1  ??? (<C31962C8-49A2-3318-8FCE-1D95D4146775> + 131372) [0x102b4412c]
                                                1  _UIGraphicsBeginImageContextWithOptions + 287 (UIKitCore + 5424488) [0x248197568]
                                                  1  CGContextClearRect + 79 (CoreGraphics + 2207572) [0x21daabf54]
                                                    1  CGContextFillRect + 51 (CoreGraphics + 2207144) [0x21daabda8]
                                                      1  CGContextFillRects + 103 (CoreGraphics + 2207000) [0x21daabd18]
                                                        1  ripc_DrawRects + 311 (CoreGraphics + 2174972) [0x21daa3ffc]
                                                          1  ripc_Render + 283 (CoreGraphics + 2166880) [0x21daa2060]
                                                            1  RIPLayerBltShape + 1091 (CoreGraphics + 2872092) [0x21db4e31c]
                                                              1  argb32_mark + 5319 (CoreGraphics + 2251300) [0x21dab6a24]
                                                                1  _platform_memset + 164 (libsystem_platform.dylib + 34324) [0x21b976614]
  3  _pthread_start + 43 (libsystem_pthread.dylib + 45600) [0x21b984220]
    3  _pthread_body + 127 (libsystem_pthread.dylib + 45760) [0x21b9842c0]
      3  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 227320) [0x100d9f7f8]
        3  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 210688) [0x100d9b700]
          3  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 230112) [0x100da02e0]
            3  CFRunLoopRunSpecific + 435 (CoreFoundation + 671920) [0x21bcf10b0]
              3  __CFRunLoopRun + 1843 (CoreFoundation + 674564) [0x21bcf1b04]
                3  __CFRunLoopDoTimers + 247 (CoreFoundation + 695220) [0x21bcf6bb4]
                  3  __CFRunLoopDoTimer + 863 (CoreFoundation + 697216) [0x21bcf7380]
                    3  __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 27 (CoreFoundation + 697936) [0x21bcf7650]
                      3  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 229820) [0x100da01bc]
                        2  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 210892) [0x100d9b7cc]
                          2  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 214348) [0x100d9c54c]
                            2  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 3340892) [0x101097a5c]
                              2  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 7751348) [0x1014cc6b4]
                                2  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 4480404) [0x1011add94]
                                  1  _sigtramp + 0 (libsystem_platform.dylib + 31156) [0x21b9759b4]
                                    1  <Effective Thread QoS Default, Requested Thread QoS Default>
                                  1  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 5864804) [0x1012ffd64]
                                    1  <Effective Thread QoS Default, Requested Thread QoS Default>
                        1  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 210852) [0x100d9b7a4]
                          1  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 213320) [0x100d9c148]
                            1  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 220732) [0x100d9de3c]
                              1  ??? (<192543C6-73A9-3C79-ACC9-F9913B01BFA8> + 28632) [0x100d6efd8]
                                1  <Effective Thread QoS Default, Requested Thread QoS Default>

  Binary Images:
           0x100c7c000 -                ???  Runner                   <7A4EC2FE-9334-3491-8222-1B615846FF47>  /private/var/containers/Bundle/Application/694C0C3D-73DA-4D83-BC4F-3162D15EBF85/Runner.app/Runner
           0x100d68000 -                ???  ???                      <192543C6-73A9-3C79-ACC9-F9913B01BFA8>
           0x102b24000 -                ???  ???                      <C31962C8-49A2-3318-8FCE-1D95D4146775>
           0x102ba0000 -                ???  ???                      <DF1BD73E-C223-36AE-B1C3-66818F6AAFD7>
           0x21b745000 -        0x21b7b4fff  libdispatch.dylib        <94DCBC89-BACB-39BE-ADFF-CCEA74E7BDB6>  /usr/lib/system/libdispatch.dylib
           0x21b7b5000 -        0x21b7defff  libdyld.dylib            <39EE943A-D0CA-3011-8051-9D4A1173734F>  /usr/lib/system/libdyld.dylib
           0x21b8df000 -        0x21b909fff  libsystem_kernel.dylib   <CC9F9508-B5BF-3478-A442-F3CEC1591804>  /usr/lib/system/libsystem_kernel.dylib
           0x21b938000 -        0x21b95afff  libsystem_malloc.dylib   <3CF79822-26B0-3974-9F8E-EBD3F328C63E>  /usr/lib/system/libsystem_malloc.dylib
           0x21b96e000 -        0x21b978fff  libsystem_platform.dylib <8B5DD1F4-CD3C-3C99-B291-1F7290DDF050>  /usr/lib/system/libsystem_platform.dylib
           0x21b979000 -        0x21b989fff  libsystem_pthread.dylib  <BEB13D83-11CD-3AA1-A18D-EA822DA57C69>  /usr/lib/system/libsystem_pthread.dylib
           0x21bc4d000 -        0x21bfa9fff  CoreFoundation           <8F67863F-A23E-3771-90D7-993168740877>  /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
           0x21c6b7000 -        0x21c9a1fff  Foundation               <25293172-DF9F-3FB9-9E4F-FD4EA250557E>  /System/Library/Frameworks/Foundation.framework/Foundation
           0x21d891000 -        0x21de35fff  CoreGraphics             <D0EFDA4F-EBFD-32BE-87AF-4CA6191FA356>  /System/Library/Frameworks/CoreGraphics.framework/CoreGraphics
           0x21dee7000 -        0x21def9fff  GraphicsServices         <E8535E85-7A0C-3684-8305-1D66703EC391>  /System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices
           0x21defe000 -        0x21df40fff  AppleJPEG                <47F91456-9162-375C-85D8-58D2EDF92463>  /System/Library/PrivateFrameworks/AppleJPEG.framework/AppleJPEG
           0x21df41000 -        0x21e4ddfff  ImageIO                  <D93107D7-853E-394A-A205-05D1DBF6FACF>  /System/Library/Frameworks/ImageIO.framework/ImageIO
           0x22a61b000 -        0x22a807fff  Photos                   <C2409886-FA8A-3AEA-BCFF-CB28E45D7057>  /System/Library/Frameworks/Photos.framework/Photos
           0x247c6b000 -        0x248d4ffff  UIKitCore                <40A93E93-9F86-35C1-905C-7B947C7C2305>  /System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore

MCOfficer avatar Aug 19 '19 13:08 MCOfficer

Thx for report, I will try to find a solution as soon as possible.

CaiJingLong avatar Aug 20 '19 01:08 CaiJingLong

Same Problem! 😕 We have over 8k users and a lot of them have this issue :/

nilsreichardt avatar Aug 23 '19 13:08 nilsreichardt

Any updates?

nilsreichardt avatar Dec 23 '19 12:12 nilsreichardt

I tried to use the Example to trigger this problem and found that the cause of the crash was not caused by this library. The main memory pressure came from Dart's List<int>.

And I edit some code to reproduce it.

Code:

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:image_picker/image_picker.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<File> _images = [];
  List<List<int>> _compressed = [];

  void _pickImage() async {
    File file = await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() {
      _images.add(file);
    });
  }

  void _multiply() {
    setState(() => _images.addAll(List.of(_images)));
  }

  void _compress() async {
    for (File image in _images) {
      List<int> compressed = await FlutterImageCompress.compressWithFile(
          image.absolute.path,
          minHeight: 1000,
          minWidth: 1000);
      setState(() => _compressed.add(compressed));
    }
  }

  void _clearImage() {
    setState(() {
      _images.clear();
    });
  }

  void _clearCompressed() {
    setState(() {
      _compressed.clear();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Selected: ${_images.length} Images',
            ),
            RaisedButton(
              onPressed: _pickImage,
              child: Text('Pick Image'),
            ),
            RaisedButton(
              onPressed: _multiply,
              child: Text('Multiply Selected'),
            ),
            RaisedButton(
              onPressed: _clearImage,
              child: Text('Clear images'),
            ),
            Text(
              'Compressed: ${_compressed.length} Images',
            ),
            RaisedButton(
              onPressed: _compress,
              child: Text('Compress'),
            ),
            RaisedButton(
              onPressed: _clearCompressed,
              child: Text('Clear Compress'),
            )
          ],
        ),
      ),
    );
  }
}

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2
  flutter_image_compress: ^0.6.3
  image_picker: ^0.6.2+3

My env:

[✓] Flutter (Channel dev, v1.13.5, on Mac OS X 10.15 19A602, locale zh-Hans-CN)
    • Flutter version 1.13.5 at /Volumes/Evo512/sdk/flutter/fvm/dev
    • Framework revision 41a911099b (4 days ago), 2019-12-19 13:48:02 -0800
    • Engine revision 0f90e6546b
    • Dart version 2.8.0 (build 2.8.0-dev.0.0 aa6709974d)

[✓] Xcode - develop for iOS and macOS (Xcode 11.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.1, Build version 11A1027
    • CocoaPods version 1.8.4

[✓] Connected device (4 available)
    • iPad mini2 • 58366e2a63aae6f85e6e806039316c27faa52868 • ios            • iOS 12.1.4

Use next steps.

  1. Open application in debug mode(or profile) at xcode, (Now the memory is 100~120MB
  2. Click "Pick Image" button to pick an image.
  3. Click multiply Selected 4 times. (16 Images)
  4. Compress and wait moment.

Now: When the compress image count is 16. The momery is 280MB~300MB.

Then, I click clear "Clear Compress" button, and wait 30 seconds, the momery will fall back to 120mb.

CaiJingLong avatar Dec 24 '19 02:12 CaiJingLong

So I suggest that if there is memory pressure, you can use the file to file compression method.
And only convert the format when you need to upload.
Although the file method needs to go through io, it can relieve the memory pressure.

CaiJingLong avatar Dec 24 '19 03:12 CaiJingLong

I am still facing that error

Loopex2019 avatar Jun 03 '20 21:06 Loopex2019

also getting this same issue when compressing many images.

This is a pretty significant problem as the alternative is using io, which both isnt clean and is more of a performance hit.

Fnalsk avatar Apr 30 '21 17:04 Fnalsk