Don't allow GC to start if the runtime is shutting down

Daemon threads may be trying to do allocations as the runtime is
shutting down, don't allow GC to start since it touches the heap
and may cause crashes if it is running while we delete the heap
or thread list.

Bug: 18577101
Change-Id: I1404b41f2aee13d3c26acffdf588c985628beefc
diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc
index 6baa9b2..e019d11 100644
--- a/runtime/gc/heap.cc
+++ b/runtime/gc/heap.cc
@@ -2401,6 +2401,11 @@
                                                bool clear_soft_references) {
   Thread* self = Thread::Current();
   Runtime* runtime = Runtime::Current();
+  // Don't allow the GC to start if the runtime is shutting down. This can occur if a Daemon thread
+  // is still allocating.
+  if (runtime->IsShuttingDown(self)) {
+    return collector::kGcTypeNone;
+  }
   // If the heap can't run the GC, silently fail and return that no GC was run.
   switch (gc_type) {
     case collector::kGcTypePartial: {