Open KevinBrendel opened 2 years ago
Reproducible on stable and master
Can't share the instruments files because the sizes are too large for GH.
The demo has the retain cycle, please break the cycle and try again. https://github.com/KevinBrendel/flutter_macos_multi_window_memory/blob/df9014c56d865a831dab270df892dfcf303f3393/macos/Runner/MainFlutterWindow.swift?_pjax=%23js-repo-pjax-container%2C%20div%5Bitemtype%3D%22http%3A%2F%2Fschema.org%2FSoftwareSourceCode%22%5D%20main%2C%20%5Bdata-pjax-container%5D#L17
Thanks for the suggestion. I am pretty new to Swift, so to be sure, I completely removed the MethodChannel and replaced it with a native menu option (Window -> New Window or CMD+N).
This seems to have increased the amount of memory that is released when closing a window, but still, a large portion remains allocated.
cc @cbracken This could also be the instance of the VM that is running after the engine instance goes away. The fix is to explicitly tell the engine to shutdown the VM when the last instance goes away. Today, the VM stays up so that subsequent isolate launches during resume etc. are faster than first launch.
That definitely sounds like it would cause this issue. Is there some way for me to configure the engine to shut down the VM to test this (without compiling my own version of Flutter)?
without compiling my own version of Flutter
It's part of the public Embedder API but I am not sure if the embedder hides this in turn. Which is why I pinged @cbracken .
Details
Launching additional flutter windows on macOS using the following Swift code:
window.initializeFlutter()
mostly consists of the initialization code that is placed inawakeFromNib()
per default. For a complete minimal example project based on the default template, see this repository: https://github.com/KevinBrendel/flutter_macos_multi_window_memoryWhen closing a window, the allocated memory for this window is not released.
I managed to reduce the memory allocation by calling shutDownEngine(), but still a lot of memory remains allocated.
This issue occurs on both the current stable and master channels.
Target Platform: macOS Target OS version/browser: 12.1 Devices: MacBook Pro (16-inch, 2021) Architecture: Apple Silicon / ARM64 (M1 Max)
Logs
Logs
``` Analyzing flutter_macos_multi_window_memory... No issues found! (ran in 1.7s) ``` ``` [✓] Flutter (Channel master, 2.9.0-1.0.pre.322, on macOS 12.1 21C52 darwin-arm, locale en-DE) • Flutter version 2.9.0-1.0.pre.322 at /Users/kevin/SDKs/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 9490917408 (4 hours ago), 2022-01-11 13:00:12 -0600 • Engine revision a79a3bac0c • Dart version 2.16.0 (build 2.16.0-154.0.dev) • DevTools version 2.9.2 [✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0) • Android SDK at /Users/kevin/Library/Android/sdk • Platform android-32, build-tools 32.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1) • Xcode at /Applications/Xcode.app/Contents/Developer • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2020.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 11.0.10+0-b96-7249189) [✓] VS Code (version 1.63.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.32.0 [✓] Connected device (2 available) • macOS (desktop) • macos • darwin-arm64 • macOS 12.1 21C52 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 97.0.4692.71 [✓] HTTP Host Availability • All required HTTP hosts are available ```