diff --git a/engine/src/flutter/common/settings.h b/engine/src/flutter/common/settings.h index 3c060e2c81..b13416556f 100644 --- a/engine/src/flutter/common/settings.h +++ b/engine/src/flutter/common/settings.h @@ -207,6 +207,10 @@ struct Settings { // shells in the platform (via their embedding APIs) should cooperate to make // sure this flag is never set if they want the VM to shutdown and free all // associated resources. + // It can be customized by application, more detail: + // https://github.com/flutter/flutter/issues/95903 + // TODO(eggfly): Should it be set to false by default? + // https://github.com/flutter/flutter/issues/96843 bool leak_vm = true; // Engine settings diff --git a/engine/src/flutter/shell/common/switches.cc b/engine/src/flutter/shell/common/switches.cc index 491ca93197..e7b89bf673 100644 --- a/engine/src/flutter/shell/common/switches.cc +++ b/engine/src/flutter/shell/common/switches.cc @@ -384,6 +384,10 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) { command_line.GetOptionValue(FlagForSwitch(Switch::CacheDirPath), &settings.temp_directory_path); + bool leak_vm = "true" == command_line.GetOptionValueWithDefault( + FlagForSwitch(Switch::LeakVM), "true"); + settings.leak_vm = leak_vm; + if (settings.icu_initialization_required) { command_line.GetOptionValue(FlagForSwitch(Switch::ICUDataFilePath), &settings.icu_data_path); diff --git a/engine/src/flutter/shell/common/switches.h b/engine/src/flutter/shell/common/switches.h index 0a44e6cc58..61e5cc902a 100644 --- a/engine/src/flutter/shell/common/switches.h +++ b/engine/src/flutter/shell/common/switches.h @@ -215,6 +215,11 @@ DEF_SWITCH(EnableSkParagraph, "enable-skparagraph", "Selects the SkParagraph implementation of the text layout engine.") +DEF_SWITCH(LeakVM, + "leak-vm", + "When the last shell shuts down, the shared VM is leaked by default " + "(the leak_vm in VM settings is true). To clean up the leak VM, set " + "this value to false.") DEF_SWITCHES_END void PrintUsage(const std::string& executable_name); diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java index 0a3c81e87b..0278a767f5 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java @@ -40,6 +40,19 @@ public class FlutterLoader { private static final String ENABLE_SKPARAGRAPH_META_DATA_KEY = "io.flutter.embedding.android.EnableSkParagraph"; + /** + * Set whether leave or clean up the VM after the last shell shuts down. It can be set from app's + * meta-data in in AndroidManifest.xml. Set it to true in to leave the Dart VM, + * set it to false to destroy VM. + * + *

If your want to let your app destroy the last shell and re-create shells more quickly, set + * it to true, otherwise if you want to clean up the memory of the leak VM, set it to false. + * + *

TODO(eggfly): Should it be set to false by default? + * https://github.com/flutter/flutter/issues/96843 + */ + private static final String LEAK_VM_META_DATA_KEY = "io.flutter.embedding.android.LeakVM"; + // Must match values in flutter::switches static final String AOT_SHARED_LIBRARY_NAME = "aot-shared-library-name"; static final String AOT_VMSERVICE_SHARED_LIBRARY_NAME = "aot-vmservice-shared-library-name"; @@ -299,6 +312,9 @@ public class FlutterLoader { shellArgs.add("--enable-skparagraph"); } + final String leakVM = isLeakVM(metaData) ? "true" : "false"; + shellArgs.add("--leak-vm=" + leakVM); + long initTimeMillis = SystemClock.uptimeMillis() - initStartTimestampMillis; flutterJNI.init( @@ -318,6 +334,14 @@ public class FlutterLoader { } } + private static boolean isLeakVM(@Nullable Bundle metaData) { + final boolean leakVMDefaultValue = true; + if (metaData == null) { + return leakVMDefaultValue; + } + return metaData.getBoolean(LEAK_VM_META_DATA_KEY, leakVMDefaultValue); + } + /** * Same as {@link #ensureInitializationComplete(Context, String[])} but waiting on a background * thread, then invoking {@code callback} on the {@code callbackHandler}. diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/loader/FlutterLoaderTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/loader/FlutterLoaderTest.java index 854a23182f..3d3dd15229 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/loader/FlutterLoaderTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/loader/FlutterLoaderTest.java @@ -21,6 +21,7 @@ import static org.robolectric.Shadows.shadowOf; import android.annotation.TargetApi; import android.app.ActivityManager; import android.content.Context; +import android.os.Bundle; import io.flutter.embedding.engine.FlutterJNI; import java.util.Arrays; import java.util.List; @@ -85,6 +86,58 @@ public class FlutterLoaderTest { assertTrue(arguments.contains(oldGenHeapArg)); } + @Test + public void itSetsLeakVMToTrueByDefault() { + FlutterJNI mockFlutterJNI = mock(FlutterJNI.class); + FlutterLoader flutterLoader = new FlutterLoader(mockFlutterJNI); + + assertFalse(flutterLoader.initialized()); + flutterLoader.startInitialization(RuntimeEnvironment.application); + flutterLoader.ensureInitializationComplete(RuntimeEnvironment.application, null); + shadowOf(getMainLooper()).idle(); + + final String leakVMArg = "--leak-vm=true"; + ArgumentCaptor shellArgsCaptor = ArgumentCaptor.forClass(String[].class); + verify(mockFlutterJNI, times(1)) + .init( + eq(RuntimeEnvironment.application), + shellArgsCaptor.capture(), + anyString(), + anyString(), + anyString(), + anyLong()); + List arguments = Arrays.asList(shellArgsCaptor.getValue()); + assertTrue(arguments.contains(leakVMArg)); + } + + @Test + public void itSetsTheLeakVMFromMetaData() { + FlutterJNI mockFlutterJNI = mock(FlutterJNI.class); + FlutterLoader flutterLoader = new FlutterLoader(mockFlutterJNI); + Bundle metaData = new Bundle(); + metaData.putBoolean("io.flutter.embedding.android.LeakVM", false); + RuntimeEnvironment.application.getApplicationInfo().metaData = metaData; + + FlutterLoader.Settings settings = new FlutterLoader.Settings(); + assertFalse(flutterLoader.initialized()); + flutterLoader.startInitialization(RuntimeEnvironment.application, settings); + flutterLoader.ensureInitializationComplete(RuntimeEnvironment.application, null); + shadowOf(getMainLooper()).idle(); + + final String leakVMArg = "--leak-vm=false"; + ArgumentCaptor shellArgsCaptor = ArgumentCaptor.forClass(String[].class); + verify(mockFlutterJNI, times(1)) + .init( + eq(RuntimeEnvironment.application), + shellArgsCaptor.capture(), + anyString(), + anyString(), + anyString(), + anyLong()); + List arguments = Arrays.asList(shellArgsCaptor.getValue()); + assertTrue(arguments.contains(leakVMArg)); + } + @Test public void itUsesCorrectExecutorService() { FlutterJNI mockFlutterJNI = mock(FlutterJNI.class); diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm index 27700f8145..08d788c902 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm @@ -160,6 +160,13 @@ flutter::Settings FLTDefaultSettingsForBundle(NSBundle* bundle) { NSNumber* enableSkParagraph = [mainBundle objectForInfoDictionaryKey:@"FLTEnableSkParagraph"]; settings.enable_skparagraph = (enableSkParagraph != nil) ? enableSkParagraph.boolValue : false; + // Leak Dart VM settings, set whether leave or clean up the VM after the last shell shuts down. + NSNumber* leakDartVM = [mainBundle objectForInfoDictionaryKey:@"FLTLeakDartVM"]; + // It will change the default leak_vm value in settings only if the key exists. + if (leakDartVM != nil) { + settings.leak_vm = leakDartVM.boolValue; + } + #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG // There are no ownership concerns here as all mappings are owned by the // embedder and not the engine. diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProjectTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProjectTest.mm index 56cc57c204..234e4678e6 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProjectTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProjectTest.mm @@ -39,6 +39,17 @@ FLUTTER_ASSERT_ARC [FlutterDartProject domainNetworkPolicy:appTransportSecurity]); } +- (void)testLeakDartVMSettingsAreCorrectlyParsed { + // The FLTLeakDartVM's value is defined in Info.plist + NSBundle* mainBundle = [NSBundle mainBundle]; + NSNumber* leakDartVM = [mainBundle objectForInfoDictionaryKey:@"FLTLeakDartVM"]; + XCTAssertEqual(leakDartVM.boolValue, NO); + + auto settings = FLTDefaultSettingsForBundle(); + // Check settings.leak_vm value is same as the value defined in Info.plist. + XCTAssertEqual(settings.leak_vm, NO); +} + - (void)testEmptySettingsAreCorrect { XCTAssertFalse([FlutterDartProject allowsArbitraryLoads:[[NSDictionary alloc] init]]); XCTAssertEqualObjects(@"", [FlutterDartProject domainNetworkPolicy:[[NSDictionary alloc] init]]); diff --git a/engine/src/flutter/testing/ios/IosUnitTests/App/Info.plist b/engine/src/flutter/testing/ios/IosUnitTests/App/Info.plist index 52b6c20501..dba0dc0a63 100644 --- a/engine/src/flutter/testing/ios/IosUnitTests/App/Info.plist +++ b/engine/src/flutter/testing/ios/IosUnitTests/App/Info.plist @@ -44,6 +44,8 @@ + FLTLeakDartVM + UIRequiredDeviceCapabilities armv7