Skip to content

[Web] Gridview with images has significant memory leak #94500

@mat100payette

Description

@mat100payette

Details

Using GridView.builder with many Image.network() children seems to create a memory leak.
The following minimal app (with a few added lines of solutions I tested from here and there) starts at around 175Mb of RAM consumption. After scrolling through all images, the RAM reaches a whopping 500Mb and never goes back down.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

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

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

  final String title;
  late final List<String> imageLinks;
  final ScrollController controller = ScrollController();

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    widget.imageLinks = List.generate(1000, (index) => 'https://picsum.photos/id/$index/200/300');
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              height: 200,
              child: GridView.builder(
                controller: widget.controller,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  childAspectRatio: 16 / 9,
                ),
                addAutomaticKeepAlives: false, // Tried this
                addRepaintBoundaries: false, // Tried this
                shrinkWrap: true, // Tried this
                cacheExtent: 0, // Tried this
                itemCount: widget.imageLinks.length,
                itemBuilder: (context, index) => Image.network(widget.imageLinks[index]),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Target Platform: Web
Target OS version/browser: Chrome 96.0.4664.45 (64 bits)
Devices: -

As a side note, I did also try to use Disable Cache in Chrome DevTools, but that did not help.

Logs

Logs
No issues found! (ran in 2.7s)
[√] Flutter (Channel master, 2.6.0-12.0.pre.878, on Microsoft Windows [version 10.0.18363.1916], locale fr-CA)
    • Flutter version 2.6.0-12.0.pre.878 at C:\Flutter\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 17980da91a (2 hours ago), 2021-12-01 12:44:07 -0800
    • Engine revision c2fcadef89
    • Dart version 2.16.0 (build 2.16.0-56.0.dev)
    • DevTools version 2.8.0

[X] Android toolchain - develop for Android devices
    X Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.


[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).

[√] VS Code (version 1.62.3)
    • VS Code at C:\Users\matpa\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.28.0

[√] Connected device (2 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 96.0.4664.45
    • Edge (web)   • edge   • web-javascript • Microsoft Edge 96.0.1054.29

! Doctor found issues in 2 categories.

Metadata

Metadata

Assignees

Labels

P0Critical issues such as a build break or regressione: web_canvaskitCanvasKit (a.k.a. Skia-on-WebGL) rendering backend for Webengineflutter/engine related. See also e: labels.perf: memoryPerformance issues related to memoryplatform-webWeb applications specificallyr: fixedIssue is closed as already fixed in a newer version

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions