Skip to content

Stacking Widgets on top of UiKitView results in Memory explosion #154549

Closed
flutter-team-archive/engine
#55036

Description

@Heidi0039

Steps to reproduce

  1. Clone https://github.com/rohit11/Flutter_PlatformView.
    I stumbled upon this public example when trying to reproduce this issue without setting up too much myself, and it perfectly illustrates the problem. Thanks rohit11 ;)

  2. Replace myapp.dart with the one from CodeSample

  3. Tap the button

Expected results

Tapping the Button does not affect the devices memory, as the button itself has no logic in its onPressed.

Actual results

Tapping the Button increases the apps memory usage indefinitely until it is killed by the os.

Code sample

import 'package:flutter/material.dart';

class MyApp extends StatelessWidget {

  Widget build(context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Platform View'),
        ),
        body: SafeArea(child: Stack(children: [
          SizedBox(
            width: 380,
            height: 380,
            child: UiKitView(
              viewType: 'NativeView',
            ),
          ),
          Stack(
            children: List<Widget>.generate(10000, (index) {
              // The problem already happens with a small amount of widgets.
              // Using an excessive amount of widgets is just to make the problem more evident.
              return Text("Lots of Texts represent a Widget with complex components.");
            }),
          ),

          Align(
            alignment: Alignment.bottomCenter,
            child:
            TextButton(
              child: Text("Button"),
              onPressed: () {
                print("Tap ${DateTime.now()}");
              },
            ),
          ),
        ],
        ),
        ),
      ),
    );
  }
}

Screenshots or Video

Screenshot 2024-08-22 at 16 09 09

As far as I can tell, there are IOGPUMetalResources allocated, which are never released and persist until the App runs out of memory?
Screenshot 2024-09-03 at 15 50 02

Logs

No response

Flutter Doctor output

[!] Flutter (Channel [user-branch], 3.24.1, on macOS 15.0 24A5320a darwin-arm64, locale en-DE)
! Flutter version 3.24.1 on channel [user-branch] at /Users/heidenthal/Projects/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/setup.
! Upstream repository unknown source is not a standard remote.
Set environment variable "FLUTTER_GIT_URL" to unknown source to dismiss this error.
• Framework revision 5874a72 (2 weeks ago), 2024-08-20 16:46:00 -0500
• Engine revision c9b9d5780d
• Dart version 3.5.1
• DevTools version 2.37.2
• 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 34.0.0)
• Android SDK at /Users/heidenthal/Library/Android/sdk
• Platform android-34, build-tools 34.0.0
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
• All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.0)
• Xcode at /Users/heidenthal/Downloads/Xcode-beta.app/Contents/Developer
• Build 16A5202i
• CocoaPods version 1.15.2

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

[✓] Android Studio (version 2022.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.6+0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.92.2)
• VS Code at /Users/heidenthal/Downloads/Visual Studio Code.app/Contents
• Flutter extension can be installed from:
🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (5 available)
• Heidis iPhone (mobile) • 00008110-000E60AA0C40401E • ios • iOS 18.0 22A5350a
• iPhone14Pro (mobile) • 00008120-001251C20A38C01E • ios • iOS 17.3 21D50
• macOS (desktop) • macos • darwin-arm64 • macOS 15.0 24A5320a darwin-arm64
• Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin • macOS 15.0 24A5320a darwin-arm64
• Chrome (web) • chrome • web-javascript • Google Chrome 128.0.6613.114

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

Note

Please note, that I am using a flutter 3.24.1 user-branch in the flutter doctor output as I have played around with this bug for a while. It does happen on the stable 3.24.1 version as well.
I am also currently using the MacOS Sequoia Beta including the xCode 16 Beta, but had the same issue a few weeks before when I was still using the official MacOS release with xCode 15.4.

The memory also only appears in the xCode memory profiler, not in the flutter profiler.

Activity

  1. added
    in triagePresently being triaged by the triage team
    on Sep 4, 2024
  2. huycozy commented on Sep 4, 2024

    @huycozy
    Member

    I ran my complete sample code at ios-stack-memory-154549 on XCode in profile build configuration and observed more memory is consumed every time the button is pressed. The crash will happen if I keep pressing the button. Checked on Flutter master channel 3.25.0-1.0.pre.248 with my device iPhone 7, iOS 15.8.

    If I run the project from Flutter, I can see the fatal crash as below:

    Output fatal crash log
    Launching lib/main.dart on iPhone in debug mode...
    Automatically signing iOS for device deployment using specified development team in Xcode project: X8NNQ9CYL2
    Xcode build done.                                           23.6s
    Connecting to VM Service at ws://127.0.0.1:61182/rVUDkzrqfkg=/ws
    Connected to the VM Service.
    flutter: Tap 2024-09-04 12:45:04.248361
    flutter: Tap 2024-09-04 12:45:09.623903
    flutter: Tap 2024-09-04 12:45:09.628814
    flutter: Tap 2024-09-04 12:45:13.063089
    [ServicesDaemonManager] interruptionHandler is called. -[FontServicesDaemonManager connection]_block_invoke
    flutter: Tap 2024-09-04 12:45:16.530956
    flutter: Tap 2024-09-04 12:45:16.538780
    flutter: Tap 2024-09-04 12:45:21.907475
    flutter: Tap 2024-09-04 12:45:23.788225
    flutter: Tap 2024-09-04 12:45:23.800548
    * thread #9, name = 'io.flutter.1.raster', stop reason = EXC_RESOURCE (RESOURCE_TYPE_MEMORY: high watermark memory limit exceeded) (limit=1450 MB)
        frame #0: 0x00000001dcec9c40 AGXMetalA10`___lldb_unnamed_symbol2137 + 832
    AGXMetalA10`___lldb_unnamed_symbol2137:
    ->  0x1dcec9c40 <+832>: str    x9, [x11]
        0x1dcec9c44 <+836>: str    w8, [x11, #0x8]
        0x1dcec9c48 <+840>: add    x3, x20, #0x2b0
        0x1dcec9c4c <+844>: ldrb   w4, [x20, #0x4f0]
    Target 0: (Runner) stopped.
    Lost connection to device.
    
    Exited.
    flutter doctor -v (master)
    [!] Flutter (Channel master, 3.25.0-1.0.pre.248, on macOS 14.6 23G80 darwin-x64, locale en-VN)
        • Flutter version 3.25.0-1.0.pre.248 on channel master at /Users/huynq/Documents/GitHub/flutter_master
        ! Warning: `flutter` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
        ! Warning: `dart` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 182c266961 (2 hours ago), 2024-09-03 17:20:16 -0700
        • Engine revision 8d7d5ff693
        • Dart version 3.6.0 (build 3.6.0-198.0.dev)
        • DevTools version 2.39.0
        • 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/huynq/Library/Android/sdk
        • Platform android-35, build-tools 35.0.0
        • ANDROID_HOME = /Users/huynq/Library/Android/sdk
        • 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/Xcode15.4.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 2024.1)
        • Android Studio at /Applications/Android Studio Koala.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.11+0-17.0.11b1207.24-11852314)
    
    [✓] 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
        • android-studio-dir = /Applications/Android Studio.app/
        • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
    
    [✓] IntelliJ IDEA Ultimate Edition (version 2024.2.0.2)
        • IntelliJ at /Applications/IntelliJ IDEA.app
        • Flutter plugin version 81.1.3
        • Dart plugin version 242.20629
    
    [✓] VS Code (version 1.92.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.94.0
    
    [✓] Connected device (2 available)
        • macOS (desktop) • macos  • darwin-x64     • macOS 14.6 23G80 darwin-x64
        • Chrome (web)    • chrome • web-javascript • Google Chrome 128.0.6613.119
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.

    Perhaps this is somewhat similar to #150823 (is polyline also rendered with Stack widgets?)

  3. added
    platform-iosiOS applications specifically
    engineflutter/engine related. See also e: labels.
    a: platform-viewsEmbedding Android/iOS views in Flutter apps
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    c: fatal crashCrashes that terminate the process
    team-iosOwned by iOS platform team
    perf: memoryPerformance issues related to memory
    and removed
    in triagePresently being triaged by the triage team
    on Sep 4, 2024
  4. littleGnAl commented on Sep 9, 2024

    @littleGnAl
    Contributor
    class _MyHomePageState extends State<MyHomePage> {
    ...
    
      @override
      Widget build(BuildContext context) {
        // This method is rerun every time setState is called, for instance as done
        // by the _incrementCounter method above.
        //
        // The Flutter framework has been optimized to make rerunning build methods
        // fast, so that you can just rebuild anything that needs updating rather
        // than having to individually change instances of widgets.
        return Scaffold(
          appBar: AppBar(
            // TRY THIS: Try changing the color here to a specific color (to
            // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
            // change color while the other colors stay the same.
            backgroundColor: Theme.of(context).colorScheme.inversePrimary,
            // Here we take the value from the MyHomePage object that was created by
            // the App.build method, and use it to set our appbar title.
            title: Text(widget.title),
          ),
          body: const Stack(
            children: [
              MyPlatformView(), // Follow https://docs.flutter.dev/platform-integration/ios/platform-views to implement it.
              ...List<Widget>.generate(10000, (index) {
                return const CircularProgressIndicator.adaptive();
              }),
            ],
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ), // This trailing comma makes auto-formatting nicer for build methods.
        );
      }
    }

    In my case, I can reproduce it by adding a CircularProgressIndicator.adaptive() widget without any interaction.
    image

  5. littleGnAl commented on Sep 9, 2024

    @littleGnAl
    Contributor

    If I disable Impeller, the memory will stop growing continuously.

    image image
  6. added
    r: fixedIssue is closed as already fixed in a newer version
    on Sep 10, 2024
  7. github-actions commented on Sep 25, 2024

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  8. locked as resolved and limited conversation to collaborators on Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: platform-viewsEmbedding Android/iOS views in Flutter appsc: fatal crashCrashes that terminate the processengineflutter/engine related. See also e: labels.found in release: 3.24Found to occur in 3.24found in release: 3.25Found to occur in 3.25has reproducible stepsThe issue has been confirmed reproducible and is ready to work onperf: memoryPerformance issues related to memoryplatform-iosiOS applications specificallyr: fixedIssue is closed as already fixed in a newer versionteam-iosOwned by iOS platform team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions