Add a new startup option, --experimental_oom_more_eagerly. Features to have Bazel exit more eagerly due to an OOM will be guarded by this flag.

--
MOS_MIGRATED_REVID=116567102
diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc
index 336dbc2..9591a3c 100644
--- a/src/main/cpp/blaze.cc
+++ b/src/main/cpp/blaze.cc
@@ -271,6 +271,9 @@
   } else {
     result.push_back("--nodeep_execroot");
   }
+  if (globals->options.oom_more_eagerly) {
+    result.push_back("--experimental_oom_more_eagerly");
+  }
   if (globals->options.watchfs) {
     result.push_back("--watchfs");
   }
diff --git a/src/main/cpp/blaze_startup_options.cc b/src/main/cpp/blaze_startup_options.cc
index acb3b46..7a27abe 100644
--- a/src/main/cpp/blaze_startup_options.cc
+++ b/src/main/cpp/blaze_startup_options.cc
@@ -51,6 +51,7 @@
       batch_cpu_scheduling(rhs.batch_cpu_scheduling),
       io_nice_level(rhs.io_nice_level),
       max_idle_secs(rhs.max_idle_secs),
+      oom_more_eagerly(rhs.oom_more_eagerly),
       watchfs(rhs.watchfs),
       allow_configurable_attributes(rhs.allow_configurable_attributes),
       option_sources(rhs.option_sources),
diff --git a/src/main/cpp/blaze_startup_options.h b/src/main/cpp/blaze_startup_options.h
index 59b893e..d82bfad 100644
--- a/src/main/cpp/blaze_startup_options.h
+++ b/src/main/cpp/blaze_startup_options.h
@@ -146,6 +146,8 @@
 
   int max_idle_secs;
 
+  bool oom_more_eagerly;
+
   // If true, Blaze will listen to OS-level file change notifications.
   bool watchfs;
 
diff --git a/src/main/cpp/blaze_startup_options_common.cc b/src/main/cpp/blaze_startup_options_common.cc
index a640418..4b06335 100644
--- a/src/main/cpp/blaze_startup_options_common.cc
+++ b/src/main/cpp/blaze_startup_options_common.cc
@@ -50,6 +50,7 @@
   // 3 hours (but only 5 seconds if used within a test)
   max_idle_secs = testing ? 5 : (3 * 3600);
   webstatus_port = 0;
+  oom_more_eagerly = false;
   watchfs = false;
   invocation_policy = NULL;
 }
@@ -80,6 +81,7 @@
   lhs->io_nice_level = rhs.io_nice_level;
   lhs->max_idle_secs = rhs.max_idle_secs;
   lhs->webstatus_port = rhs.webstatus_port;
+  lhs->oom_more_eagerly = rhs.oom_more_eagerly;
   lhs->watchfs = rhs.watchfs;
   lhs->allow_configurable_attributes = rhs.allow_configurable_attributes;
   lhs->fatal_event_bus_exceptions = rhs.fatal_event_bus_exceptions;
@@ -204,6 +206,12 @@
   } else if (GetNullaryOption(arg, "-x")) {
     fprintf(stderr, "WARNING: The -x startup option is now ignored "
             "and will be removed in a future release\n");
+  } else if (GetNullaryOption(arg, "--experimental_oom_more_eagerly")) {
+    oom_more_eagerly = true;
+    option_sources["experimental_oom_more_eagerly"] = rcfile;
+  } else if (GetNullaryOption(arg, "--noexperimental_oom_more_eagerly")) {
+    oom_more_eagerly = false;
+    option_sources["experimental_oom_more_eagerly"] = rcfile;
   } else if (GetNullaryOption(arg, "--watchfs")) {
     watchfs = true;
     option_sources["watchfs"] = rcfile;
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeServerStartupOptions.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeServerStartupOptions.java
index 9f6ce88..bd07e16 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeServerStartupOptions.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeServerStartupOptions.java
@@ -166,6 +166,17 @@
           + "$OUTPUT_BASE")
   public boolean deepExecRoot;
 
+  @Option(
+    name = "experimental_oom_more_eagerly",
+    defaultValue = "false", // NOTE: purely decorative!  See class docstring.
+    category = "server startup",
+    help =
+        "If set, attempt to detect Java heap OOM conditions and exit before thrashing.  Only "
+            + "honored when --batch is also passed. In some cases, builds that previously succeeded"
+            + " may OOM if they were close to OOMing before."
+  )
+  public boolean oomMoreEagerly;
+
   @Option(name = "block_for_lock",
       defaultValue = "true", // NOTE: purely decorative!  See class docstring.
       category = "server startup",