Skip to content

Memory leaks with StatelessWidget due to closures sharing captured variables #79605

Open

Description

@esDotDev

In this example, each time I press "Click", 50M of ram is allocated. It is never reclaimed, I can push 30 pages and take up 1.5gb, despite there only ever being 1 page on the nav stack. GC never kicks in. What's going on here?

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: MemoryTest(),
  ));
}

class MemoryTest extends StatelessWidget {
  final List<EdgeInsets> insets = List.generate(
    1000000,
    (index) => EdgeInsets.all(0),
  );
  @override
  Widget build(BuildContext context) {
    return OutlinedButton(
      child: Text("CLICK"),
      onPressed: () {
        Navigator.of(context).pushReplacement(MaterialPageRoute(
          builder: (_) => MemoryTest(),
        ));
      },
    );
  }
}

I'm using Windows Task manager to watch the total RAM consumed by the Flutter App process.
image

Flutter (Channel master, 2.1.0-11.0.pre.122, on Microsoft Windows [Version 10.0.18363.1440], locale en-US)

Activity

  1. added
    in triage
    on Apr 5, 2021
  2. TahaTesser commented on Apr 5, 2021

    @TahaTesser
    Contributor

    Hi @esDotDev
    I can reproduce it on windows, ios, macos, chrome and android, memory usage keeps increasing on all of them.

    memory usage - screenshots

    macOS

    Screenshot 2021-04-05 at 14 10 55

    windows

    IMG_2379

    iOS

    Xcode Device
    Screenshot 2021-04-05 at 14 07 42

    web

    Screenshot 2021-04-05 at 14 09 21

    flutter doctor -v
    [✓] Flutter (Channel stable, 2.0.4, on Microsoft Windows [Version 10.0.19042.870], locale en-US)
        • Flutter version 2.0.4 at C:\Users\Taha\Code\flutter_stable
        • Framework revision b1395592de (4 days ago), 2021-04-01 14:25:01 -0700
        • Engine revision 2dce47073a
        • Dart version 2.12.2
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
        • Android SDK at C:\Users\Taha\Code\SDK
        • Platform android-30, build-tools 30.0.3
        • ANDROID_HOME = C:\Users\Taha\Code\SDK
        • Java binary at: C:\Users\Taha\Code\android-studio\jre\bin\java
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
        • All Android licenses accepted.
    
    [✓] Chrome - develop for the web
        • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
    
    [✓] Visual Studio - develop for Windows (Visual Studio Community 2019 16.9.2)
        • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
        • Visual Studio Community 2019 version 16.9.31112.23
        • Windows 10 SDK version 10.0.19041.0
    
    [✓] Android Studio (version 4.1.0)
        • Android Studio at C:\Users\Taha\Code\android-studio
        • Flutter plugin can be installed from:
          � https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          � https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    
    [✓] VS Code (version 1.55.0)
        • VS Code at C:\Users\Taha\AppData\Local\Programs\Microsoft VS Code
        • Flutter extension version 3.21.0
    
    [✓] Connected device (3 available)
        • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19042.870]
        • Chrome (web)      • chrome  • web-javascript • Google Chrome 89.0.4389.114
        • Edge (web)        • edge    • web-javascript • Microsoft Edge 89.0.774.63
    
    • No issues found!
    [✓] Flutter (Channel master, 2.1.0-13.0.pre.444, on Microsoft Windows [Version 10.0.19042.870], locale en-US)
        • Flutter version 2.1.0-13.0.pre.444 at C:\Users\Taha\Code\flutter_master
        • Framework revision 824b1ad3f8 (2 days ago), 2021-04-03 15:24:02 -0400
        • Engine revision e393800c63
        • Dart version 2.13.0 (build 2.13.0-196.0.dev)
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
        • Android SDK at C:\Users\Taha\Code\SDK
        • Platform android-30, build-tools 30.0.3
        • ANDROID_HOME = C:\Users\Taha\Code\SDK
        • Java binary at: C:\Users\Taha\Code\android-studio\jre\bin\java
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
        • All Android licenses accepted.
    
    [✓] Chrome - develop for the web
        • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
    
    [✓] Visual Studio - develop for Windows (Visual Studio Community 2019 16.9.2)
        • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
        • Visual Studio Community 2019 version 16.9.31112.23
        • Windows 10 SDK version 10.0.19041.0
    
    [✓] Android Studio (version 4.1.0)
        • Android Studio at C:\Users\Taha\Code\android-studio
        • Flutter plugin can be installed from:
          � https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          � https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    
    [✓] VS Code (version 1.55.0)
        • VS Code at C:\Users\Taha\AppData\Local\Programs\Microsoft VS Code
        • Flutter extension version 3.21.0
    
    [✓] Connected device (3 available)
        • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19042.870]
        • Chrome (web)      • chrome  • web-javascript • Google Chrome 89.0.4389.114
        • Edge (web)        • edge    • web-javascript • Microsoft Edge 89.0.774.63
    
    • No issues found!

    Thanks for filing the issue

  3. added
    engine
    found in release: 2.0
    found in release: 2.1
    has reproducible steps
    perf: memory
    c: performance
    and removed
    in triage
    on Apr 5, 2021
  4. added
    framework
    on Apr 5, 2021
  5. chinmaygarde commented on Apr 5, 2021

    @chinmaygarde
    Contributor
  6. added
    P2
    on Apr 5, 2021
  7. Hixie commented on Apr 5, 2021

    @Hixie
    Contributor

    This is probably debug-mode specific. I expect we're keeping widgets around for the inspector or the timeline or some such. If that's it, it's probably working as intended. Does it repro in --profile mode?

    cc @jacob314

  8. esDotDev commented on Apr 5, 2021

    @esDotDev
    Author

    From my testing it works in profile and release mode.

    Here you can see me replicating it with a much more realworld example:

    class MemoryTest extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        Widget content = OutlinedButton(
          child: Text("CLICK"),
          onPressed: () {
            Navigator.of(context).pushReplacement(MaterialPageRoute(
              builder: (_) => MemoryTest(),
            ));
          },
        );
        for (var i = 10; i-- > 0;) {
          content = Padding(padding: EdgeInsets.all(0), child: content);
        }
        return content;
      }
    }
    
    6WgwMzZ6cv.mp4

    As I add routes, memory just climbs. Also, as I resize the window, it also generates a ton of memory that is seemingly never reclaimed.

  9. Hixie commented on Apr 5, 2021

    @Hixie
    Contributor

    If it's leaking in --release mode that's definitely an issue. cc @dnfield

  10. 47 remaining items

  11. dam-ease commented on May 12, 2023

    @dam-ease

    This issue still persists on some platforms. I am able to reproduce this on the latest stable and master channels on macos and android. Memory usage keeps increasing.
    macOS, Android - Reproducible
    Chrome Web, iOS - Non-reproducible

    Code Sample

    flutter doctor -v
    [✓] Flutter (Channel master, 3.11.0-2.0.pre.55, on macOS 13.3 22E252 darwin-arm64, locale en-NG)
        • Flutter version 3.11.0-2.0.pre.55 on channel master at /Users/dammya/fvm/versions/master
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 835b892d7f (6 days ago), 2023-05-07 01:30:22 +0530
        • Engine revision 23f730efbf
        • Dart version 3.1.0 (build 3.1.0-83.0.dev)
        • DevTools version 2.23.1
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
        • Android SDK at /Users/dammya/Library/Android/sdk
        • Platform android-33, build-tools 32.1.0-rc1
        • ANDROID_HOME = /Users/dammya/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14E222b
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2021.2)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    
    [✓] IntelliJ IDEA Community Edition (version 2022.3.2)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
    
    [✓] VS Code (version 1.78.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.50.0
    
    [✓] VS Code (version 1.78.0)
        • VS Code at /Users/dammya/.Trash/Visual Studio Code 20.07.05.app/Contents
        • Flutter extension version 3.50.0
    
    [✓] Connected device (5 available)
        • TECNO LC6a (mobile)        • 044882598G014637                     • android-arm    • Android 9 (API 28)
        • sdk gphone arm64 (mobile)  • emulator-5554                        • android-arm64  • Android 11 (API 30) (emulator)
        • iPhone 14 Pro Max (mobile) • D7F3302D-D66F-4B89-B8CF-DBEE1CDB5449 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-16-4 (simulator)
        • macOS (desktop)            • macos                                • darwin-arm64   • macOS 13.3 22E252 darwin-arm64
        • Chrome (web)               • chrome                               • web-javascript • Google Chrome 113.0.5672.92
    
    [✓] Network resources
        • All expected network resources are available.
    
    • No issues found!
    
    [✓] Flutter (Channel stable, 3.10.0, on macOS 13.3 22E252 darwin-arm64, locale en-NG)
        • Flutter version 3.10.0 on channel stable at /Users/dammya/fvm/versions/stable
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 84a1e904f4 (2 days ago), 2023-05-09 07:41:44 -0700
        • Engine revision d44b5a94c9
        • Dart version 3.0.0
        • DevTools version 2.23.1
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
        • Android SDK at /Users/dammya/Library/Android/sdk
        • Platform android-33, build-tools 32.1.0-rc1
        • ANDROID_HOME = /Users/dammya/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14E222b
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2021.2)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    
    [✓] IntelliJ IDEA Community Edition (version 2022.3.2)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
    
    [✓] VS Code (version 1.78.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.50.0
    
    [✓] VS Code (version 1.78.0)
        • VS Code at /Users/dammya/.Trash/Visual Studio Code 20.07.05.app/Contents
        • Flutter extension version 3.50.0
    
    [✓] Connected device (5 available)
        • TECNO LC6a (mobile)        • 044882598G014637                     • android-arm    • Android
          9 (API 28)
        • sdk gphone arm64 (mobile)  • emulator-5554                        • android-arm64  • Android
          11 (API 30) (emulator)
        • iPhone 14 Pro Max (mobile) • D7F3302D-D66F-4B89-B8CF-DBEE1CDB5449 • ios            •
          com.apple.CoreSimulator.SimRuntime.iOS-16-4 (simulator)
        • macOS (desktop)            • macos                                • darwin-arm64   • macOS
          13.3 22E252 darwin-arm64
        • Chrome (web)               • chrome                               • web-javascript • Google
          Chrome 113.0.5672.92
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    • No issues found!
    
    
  12. added
    found in release: 3.10
    found in release: 3.11
    and removed
    found in release: 2.0
    found in release: 2.1
    on May 12, 2023
  13. jacob314 commented on May 15, 2023

    @jacob314
    Contributor

    Fyi @polina-c. Would be good to verify that the leak_tracker package makes it easy to write memory leak tracking tests to catch this issue.

  14. polina-c commented on May 15, 2023

    @polina-c
    Contributor

    Yes, this is the plan: flutter/devtools#3951

  15. added
    team-framework
    triaged-framework
    on Jul 8, 2023
  16. alienfractal commented on Feb 5, 2024

    @alienfractal
  17. Hixie commented on Feb 6, 2024

    @Hixie
  18. changed the title [-]Memory leak with Stateless Widget[/-] [+]Memory leaks with StatelessWidget due to closures sharing captured variables[/+] on Feb 6, 2024
  19. jacob314 commented on Feb 7, 2024

    @jacob314
    Contributor

    I agree that this is about a design decision that causes leaks in the Dart VM. However, it is a design decision made by most VMs. For example, if you were writing JS with the same pattern you would get a leak in V8. The only reason Dart doesn't get a leak when compiling to JS is because of quirks in how Dart closures get compiled to JS. I'm excited to see whether @polina-c's leak detection tooling is able to make it easier to catch and fix leaks like this.

  20. Hixie commented on Feb 8, 2024

    @Hixie
    Contributor

    To be clear I didn't mean that statement to be a criticism of the Dart VM decision.

  21. iapicca commented on Aug 8, 2024

    @iapicca

    does not seem to occur on web-wasm (although so much RAM used in general) nor macos

    code sample
    import 'package:flutter/material.dart';
    
    void main() => runApp(MaterialApp(home: Material(child: MemoryTest())));
    
    class MemoryTest extends StatelessWidget {
    // ignore: unused_field, prefer_const_constructors
    final _insets = [for (var i = 0; i < 1000000; i++) EdgeInsets.all(0)];
    
    MemoryTest({super.key});
    @override
    Widget build(context) => Center(
          child: OutlinedButton(
            child: const Text('click'),
            onPressed: () => Navigator.of(context).pushReplacement(
              MaterialPageRoute(
                builder: (context) => MemoryTest(),
              ),
            ),
          ),
        );
    }
    
    doctor
    [✓] Flutter (Channel stable, 3.24.0, on macOS 14.5 23F79 darwin-arm64, locale en-US)
      • Flutter version 3.24.0 on channel stable at /Users/francesco/fvm/versions/stable
      • Upstream repository https://github.com/flutter/flutter.git
      • Framework revision 80c2e84975 (9 days ago), 2024-07-30 23:06:49 +0700
      • Engine revision b8800d88be
      • Dart version 3.5.0
      • DevTools version 2.37.2
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
      • Android SDK at /Users/francesco/Library/Android/sdk
      • Platform android-34, build-tools 34.0.0
      • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
      • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
      • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 15.4)
      • Xcode at /Applications/Xcode.app/Contents/Developer
      • Build 15F31d
      • CocoaPods version 1.15.2
    
    [✓] Chrome - develop for the web
      • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2022.3)
      • Android Studio at /Applications/Android Studio.app/Contents
      • Flutter plugin can be installed from:
        🔨 https://plugins.jetbrains.com/plugin/9212-flutter
      • Dart plugin can be installed from:
        🔨 https://plugins.jetbrains.com/plugin/6351-dart
      • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    
    [✓] VS Code (version 1.92.0)
      • VS Code at /Applications/Visual Studio Code.app/Contents
      • Flutter extension version 3.94.0
    
    [✓] Connected device (3 available)
      • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 14.5 23F79 darwin-arm64
      • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 14.5 23F79 darwin-arm64
      • Chrome (web)                    • chrome                • web-javascript • Google Chrome 120.0.6099.71
    
    [✓] Network resources
      • All expected network resources are available.
    
    • No issues found!
    • flutter build web --wasm
    • dart pub global activate dhttpd
    • dhttpd --path build/web/
    • open http://localhost:8080/
    • click click as much as you like
    web screenshot

    image

    • flutter run --release -d macos
    • click click as much as you like
    macos screenshot image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2c: performancefound in release: 3.10found in release: 3.11frameworkhas reproducible stepsperf: memoryteam-frameworktriaged-framework

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions