Skip to content

Dart/Flutter never release memory of "Dart/Flutter Native" leading to OOM crash #102140

@NeroThroN

Description

@NeroThroN

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.

Graph

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.
Analysis
Allocations

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listc: performanceRelates to speed or footprint issues (see "perf:" labels)engineflutter/engine related. See also e: labels.f: scrollingViewports, list views, slivers, etc.found in release: 2.10Found to occur in 2.10found in release: 2.13Found to occur in 2.13found in release: 3.1Found to occur in 3.1frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onperf: memoryPerformance issues related to memoryteam-engineOwned by Engine teamtriaged-engineTriaged by Engine team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions