Repository: flutter
Issue: Flutter instances don't release all memory when window is closed in multi-window macOS application.
Labels: engine,c: performance,platform-mac,perf: memory,has reproducible steps,P2,found in release: 2.8,found in release: 2.9,team-engine,triaged-engine

## Details

Launching additional flutter windows on macOS using the following Swift code:

```
let window = MainFlutterWindow()
window.initializeFlutter()
window.styleMask = styleMask
window.setFrame(frame, display: true)

let windowController = NSWindowController()
windowController.window = window
windowController.showWindow(self)
```

`window.initializeFlutter()` mostly consists of the initialization code that is placed in `awakeFromNib()` per default.
For a complete minimal example project based on the default template, see this repository:
https://github.com/KevinBrendel/flutter_macos_multi_window_memory

When 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.

![Screenshot 2022-01-11 at 23 26 36](https://user-images.githubusercontent.com/55455206/149031493-908fae2d-06be-4cc4-ad39-38c564f80013.png)

This issue occurs on both the current stable and master channels.

<!--
     Please tell us which target platform(s) the problem occurs (Android / iOS / Web / macOS / Linux / Windows)
     Which target OS version, for Web, browser, is the test system running?
     Does the problem occur on an emulator/simulator as well as on physical devices?
-->

**Target Platform:** macOS
**Target OS version/browser:** 12.1
**Devices:** MacBook Pro (16-inch, 2021)
**Architecture:** Apple Silicon / ARM64 (M1 Max)

## Logs

<details>
<summary>Logs</summary>

<!--
     Run `flutter analyze` and attach any output of that command below.
     If there are any analysis errors, try resolving them before filing this issue.
-->

```
Analyzing flutter_macos_multi_window_memory... 
No issues found! (ran in 1.7s)
```

<!-- Finally, paste the output of running `flutter doctor -v` here, with your device plugged in. -->

```
[✓] 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
```

</details>