Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory increase when pick image from gallery in example of flutter plugin image_picker

I run flutter's plugin image_picker's sample.

When I pick image one by one from gallery, the memory keep increase. Ideally the memory should jump back since it at most select one image in this example app.

enter image description here

class _MyHomePageState extends State<MyHomePage> {
  File _imageFile;              <-- this one keep the file of selected image.
  dynamic _pickImageError;
  bool isVideo = false;
  VideoPlayerController _controller;
  String _retrieveDataError;

  void _onImageButtonPressed(ImageSource source) async {
    ...
    try {
      _imageFile = await ImagePicker.pickImage(source: source);  <--- how to set value
      setState(() {});
    } catch (e) {
      _pickImageError = e;
    }
    ...
  }

  @override
  Widget build(BuildContext context) {
    ...
    Image.file(_imageFile);   <-- how to use it to display UI.
    ...
  }
}

My question is how to dispose the resource used by the File ?

like image 440
JerryZhou Avatar asked Sep 12 '19 02:09

JerryZhou


People also ask

How do I select multiple pictures from my camera on flutter?

To select multiple images we will use image_picker flutter package. in this Package, we can select multiple images from the gallery.


1 Answers

Simply selecting a File will not impact the memory, because a File is just a reference and does not hold the actual bytes of the file system entity. Creating an Image out of a file will however increase the memory usage as the Image holds all of the bytes of the File it references (once it reads all of it).

Based on what you have shared so far we cannot tell if it's a code issue or not. Unless you maintain references to more than 1 Image at a time, the minimum memory your app will use should reflect the size of that image. I say minimum because garbage collection is not overly aggressive and will not dispose of everything as soon as you no longer reference it. It will only do a GC run when it knows it needs to such that the app is not starved and the operating system is also happy. I don't know if this applies to Flutter, but with native Android it also depends on the manufacturer's customization of the Android OS which can request apps to do GC runs at various frequencies.

In your use case where the memory allocation is clearly visible, simply tap the GC button after loading the 2nd image. If the memory drops back to the same level as having a single image loaded, everything is fine. If it doesn't, you are likely to maintain unintended references to those images somewhere in your code.

Native Android also had this strange thing (possibly depending on version/manufacturer) - this was most noticeable on some older Samsung devices - where the last 4 bitmaps were cached and you had no way of clearing that cache, and depending on the sizes of the bitmaps you had to deal with, that cache could have taken almost all of the app's available memory and you ended up getting OOM errors... Hopefully Flutter doesn't do any of that, or allows the developer to control it.

like image 196
Ovidiu Avatar answered Oct 24 '22 08:10

Ovidiu