Repository: flutter
Issue: [video_player] Video persisting memory after dispose
Labels: platform-ios,p: video_player,package,perf: memory,has reproducible steps,P2,team-ios,triaged-ios,found in release: 3.24,found in release: 3.27

### Steps to reproduce

1. Press button to show video
2. Wait for video to initialize
3. Press the button to hide video and dispose
4. Repeat

### Expected results

Expected memory to be released after disposing the video.

### Actual results

Memory keeps increasing when displaying videos. It does not happen every iteration.

I have tried on iOS with and without impeller. It's definitely more frequent to see an increase when the video plays after initialising. 

It appears to have something to do with the memory allocated in FVPVideoPlayer copyPixelBuffer 

Device : iPhone 8
Software version: 14.7.1

Regarding flutter versions I have tried 3.22.1 ( as in flutter doctor ) and 3.24.4 ( on stable channel )

1st screenshot - VM memory that was created and persisted. You can see persisted memory increases over time.
2nd screenshot - IOSurface, which seems to be the problematic entry, you can also see the persisted memory increasing over time as well as the code trace ( all entries are the same )

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

void main() {
  runApp(
    MaterialApp(
      home: _App(),
    ),
  );
}

class _App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: const ValueKey<String>('home_page'),
      appBar: AppBar(title: const Text('Video player example')),
      body: OnOffVideo(),
    );
  }
}

class OnOffVideo extends StatefulWidget {
  const OnOffVideo({Key? key}) : super(key: key);

  @override
  State<OnOffVideo> createState() => _OnOffVideoState();
}

class _OnOffVideoState extends State<OnOffVideo> {
  int counter = 0;
  bool isVisible = false;

  @override
  Widget build(BuildContext context) {
    return Column(crossAxisAlignment: CrossAxisAlignment.center, children: [
      Center(
        child: GestureDetector(
          onTap: () {
            setState(() {
              isVisible = !isVisible;
            });
          },
          child: Container(
            color: isVisible ? Colors.red : Colors.green,
            height: 50,
            width: 50,
            child: Text(isVisible ? 'Hide' : 'Show'),
          ),
        ),
      ),
      if (isVisible) _BumbleBeeRemoteVideo(key: ValueKey<int>(counter++)),
    ]);
  }
}

class _BumbleBeeRemoteVideo extends StatefulWidget {
  const _BumbleBeeRemoteVideo({Key? key}) : super(key: key);

  @override
  _BumbleBeeRemoteVideoState createState() => _BumbleBeeRemoteVideoState();
}

class _BumbleBeeRemoteVideoState extends State<_BumbleBeeRemoteVideo> {
  late VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.networkUrl(
      Uri.parse('https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4'),
      videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true),
    );

    _controller.addListener(() {
      setState(() {});
    });
    _controller.setLooping(true);
    _controller.initialize().then((value) => _controller.play());
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          Container(padding: const EdgeInsets.only(top: 20.0)),
          const Text('With remote mp4'),
          Container(
            padding: const EdgeInsets.all(20),
            child: AspectRatio(
              aspectRatio: _controller.value.aspectRatio,
              child: Stack(
                alignment: Alignment.bottomCenter,
                children: <Widget>[
                  VideoPlayer(_controller),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

<img width="1728" alt="Screenshot 2024-11-06 at 13 41 44" src="https://github.com/user-attachments/assets/226ea290-75a5-442c-b379-91f4ee59202f">

<img width="1727" alt="Screenshot 2024-11-06 at 13 40 49" src="https://github.com/user-attachments/assets/b1d1a4c0-c6fa-4fa2-9a5f-62fef23f9066">

</details>



### Logs

<details open><summary>Logs</summary>

```console
[Paste your logs here]
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[!] Flutter (Channel [user-branch], 3.22.1, on macOS 14.6.1 23G93 darwin-arm64, locale en-PT)
    ! Flutter version 3.22.1 on channel [user-branch] at /Users/user/flutter
      Currently on an unknown channel. Run `flutter channel` to switch to an official channel.
      If that doesn't fix the issue, reinstall Flutter by following instructions at https://flutter.dev/docs/get-started/install.
    ! Upstream repository unknown source is not a standard remote.
      Set environment variable "FLUTTER_GIT_URL" to unknown source to dismiss this error.
    • Framework revision a14f74ff3a (6 months ago), 2024-05-22 11:08:21 -0500
    • Engine revision 55eae6864b
    • Dart version 3.4.1
    • DevTools version 2.34.3
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/user/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
    • 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 2023.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.10+0-17.0.10b1087.21-11572160)

[✓] VS Code (version 1.92.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (3 available)            
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 14.6.1 23G93 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 14.6.1 23G93 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 130.0.6723.93

[✓] Network resources
    • All expected network resources are available.
```

</details>