Repository: flutter
Issue: A memory leak occurs when using SurfaceView together with AndroidView
Labels: platform-android,c: performance,a: platform-views,perf: memory,has reproducible steps,P2,found in release: 3.3,found in release: 3.7,team-android,triaged-android

## Details
There seems to be a memory leak when using **_AndroidView_** together with **_SurfaceView_** at native side.  

I've created simple app which can demonstrate the issue. It displays AndroidView which could use SurfaceView or TextureView to display native content.
App is able to toggle AndroidView visibility.
The leak occurs when AndroidView is removed from widget tree and then recreated multiple times during app operation.
Sources: https://github.com/add022/flutter_native_view_sample_java

Here is sample screenshot from Profiler when app is using **SurfaceView** and toggling AndroidView visibility multiple times. Memory constantly growing, GC is also not able to reclaim allocated memory.
![image](https://user-images.githubusercontent.com/57544373/212064870-6db65c23-9ca2-42ee-9fb4-7a5e2bdd0453.png)
I've used SurfaceHolder's callbacks to track surface lifecycle and it seems that **surfaceDestroyed()** seems to be not called when AndroidView is removed from widget tree.  That may be some clue.


On the other side when **TextureView** is used with the same scenario the leak seems to be not visible, and memory seems to be correclty reclaimed by GC:
![image](https://user-images.githubusercontent.com/57544373/212066178-85cb04c7-d1b1-452c-8460-801531c25d54.png)
I've also examined the lifecycle of TextureView using TextureView.SurfaceTextureListener.
In this case the **onSurfaceTextureDestroyed** seems to be called every time AndroidView is removed from widget tree. 

Issue was oserved on Flutter **3.3.9** and also verified that it still exists in **3.7.0-1.4.pre**.

**Target Platform:** Android
**Target OS version/browser:** Android 13(and others)
**Devices:** Samsung S22+(and others)



## Logs

<details>
<summary>Logs</summary>


Here is some StrictMode violation which can be observed after GC:
```
2023-01-12 14:26:48.017 20071-20092 StrictMode              com...utter_native_view_sample_java  D  StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
                                                                                                    	at android.os.StrictMode$AndroidCloseGuardReporter.report(StrictMode.java:1986)
                                                                                                    	at dalvik.system.CloseGuard.warnIfOpen(CloseGuard.java:336)
                                                                                                    	at android.view.Surface.finalize(Surface.java:310)
                                                                                                    	at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:319)
                                                                                                    	at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:306)
                                                                                                    	at java.lang.Daemons$Daemon.run(Daemons.java:140)
                                                                                                    	at java.lang.Thread.run(Thread.java:1012)
                                                                                                    Caused by: java.lang.Throwable: Explicit termination method 'Surface.release' not called
                                                                                                    	at dalvik.system.CloseGuard.openWithCallSite(CloseGuard.java:288)
                                                                                                    	at dalvik.system.CloseGuard.open(CloseGuard.java:257)
                                                                                                    	at android.view.Surface.setNativeObjectLocked(Surface.java:760)
                                                                                                    	at android.view.Surface.<init>(Surface.java:294)
                                                                                                    	at io.flutter.plugin.platform.VirtualDisplayController.create(VirtualDisplayController.java:53)
                                                                                                    	at io.flutter.plugin.platform.PlatformViewsController$1.configureForVirtualDisplay(PlatformViewsController.java:541)
                                                                                                    	at io.flutter.plugin.platform.PlatformViewsController$1.createForTextureLayer(PlatformViewsController.java:219)
                                                                                                    	at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.create(PlatformViewsChannel.java:128)
                                                                                                    	at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.onMethodCall(PlatformViewsChannel.java:55)
                                                                                                    	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)
                                                                                                    	at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
                                                                                                    	at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
                                                                                                    	at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
                                                                                                    	at android.os.Handler.handleCallback(Handler.java:942)
                                                                                                    	at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                    	at android.os.Looper.loopOnce(Looper.java:226)
                                                                                                    	at android.os.Looper.loop(Looper.java:313)
                                                                                                    	at android.app.ActivityThread.main(ActivityThread.java:8741)
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)

```

Also following logs about abandoned buffers seems to appear after GC execution:
```
2023-01-12 14:26:49.423 20071-20238 BufferQueueProducer     com...utter_native_view_sample_java  E  [SurfaceTexture-0-20071-0](id:4e6700000002,api:1,p:1138,c:20071) dequeueBuffer: BufferQueue has been abandoned
2023-01-12 14:26:49.423 20071-20238 BufferQueueProducer     com...utter_native_view_sample_java  E  [SurfaceTexture-0-20071-1](id:4e6700000005,api:1,p:1138,c:20071) dequeueBuffer: BufferQueue has been abandoned
2023-01-12 14:26:49.423 20071-20238 BufferQueueProducer     com...utter_native_view_sample_java  E  [SurfaceTexture-0-20071-2](id:4e6700000008,api:1,p:1138,c:20071) dequeueBuffer: BufferQueue has been abandoned
2023-01-12 14:26:49.423 20071-20238 BufferQueueProducer     com...utter_native_view_sample_java  E  [SurfaceTexture-0-20071-3](id:4e670000000b,api:1,p:1138,c:20071) dequeueBuffer: BufferQueue has been abandoned
2023-01-12 14:26:49.423 20071-20238 BufferQueueProducer     com...utter_native_view_sample_java  E  [SurfaceTexture-0-20071-4](id:4e670000000e,api:1,p:1138,c:20071) dequeueBuffer: BufferQueue has been abandoned
2023-01-12 14:26:49.423 20071-20238 BufferQueueProducer     com...utter_native_view_sample_java  E  [SurfaceTexture-0-20071-5](id:4e6700000011,api:1,p:1138,c:20071) dequeueBuffer: BufferQueue has been abandoned
2023-01-12 14:26:49.424 20071-20238 BufferQueueProducer     com...utter_native_view_sample_java  E  [SurfaceTexture-0-20071-6](id:4e6700000014,api:1,p:1138,c:20071) dequeueBuffer: BufferQueue has been abandoned
2023-01-12 14:26:49.424 20071-20238 BufferQueueProducer     com...utter_native_view_sample_java  E  [SurfaceTexture-0-20071-7](id:4e6700000017,api:1,p:1138,c:20071) dequeueBuffer: BufferQueue has been abandoned

```



<!-- Finally, paste the output of running `flutter doctor -v` here, with your device plugged in. -->

Flutter doctor:
```
[✓] Flutter (Channel stable, 3.3.9, on macOS 12.5.1 21G83 darwin-x64, locale en-PL)
    • Flutter version 3.3.9 on channel stable at /Users/domin/fvm/versions/3.3.9
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b8f7f1f986 (7 weeks ago), 2022-11-23 06:43:51 +0900
    • Engine revision 8f2221fbef
    • Dart version 2.18.5
    • DevTools version 2.15.0

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/domin/Library/Android/sdk
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.

[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14B47
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.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.13+0-b1751.21-8125866)

[✓] VS Code (version 1.74.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.56.0

[✓] Connected device (3 available)
    • SM S906B (mobile) • RFCTA01WFRA • android-arm64  • Android 13 (API 33)
    • macOS (desktop)   • macos       • darwin-x64     • macOS 12.5.1 21G83 darwin-x64
    • Chrome (web)      • chrome      • web-javascript • Google Chrome 109.0.5414.87

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.

```

</details>