Skip to content

Pointers compression in iOS makes apps more easily to OOM in production. #105183

@yeatse

Description

@yeatse

Recently we upgraded our app to latest stable version of flutter, and encountered much more crashes in production. Most of the crashes are unrelated to flutter itself, but they are all memory issues.

Steps to Reproduce

  1. flutter create bug, then replace ios/main.swift as below
  2. Run this sample in iPhone 7 (or other low-end iOS devices, physical devices are required), and wait for 3 seconds.

Expected results:

There should not be any issue allocating the memory, because iPhone 7 has 2GB RAM.

Actual results:

malloc returned nil even though the memory footprint is low.

In practical situations I would never allocate a huge chunk of memory by myself, but in my app there are many features which will cost a lot of memory, like map views, web views, or editing a large photo, and after upgrading they are much more fragile than before.

Code sample
import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    
    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3)) {
      let ptr = malloc(1024 * 1024 * 1024) // 1GB
      print("memory allocated: \(String(describing: ptr))")
    }
    
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}
Logs
[✓] Flutter (Channel stable, 3.0.1, on macOS 12.4 21F79 darwin-arm, locale zh-Hans-CN)
    • Flutter version 3.0.1 at /Users/yeatse/Developer/Repo/flutter/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision fb57da5f94 (13 days ago), 2022-05-19 15:50:29 -0700
    • Engine revision caaafc5604
    • Dart version 2.17.1
    • DevTools version 2.12.2

[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.


[✓] Xcode - develop for iOS and macOS (Xcode 13.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE = /Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).

[✓] VS Code (version 1.67.2)
    • VS Code at /Users/yeatse/Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.42.0

[✓] Proxy Configuration
    • HTTP_PROXY is set
    • NO_PROXY is 127.0.0.1,localhost,::1
    • NO_PROXY contains localhost
    • NO_PROXY contains 127.0.0.1
    • NO_PROXY contains ::1

[✓] Connected device (3 available)
    • CC's iPhone 7 (mobile) • ff750f59fe91f87eed8aff9c32219c2cf7e2d23c • ios            • iOS 15.2.1 19C63
    • macOS (desktop)        • macos                                    • darwin-arm64   • macOS 12.4 21F79 darwin-arm
    • Chrome (web)           • chrome                                   • web-javascript • Microsoft Edge 102.0.1245.30
    ! Error: CC’s iPhone 12 Pro is not connected. Xcode will continue when CC’s iPhone 12 Pro is connected. (code -13)
    ! Error: CC's iPhone 7 is busy: Fetching debug symbols for CC's iPhone 7. Xcode will continue when CC's iPhone 7 is finished. (code -10)

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

! Doctor found issues in 2 categories.

Question

After bisecting I believe that the problem comes from flutter/engine#30333, as when I revert it in my local build of flutter, or switch flutter to the commit before #95364, the problem disappears. But I really want the pointer compression feature in my app, so what should I do?

From comment by @rmacnak-google in #95482 (comment)

madvise(DONTNEED) is lazy on macOS/iOS: the pages are not immediately released and RSS does not immediately decrease as on Linux/Android, but the pages will be released once there is memory pressure.

Is lazy releasing of pages causing this issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listc: performanceRelates to speed or footprint issues (see "perf:" labels)dependency: dartDart team may need to help usengineflutter/engine related. See also e: labels.found in release: 3.0Found to occur in 3.0found in release: 3.1Found to occur in 3.1has 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 versionwaiting for PR to land (fixed)A fix is in flight

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions