From cf1bffaa00a76cfeb2d85697b28e3e993e023602 Mon Sep 17 00:00:00 2001 From: Miguel Aranda <miguelaranda@google.com> Date: Thu, 19 Oct 2023 15:14:10 +0000 Subject: [PATCH] Allow explicit GC usage for StrictMode setClassInstanceLimit This wraps the calls to System.GC() in a disabling of the explicit gc checks from another strict mode so that those calls do not produce a crash when SetClassInstanceLimit is used. Change-Id: I54d2ff975838e3f54879de3fbff25614137b31df Test: atest testClassInstanceLimit Bug: 305288285 --- core/java/android/os/StrictMode.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/java/android/os/StrictMode.java b/core/java/android/os/StrictMode.java index 180735bf48eb8..6d572591f7be8 100644 --- a/core/java/android/os/StrictMode.java +++ b/core/java/android/os/StrictMode.java @@ -2023,9 +2023,13 @@ public final class StrictMode { return; } + // Temporarily disable checks so that explicit GC is allowed. + final int oldMask = getThreadPolicyMask(); + setThreadPolicyMask(0); System.gc(); System.runFinalization(); System.gc(); + setThreadPolicyMask(oldMask); // Note: classInstanceLimit is immutable, so this is lock-free // Create the classes array. -- GitLab