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

[Feature] switch to FlutterEngineGroup to save 99% memory usage #149

Open
chipweinberger opened this issue Jan 18, 2024 · 2 comments
Open

Comments

@chipweinberger
Copy link
Contributor

chipweinberger commented Jan 18, 2024

see here: https://docs.flutter.dev/add-to-app/multiple-flutters

The 2.0.0 Flutter release drastically reduces the memory footprint of additional Flutter engines from ~19MB on Android and ~13MB on iOS, to ~180kB on Android and iOS. This ~99% fixed cost reduction allows the multiple Flutters pattern to be used more liberally in your add-to-app integration.

sample code: https://github.com/flutter/samples/tree/master_archived/add_to_app/multiple_flutters

Here's a basic guide on how to use multiple Flutters in both Android and iOS projects:

For Android:

  1. Add Flutter to Your Project:
    Ensure you have Flutter integrated into your existing Android project. Follow the official guide if you haven't done this yet.

  2. Use FlutterEngineGroup:
    FlutterEngineGroup is a class that manages multiple FlutterEngine instances. You can create a single FlutterEngineGroup and use it to spawn multiple FlutterEngine instances.

    Example:

    FlutterEngineGroup engineGroup = new FlutterEngineGroup(context);
    
    // Create a FlutterEngine for a specific entrypoint or Dart file
    FlutterEngine flutterEngine = engineGroup.createAndRunEngine(context, entrypoint);
  3. Display Flutter UI:
    Use FlutterActivity or FlutterFragment to display the UI rendered by a FlutterEngine. Each instance can run different Dart entrypoints or the same entrypoint with different initial routes.

    Example:

    startActivity(
        FlutterActivity
            .withNewEngine()
            .initialRoute("/my_route")
            .build(context)
    );

For iOS:

  1. Add Flutter to Your Project:
    Integrate Flutter into your existing iOS project following the official integration guide.

  2. Use FlutterEngineGroup:
    On iOS, use FlutterEngineGroup to manage multiple FlutterEngine instances.

    Example:

    FlutterEngineGroup* engineGroup = [[FlutterEngineGroup alloc] initWithName:@"my_engines" project:nil];
    
    // Create a FlutterEngine
    FlutterEngine* flutterEngine = [engineGroup makeEngineWithEntrypoint:nil libraryURI:nil];
  3. Display Flutter UI:
    Use a FlutterViewController to display the Flutter content. Each FlutterEngine can be attached to its own FlutterViewController.

    Example:

    FlutterViewController* flutterViewController = [[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
    [self presentViewController:flutterViewController animated:YES completion:nil];

Notes:

  • Isolation: Each Flutter instance is isolated from others, meaning they do not share Dart execution contexts or memory.
  • Performance Consideration: Be mindful of the resource usage when spawning multiple Flutter instances, as each instance consumes memory and CPU.
  • Use Cases: This approach is ideal for apps that need to display multiple Flutter screens simultaneously, or apps that integrate Flutter incrementally.

For more detailed information and the latest updates, refer to the Flutter documentation and the multiple_flutters module in the official Flutter samples repository.

@chipweinberger
Copy link
Contributor Author

some discussion here: #111 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant