Merge "Fix oat_file_assistant_test32 flaky failures with GSS collector."
diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc
index 0cad11f..dff8f4d 100644
--- a/runtime/gc/heap.cc
+++ b/runtime/gc/heap.cc
@@ -291,10 +291,18 @@
     // Try to reserve virtual memory at a lower address if we have a separate non moving space.
     request_begin = reinterpret_cast<uint8_t*>(300 * MB);
   }
+  // Attempt to create 2 mem maps at or after the requested begin.
   if (foreground_collector_type_ != kCollectorTypeCC) {
-    // Attempt to create 2 mem maps at or after the requested begin.
-    main_mem_map_1.reset(MapAnonymousPreferredAddress(kMemMapSpaceName[0], request_begin, capacity_,
-                                                      &error_str));
+    if (separate_non_moving_space) {
+      main_mem_map_1.reset(MapAnonymousPreferredAddress(kMemMapSpaceName[0], request_begin,
+                                                        capacity_, &error_str));
+    } else {
+      // If no separate non-moving space, the main space must come
+      // right after the image space to avoid a gap.
+      main_mem_map_1.reset(MemMap::MapAnonymous(kMemMapSpaceName[0], request_begin, capacity_,
+                                                PROT_READ | PROT_WRITE, true, false,
+                                                &error_str));
+    }
     CHECK(main_mem_map_1.get() != nullptr) << error_str;
   }
   if (support_homogeneous_space_compaction ||
diff --git a/runtime/oat_file_assistant_test.cc b/runtime/oat_file_assistant_test.cc
index 71679ae..be8652c 100644
--- a/runtime/oat_file_assistant_test.cc
+++ b/runtime/oat_file_assistant_test.cc
@@ -203,7 +203,9 @@
     // Ensure a chunk of memory is reserved for the image space.
     uintptr_t reservation_start = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MIN_DELTA;
     uintptr_t reservation_end = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MAX_DELTA
-      + 100 * 1024 * 1024;
+        // Include the main space that has to come right after the
+        // image in case of the GSS collector.
+        + 384 * MB;
 
     std::string error_msg;
     std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));