Use exit to avoid destructors in dex2oat.

Change-Id: I5f8b396dfa614af2935053c219b09b406a632618
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index 3848b6d..c4cce2f 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -17,6 +17,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/stat.h>
+#include <valgrind.h>
 
 #include <fstream>
 #include <iostream>
@@ -1072,6 +1073,13 @@
   if (dump_timing || (dump_slow_timing && timings.GetTotalNs() > MsToNs(1000))) {
     LOG(INFO) << Dumpable<base::TimingLogger>(timings);
   }
+
+  // Everything was successfully written, do an explicit exit here to avoid running Runtime
+  // destructors that take time (bug 10645725) unless we're a debug build or running on valgrind.
+  if (!kIsDebugBuild || (RUNNING_ON_VALGRIND == 0)) {
+    exit(EXIT_SUCCESS);
+  }
+
   return EXIT_SUCCESS;
 }