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
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