-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Description
Problem
Recently I encounter some crashs on an app because it was Out Of Memory. After some digging with DevTools I quickly found that a scrollable list of images was filling anormaly the memory.
After some testing I have the feeling that pictures are remove from "Dart/Flutter" cache but are never release from "Dart/Flutter Native" memory.
I've tried to use cacheWidth and cacheHeight parameters but same issue the memory never empty itself. I've tried cached_network_image package but the same issue occurs with the fact that it fills up faster the memory since the images are cached. I've tried also to download the content of pictures (Uint8List) to display with Image.memory but same issue occurs. This behaviors confort me that the problem is based on the ListView keeping rasterize children in memory or something like this.
On this graph we can see the evolution of the "Dart/Flutter Native" that increase constantly without decreasing in contrary of the "Dart/Flutter" that manage smarter the memory consumption.
On the Analysis and Allocations tab we can see that the _List class is instantiate a large number of times causing to use a lot's of bytes.


How to reproduce
In order to reproduce, just create a ListView that contains images. After scrolling for a bunch of minutes, take a look of the analysis tab of Devtool memory page. After poping the screen containing the ListView, memory is never releaseed.
ListView.builder(
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(6),
child: ClipRRect(
borderRadius: BorderRadius.circular(12),
child: AspectRatio(
aspectRatio: 1,
child: Container(
decoration: BoxDecoration(color: Colors.grey[200]),
child: Image.network(pictures[index % pictures.length], fit: BoxFit.cover),
),
),
),
);
},
)I have created a blank flutter project that reproduce this issue : Complete demo code
Questions
- What's the solution to this issue ?
- What would be a good alternative to avoid this problem ?
- Is Flutter team aware of this behavior and can create a discussion to view the evolution of a potential solution ?
