Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak with IndexedStack containing a UiKitView #148639

Open
ristiisa opened this issue May 19, 2024 · 5 comments
Open

Memory leak with IndexedStack containing a UiKitView #148639

ristiisa opened this issue May 19, 2024 · 5 comments
Assignees
Labels
a: platform-views Embedding Android/iOS views in Flutter apps c: performance Relates to speed or footprint issues (see "perf:" labels) found in release: 3.22 Found to occur in 3.22 has reproducible steps The issue has been confirmed reproducible and is ready to work on perf: memory Performance issues related to memory platform-ios iOS applications specifically team-ios Owned by iOS platform team

Comments

@ristiisa
Copy link

Steps to reproduce

git clone git@github.com:ristiisa/indexedstackmemoryleak.git
cd indexedstackmemoryleak
flutter create .
flutter pub get
open ios/Runner.xcworkspace
  1. run code example using xcode
  2. open memory report
  3. switch between tab 1 and 2
  4. witness memory usage growth when tab 1 is selected

Expected results

constant memory usage

Actual results

memory leaks until crash

Code sample

Code sample
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

import 'dart:developer' as dev;

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  runApp(MaterialApp(home: LeakSample()));
}

class LeakSample extends StatefulWidget {
  @override
  State<LeakSample> createState() => _LeakSampleState();
}

class _LeakSampleState extends State<LeakSample> {
  Stream<int>? value;
  int lastValue = 0;

  @override
  initState() {
    super.initState();

    value = Stream.periodic(const Duration(milliseconds: 1000), (_) => lastValue++);
  }
  int index = 0;

  MethodChannel? channel;

  @override
  Widget build(BuildContext context) => Scaffold(
    body: IndexedStack(index: index, children: [
      const Center(child: Text("Check memory usage")),
      StreamBuilder<int>(stream: value, builder: (context, snapshot) => UiKitView(
        viewType: "TestView",
        key: ValueKey(lastValue),
        creationParams: { "value": lastValue },
        creationParamsCodec: const StandardMessageCodec(),
        hitTestBehavior: PlatformViewHitTestBehavior.translucent,
        onPlatformViewCreated: (id) {
          channel = MethodChannel("com.example/testview_$id");
          channel?.setMethodCallHandler((call) async {
            dev.log(call.arguments, name: call.method);
          });
        },
      ))
    ]),
    bottomNavigationBar: BottomNavigationBar(
      items: const [
        BottomNavigationBarItem(
          icon: Icon(Icons.looks_one),
          label: "Tab 1",
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.looks_two),
          label: "Tab 2",
        ),
      ],
      currentIndex: index,
      onTap: (v) => setState(() => index = v),
    )
  );
}
import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)

    weak var registrar = self.registrar(forPlugin: "TestViewPlugin")

    let factory = TestViewFactory(messenger: registrar!.messenger())
    registrar!.register(factory, withId: "TestView")

    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

class TestViewFactory: NSObject, FlutterPlatformViewFactory {
  private var messenger: FlutterBinaryMessenger

  init(messenger: FlutterBinaryMessenger) {
    self.messenger = messenger
    super.init()
  }

  func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
    return FlutterStandardMessageCodec.sharedInstance()
  }

  func create(withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?) -> FlutterPlatformView {
    return TestView(frame: frame, viewIdentifier: viewId, arguments: args as? [String: Any] ?? [:], binaryMessenger: messenger)
  }
}

class TestView: NSObject, FlutterPlatformView {
  let frame: CGRect
  var viewId: Int64
  var messenger: FlutterBinaryMessenger
  var _view: UIView
  var channel: FlutterMethodChannel?
  let buffer = [UInt8](repeating: 0, count: 100*1024*1024)

  init(frame: CGRect, viewIdentifier viewId: Int64, arguments: [String: Any], binaryMessenger messenger: FlutterBinaryMessenger) {
    self.frame = frame
    self.viewId = viewId
    self.messenger = messenger

    _view = UIView()
    _view.backgroundColor = UIColor.blue
    super.init()

    let value = (arguments["value"] as? Int) ?? -1

    let label = UILabel()
    label.text = "Value: \(value)"
    label.textColor = UIColor.white
    label.textAlignment = .center
    label.translatesAutoresizingMaskIntoConstraints = false
  
    _view.addSubview(label)
    
    NSLayoutConstraint.activate([
      label.centerXAnchor.constraint(equalTo: _view.centerXAnchor),
      label.centerYAnchor.constraint(equalTo: _view.centerYAnchor)
    ])
  }

  func view() -> UIView {
    return _view
  }
}

Screenshots or Video

Screenshots / Video demonstration image
output.mp4

Logs

No response

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.19.3, on macOS 14.4 23E214 darwin-arm64, locale en-GB)
    • Flutter version 3.19.3 on channel stable at /Users/user/fvm/versions/stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ba39319843 (2 months ago), 2024-03-07 15:22:21 -0600
    • Engine revision 2e4ba9c6fb
    • Dart version 3.3.1
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/user/Library/Android/sdk
    • Platform android-34, build-tools 31.0.0
    • 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 15.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15E204a
    • CocoaPods version 1.15.0

[✓] 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)

[✓] VS Code (version 1.89.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.88.0

[✓] Connected device (4 available)
    • iPhone 15 Pro (mobile) • A34BE4F3-C4CA-4D58-9542-F55D87BEC5F1 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-17-0 (simulator)
    • macOS (desktop)        • macos                                • darwin-arm64   • macOS 14.4 23E214 darwin-arm64
    • Chrome (web)           • chrome                               • web-javascript • Google Chrome 125.0.6422.60

[✓] Network resources
    • All expected network resources are available.
@darshankawar darshankawar added the in triage Presently being triaged by the triage team label May 20, 2024
@darshankawar
Copy link
Member

Thanks for the detailed report @ristiisa
Are you seeing this in debug mode ? If so, can you try in profile or release mode and check if in these modes, do you get same behavior ? Also, try the same using physical device.

@darshankawar darshankawar added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 20, 2024
@ristiisa
Copy link
Author

ristiisa commented May 20, 2024

Can confirm the behaviour in a physical device and in release and profile mode

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 20, 2024
@darshankawar
Copy link
Member

Thanks for the update. I do see the same behavior in all modes using latest versions.

stable, master flutter doctor -v
[!] Flutter (Channel stable, 3.22.0, on macOS 12.2.1 21D62 darwin-x64, locale
    en-GB)
    • Flutter version 3.22.0 on channel stable at
      /Users/dhs/documents/fluttersdk/flutter
    ! Warning: `flutter` on your path resolves to
      /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
      your current Flutter SDK checkout at
      /Users/dhs/documents/fluttersdk/flutter. Consider adding
      /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
    ! Warning: `dart` on your path resolves to
      /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
      current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
      Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
      of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5dcb86f68f (5 days ago), 2024-05-09 07:39:20 -0500
    • Engine revision f6344b75dc
    • Dart version 3.4.0
    • 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.

[!] Xcode - develop for iOS and macOS (Xcode 12.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    ! Flutter recommends a minimum Xcode version of 13.
      Download the latest version or update via the Mac App Store.
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] VS Code (version 1.62.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.21.0

[✓] Connected device (5 available)
    • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
    • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
      ios            • iOS 14.4.1 18D61
    • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
      ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
    • macOS (desktop)            • macos                                    •
      darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
    • Chrome (web)               • chrome                                   •
      web-javascript • Google Chrome 98.0.4758.80

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.

[!] Flutter (Channel master, 3.22.0-35.0.pre.24, on macOS 12.2.1 21D62
    darwin-x64, locale en-GB)
    • Flutter version 3.22.0-35.0.pre.24 on channel master at
      /Users/dhs/documents/fluttersdk/flutter
    ! Warning: `flutter` on your path resolves to
      /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
      your current Flutter SDK checkout at
      /Users/dhs/documents/fluttersdk/flutter. Consider adding
      /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
    ! Warning: `dart` on your path resolves to
      /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
      current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
      Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
      of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2b0d879ec7 (63 minutes ago), 2024-05-16 00:13:26 -0400
    • Engine revision 942d7c35de
    • Dart version 3.5.0 (build 3.5.0-160.0.dev)
    • DevTools version 2.36.0-dev.10
    • 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 30.0.3)
    • Android SDK at /Users/dhs/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 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 13C100
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] IntelliJ IDEA Ultimate Edition (version 2021.3.2)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin version 65.1.4
    • Dart plugin version 213.7228

[✓] VS Code (version 1.62.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.29.0

[✓] Connected device (3 available)
    • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios
      • iOS 15.3.1 19D52
    • macOS (desktop)           • macos                                    •
      darwin-x64     • macOS 12.2.1 21D62 darwin-x64
    • Chrome (web)              • chrome                                   •
      web-javascript • Google Chrome 109.0.5414.119

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.
      
[!] Xcode - develop for iOS and macOS (Xcode 12.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    ! Flutter recommends a minimum Xcode version of 13.
      Download the latest version or update via the Mac App Store.
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] VS Code (version 1.62.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.21.0

[✓] Connected device (5 available)
    • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
    • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
      ios            • iOS 14.4.1 18D61
    • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
      ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
    • macOS (desktop)            • macos                                    •
      darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
    • Chrome (web)               • chrome                                   •
      web-javascript • Google Chrome 98.0.4758.80

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.



@darshankawar darshankawar added c: performance Relates to speed or footprint issues (see "perf:" labels) perf: memory Performance issues related to memory has reproducible steps The issue has been confirmed reproducible and is ready to work on found in release: 3.22 Found to occur in 3.22 platform-ios iOS applications specifically a: platform-views Embedding Android/iOS views in Flutter apps team-ios Owned by iOS platform team and removed in triage Presently being triaged by the triage team labels May 21, 2024
@cbracken
Copy link
Member

@ristiisa question: what is the intent of the StreamBuilder<int> in the above code? i.e. why not embed the underlying UIKitView directly?

@jmagman jmagman self-assigned this May 22, 2024
@cbracken cbracken added triaged-ios Triaged by iOS platform team and removed triaged-ios Triaged by iOS platform team labels May 22, 2024
@ristiisa
Copy link
Author

@ristiisa question: what is the intent of the StreamBuilder<int> in the above code? i.e. why not embed the underlying UIKitView directly?

I'm confused by this question... The intent is to show undesired behaviour. The code serves no other purpose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: platform-views Embedding Android/iOS views in Flutter apps c: performance Relates to speed or footprint issues (see "perf:" labels) found in release: 3.22 Found to occur in 3.22 has reproducible steps The issue has been confirmed reproducible and is ready to work on perf: memory Performance issues related to memory platform-ios iOS applications specifically team-ios Owned by iOS platform team
Projects
None yet
Development

No branches or pull requests

4 participants