You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Memory leaks with StatelessWidget due to closures sharing captured variables #79605
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?
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
windows
iOS
Xcode
Device
web
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!
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?
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
changed the title [-]Memory leak with Stateless Widget[/-][+]Memory leaks with StatelessWidget due to closures sharing captured variables[/+]on Feb 6, 2024
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.
[✓] 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/
Activity
TahaTesser commentedon Apr 5, 2021
Hi @esDotDev
I can reproduce it on
windows,ios,macos,chromeandandroid, memory usage keeps increasing on all of them.memory usage - screenshots
macOS
windows
iOS
web
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
chinmaygarde commentedon Apr 5, 2021
cc @goderbauer @Hixie @chunhtai
Hixie commentedon Apr 5, 2021
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
--profilemode?cc @jacob314
esDotDev commentedon Apr 5, 2021
From my testing it works in profile and release mode.
Here you can see me replicating it with a much more realworld example:
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.
Hixie commentedon Apr 5, 2021
If it's leaking in
--releasemode that's definitely an issue. cc @dnfield47 remaining items
dam-ease commentedon May 12, 2023
This issue still persists on some platforms. I am able to reproduce this on the latest
stableandmasterchannels onmacosandandroid. 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!jacob314 commentedon May 15, 2023
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.
polina-c commentedon May 15, 2023
Yes, this is the plan: flutter/devtools#3951
alienfractal commentedon Feb 5, 2024
Hixie commentedon Feb 6, 2024
[-]Memory leak with Stateless Widget[/-][+]Memory leaks with StatelessWidget due to closures sharing captured variables[/+]jacob314 commentedon Feb 7, 2024
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.
Hixie commentedon Feb 8, 2024
To be clear I didn't mean that statement to be a criticism of the Dart VM decision.
iapicca commentedon Aug 8, 2024
does not seem to occur on web-wasm (although so much RAM used in general) nor macos
code sample
doctor
flutter build web --wasmdart pub global activate dhttpddhttpd --path build/web/open http://localhost:8080/clickas much as you likeweb screenshot
flutter run --release -d macosclickas much as you likemacos screenshot