Context

After some crash (mainly on IOS) on a page that display a lot of image from network, I've search in the devtools to understand what was filling the memory.

Debugging

Inside the Memory Page of devtools, I discover that while scrolling inside the ListView.builder, images where not remove from the RAM. enter image description here

Exemple to reproduce

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 since the images are cached.

I've created this sinple exemple to test this behavior. It's an infinite list of images displayed with Image.network(). This GalleryScreen can be pop to view that memory usage never decrease after.

class GalleryScreen extends StatelessWidget {
  const GalleryScreen({Key? key}) : super(key: key);

  /// List of 100 pictures URL (Between 300ko and 20Mo)
  final List<String> pictures = const [...];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Gallery Memory Test")),
      body: Center(
        child: SizedBox(
          height: 200,
          child: ListView.builder(
            scrollDirection: Axis.horizontal,
            itemBuilder: (context, index) {
              return Padding(
                padding: const EdgeInsets.all(18),
                child: AspectRatio(
                  aspectRatio: 1,
                  child: Image.network(pictures[index % pictures.length], fit: BoxFit.cover),
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}

Question

  • What's the solution to this issue ?
  • Is Flutter team aware of this behavior and create a discussion to view the evolution of a potential solution ?


Solution 1: Vincent Castronovo

Try to lower the cache you use, it will increase the data consumption of the app but it will free more RAM.

An issue has been poster on Flutter : https://github.com/flutter/flutter/issues/102140