Skip to content

Free resources after a Flutter view is destroyed #16995

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

Closed
mehmetf opened this issue Apr 26, 2018 · 35 comments
Closed

Free resources after a Flutter view is destroyed #16995

mehmetf opened this issue Apr 26, 2018 · 35 comments
Assignees
Labels
a: existing-apps Integration with existing apps via the add-to-app flow customer: alibaba dependency: dart Dart team may need to help us engine flutter/engine repository. See also e: labels. platform-android Android applications specifically platform-ios iOS applications specifically waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds waiting for PR to land (fixed) A fix is in flight
Milestone

Comments

@mehmetf
Copy link
Contributor

mehmetf commented Apr 26, 2018

Initial experiments are showing that there's a significant bit of memory still in use after the activity that has a Flutter view has finished. E.g. we should be able to drop down to near 0 memory usage once there isn't any active Flutter views.

The methodology of experiment and results are detailed in an internal document that was shared with Flutter team.

@mehmetf mehmetf added the a: existing-apps Integration with existing apps via the add-to-app flow label Apr 26, 2018
@eseidelGoogle
Copy link
Contributor

eseidelGoogle commented Apr 26, 2018

@chinmaygarde points out in that doc that it's possible to get to 0 after closing the last view, but it requires shutting down the Dart VM, there is now way to restart the DartVM after shutdown. I didn't find a bug on that, so I filed one: dart-lang/sdk#32990

@mehmetf
Copy link
Contributor Author

mehmetf commented Aug 11, 2018

Latest experiments show that this is a big problem on iOS also.

@mehmetf mehmetf added platform-android Android applications specifically platform-ios iOS applications specifically customer: truth (g3) labels Aug 11, 2018
@eseidelGoogle
Copy link
Contributor

This is a blocking issue for customer: truth's evals. Not sure what the right label for that is, adding customer: critical for now.

@chinmaygarde
Copy link
Member

During the shell refactor, I did add the ability to cleanly shut down the VM. However, since the VM couldn't be started back up after shutdown, currently, the sole instance of the VM is held in a single global pointer. Once the issue of not being able to call Dart_Initialize multiple times is settled, the engine should be able to correctly cleanup all VM related resources. The iOS embedder at that point should fully cleanup all resources. The Android API still has a few references to the the singleton FlutterMain that we should get to deprecating and cleaning up in the meantime however. I don't expect this to hold significant resource but still.

@chinmaygarde
Copy link
Member

cc @jason-simmons

@Hixie
Copy link
Contributor

Hixie commented Aug 14, 2018

cc @a-siva

@a-siva
Copy link
Contributor

a-siva commented Aug 15, 2018

We have some static state in the Dart VM which either needs to be cleaned or made fields of a singleton object that gets deleted in Dart_Cleanup, will track this in the Dart issue dart-lang/sdk#21106

@Hixie Hixie added the dependency: dart Dart team may need to help us label Aug 28, 2018
@Hixie
Copy link
Contributor

Hixie commented Aug 28, 2018

This issue is currently blocked on the Dart VM getting the ability to restart after being shut down.

@Hixie
Copy link
Contributor

Hixie commented Sep 18, 2018

We believe this will be unblocked soon.

@Hixie Hixie added this to the Goals milestone Sep 18, 2018
@chinmaygarde
Copy link
Member

@Hixie Is there a bug tracking this? After that is fixed, I still need to apply a minor patch to the engine to let go of the VM and reacquire it.

@Hixie
Copy link
Contributor

Hixie commented Sep 25, 2018

@chinmaygarde the Dart bug is dart-lang/sdk#21106

@Hixie
Copy link
Contributor

Hixie commented Oct 16, 2018

Latest status I could find was: https://dart-review.googlesource.com/c/sdk/+/76561#message-e413f837201d18a22e7ee2d458176d5424fc98b0
@bkonyi I hear you are working on this, is that correct? Can you let us know what the status is? Thanks!

@bkonyi
Copy link
Contributor

bkonyi commented Oct 16, 2018

The VM should be able to cleanup and reinitialize properly after this CL is fixed and landed (@chinmaygarde).

@Hixie
Copy link
Contributor

Hixie commented Oct 30, 2018

Looks like that landed. Is this ready to be implemented on the Flutter engine side now?

@Hixie
Copy link
Contributor

Hixie commented Oct 31, 2018

@a-siva Looks like patching in flutter/engine#6387 still causes failures. Could someone familiar with the VM patch that diff in locally and work past the crashes until all the unit tests pass? (./out/host_debug_unopt/runtime_unittests)

@a-siva
Copy link
Contributor

a-siva commented Nov 7, 2018

We have one Dart VM change related to this that is missing in the current Flutter version https://dart-review.googlesource.com/c/sdk/+/81442.
We are in the process of updating all the cherry picks for Dart into the 2.1_dev_9.x branch
(see https://github.com/dart-lang/sdk/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+label%3Amerge-to-dev)
Once this is created we will roll this into the flutter engine.

I have patched this locally and tried the unit test and it passes just fine on Linux. On mac one of the unit test fails after about 500 iterations and I think it might be an engine or test issue. Need to chat with @chinmaygarde regarding this.

@dnfield
Copy link
Contributor

dnfield commented Nov 16, 2018

I don't think this covers everthing in here, but a lot of it will be covered when flutter/engine#6879 lands on the iOS side.

@Hixie Hixie modified the milestones: February 2019, March 2019 Feb 6, 2019
@cbracken
Copy link
Member

@chinmaygarde now that flutter/engine#7832 is landed, are we good to close this?

@chinmaygarde
Copy link
Member

The patch had to be temporarily rolled back due to a failing test. The VM has been patched and I am awaiting a roll before re-landing. The tracking issue for this is filed here: #28184

@chinmaygarde
Copy link
Member

Doing this safely has turned into a massive yak shave. The current issues being worked upon are as follows:
dart-lang/sdk#36502: Defer |Dart_Cleanup| till no isolates are in the middle of initialization. #36502
dart-lang/sdk#36503: Respect the message handler setting on the service isolate as it is shutting down. #36503
dart-lang/sdk#36504: "main" on the service isolate should be by posting a message on that isolate's message handler. #36504
dart-lang/sdk#36505: Let embedders create the service isolates on a thread of their choosing. #36505

Once the VM issues are resolved (I am working with the Dart team on this), I will have to make another pass and enable more unit-tests around secondary isolate spawns that may uncover more bugs. Hard to estimate the time remaining for this reason.

@chinmaygarde
Copy link
Member

The Shell and the VM and can now safely be shutdown and cleanup all resources necessary at runtime. The shell and runtime unittests in the engine have been updated to verify this as well. The original patch that exposed this behavior to embedders was reverted because existing Flutter applications depend on "warming-up" the VM before launching the Flutter application. In the new world, there must be a shell instance running to prevent the VM from shutting down, or, a VM reference obtained from a running shell and reused across multiple shell instantiations. Without this extra bit of care, the work done to "warm-up" the Flutter application will lead to the a startup and shutdown of the VM (expensive) in the warm-up phase followed by another VM launch. After multiple benchmarks regressed, embedders were given back the old behavior by by adding the appropriately named Settings.leak_vm flag.

Now that the engine can guarantee thread safety of VM re-initialization, existing embedding APIs must be patched to account for this new behavior and set the leak_vm flag to false, templates updated and some strategy devised for existing applications in the wild depending on the warm-up behavior.

@chinmaygarde chinmaygarde removed their assignment Apr 18, 2019
@cbracken
Copy link
Member

@chinmaygarde just to clarify, the remaining work here is all in the Dart VM -- specifically the four issues above? Should this be reassigned to @rmacnak-google / @a-siva for tracking?

@chinmaygarde
Copy link
Member

No. Thanks to Ryan's patch in the Dart SDK related to safe shut-down of internal isolates, we should be good to go. The other open issues are still valid but are no longer blocking Flutter.

The next step is to audit existing applications/benchmarks to account for this new behavior. This is an open ended task that I am not sure how to proceed on. Original discussion that led to consensus on keeping the old behavior till embedders migrate was made here for reference.

@kf6gpe kf6gpe assigned kf6gpe and unassigned kf6gpe Apr 23, 2019
@Hixie
Copy link
Contributor

Hixie commented Apr 23, 2019

If the original issue here is solved, can we move the remaining work to a new bug that clearly describes what problem remains to be fixed?

Are the originally affected customers satisfied with the current resolution?

@Hixie Hixie added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Apr 23, 2019
@cbracken cbracken self-assigned this Apr 29, 2019
@cbracken
Copy link
Member

Assigning to myself to break out sub-bugs and close this one.

@no-response
Copy link

no-response bot commented May 15, 2019

Without additional information, we are unfortunately not sure how to resolve this issue. We are therefore reluctantly going to close this bug for now. Please don't hesitate to comment on the bug if you have any more information for us; we will reopen it right away!
Thanks for your contribution.

@no-response no-response bot closed this as completed May 15, 2019
@Adam-VA
Copy link

Adam-VA commented Jul 13, 2021

@chinmaygarde points out in that doc that it's possible to get to 0 after closing the last view, but it requires shutting down the Dart VM, there is now way to restart the DartVM after shutdown. I didn't find a bug on that, so I filed one: dart-lang/sdk#32990

I got the same issue, how could I shout down the Dart VM and restart the DartVM after shutdown

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: existing-apps Integration with existing apps via the add-to-app flow customer: alibaba dependency: dart Dart team may need to help us engine flutter/engine repository. See also e: labels. platform-android Android applications specifically platform-ios iOS applications specifically waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds waiting for PR to land (fixed) A fix is in flight
Projects
None yet
Development

No branches or pull requests