ART: Prune FindArrayClass cache in image writer

The ClassLinker cache speeds up FindArrayClass requests, but all
entries are roots. It is possible that an entry is a non-image
class when creating the boot image, artificially keeping the
class around.

Bug: 21596650

(cherry picked from commit 44905ce1c97613a5cb44046049843fe1029a64cf)

Change-Id: Ief9b439945d0e293a3cb5dcddfeb189b5e174f06
diff --git a/compiler/image_writer.cc b/compiler/image_writer.cc
index 11743f3..32bde8e 100644
--- a/compiler/image_writer.cc
+++ b/compiler/image_writer.cc
@@ -718,6 +718,9 @@
     // contains data only valid during a real run.
     dex_cache->SetFieldObject<false>(mirror::DexCache::DexOffset(), nullptr);
   }
+
+  // Drop the array class cache in the ClassLinker, as these are roots holding those classes live.
+  class_linker->DropFindArrayClassCache();
 }
 
 bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index 431ef27..31140a8 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -277,9 +277,9 @@
       quick_to_interpreter_bridge_trampoline_(nullptr),
       image_pointer_size_(sizeof(void*)) {
   CHECK(intern_table_ != nullptr);
-  for (auto& root : find_array_class_cache_) {
-    root = GcRoot<mirror::Class>(nullptr);
-  }
+  static_assert(kFindArrayCacheSize == arraysize(find_array_class_cache_),
+                "Array cache size wrong.");
+  std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr));
 }
 
 void ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path) {
@@ -5886,4 +5886,9 @@
   return method;
 }
 
+void ClassLinker::DropFindArrayClassCache() {
+  std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr));
+  find_array_class_cache_next_victim_ = 0;
+}
+
 }  // namespace art
diff --git a/runtime/class_linker.h b/runtime/class_linker.h
index fa8b2e7..d9935cb 100644
--- a/runtime/class_linker.h
+++ b/runtime/class_linker.h
@@ -480,6 +480,10 @@
 
   ArtMethod* CreateRuntimeMethod();
 
+  // Clear the ArrayClass cache. This is necessary when cleaning up for the image, as the cache
+  // entries are roots, but potentially not image classes.
+  void DropFindArrayClassCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
+
  private:
   const OatFile::OatMethod FindOatMethodFor(ArtMethod* method, bool* found)
       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);