From 7b8091ad7c0292bc17c666a9eae27a2e0f7ee4da Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 8 Jun 2021 10:58:59 -0700 Subject: [PATCH] ResourcesImpl.ThemeImpl NativeAllocationRegistry Native allocations that hold theme data can be several KBs. Registering the native allocation using NativeAllocationRegistry helps induce the GC to free the malloced memory sooner and alleviate memory pressure. Bug: 187883085 Bug: 141198925 Test: atest ResourcesPerfWorkloads Change-Id: I2710cfea19565ea8aaf2b5fbd7b2c05d9cb17182 --- core/java/android/content/res/AssetManager.java | 7 +++++-- core/java/android/content/res/ResourcesImpl.java | 7 +++++++ core/jni/android_util_AssetManager.cpp | 8 ++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/core/java/android/content/res/AssetManager.java b/core/java/android/content/res/AssetManager.java index 52e54af99375e..65002547c2f08 100644 --- a/core/java/android/content/res/AssetManager.java +++ b/core/java/android/content/res/AssetManager.java @@ -1174,11 +1174,14 @@ public final class AssetManager implements AutoCloseable { void releaseTheme(long themePtr) { synchronized (this) { - nativeThemeDestroy(themePtr); decRefsLocked(themePtr); } } + static long getThemeFreeFunction() { + return nativeGetThemeFreeFunction(); + } + void applyStyleToTheme(long themePtr, @StyleRes int resId, boolean force) { synchronized (this) { // Need to synchronize on AssetManager because we will be accessing @@ -1580,7 +1583,7 @@ public final class AssetManager implements AutoCloseable { // Theme related native methods private static native long nativeThemeCreate(long ptr); - private static native void nativeThemeDestroy(long themePtr); + private static native long nativeGetThemeFreeFunction(); private static native void nativeThemeApplyStyle(long ptr, long themePtr, @StyleRes int resId, boolean force); private static native void nativeThemeRebase(long ptr, long themePtr, @NonNull int[] styleIds, diff --git a/core/java/android/content/res/ResourcesImpl.java b/core/java/android/content/res/ResourcesImpl.java index 819b01dfc58c9..b9f93b85f0bf0 100644 --- a/core/java/android/content/res/ResourcesImpl.java +++ b/core/java/android/content/res/ResourcesImpl.java @@ -54,6 +54,8 @@ import android.view.DisplayAdjustments; import com.android.internal.util.GrowingArrayUtils; +import libcore.util.NativeAllocationRegistry; + import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -1265,6 +1267,10 @@ public class ResourcesImpl { return new ThemeImpl(); } + private static final NativeAllocationRegistry sThemeRegistry = + NativeAllocationRegistry.createMalloced(ResourcesImpl.class.getClassLoader(), + AssetManager.getThemeFreeFunction()); + public class ThemeImpl { /** * Unique key for the series of styles applied to this theme. @@ -1283,6 +1289,7 @@ public class ResourcesImpl { /*package*/ ThemeImpl() { mAssets = ResourcesImpl.this.mAssets; mTheme = mAssets.createTheme(); + sThemeRegistry.registerNativeAllocation(this, mTheme); } @Override diff --git a/core/jni/android_util_AssetManager.cpp b/core/jni/android_util_AssetManager.cpp index adff37d82fa9e..73e7d86e82794 100644 --- a/core/jni/android_util_AssetManager.cpp +++ b/core/jni/android_util_AssetManager.cpp @@ -1244,10 +1244,14 @@ static jlong NativeThemeCreate(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) { return reinterpret_cast(assetmanager->NewTheme().release()); } -static void NativeThemeDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong theme_ptr) { +static void NativeThemeDestroy(jlong theme_ptr) { delete reinterpret_cast(theme_ptr); } +static jlong NativeGetThemeFreeFunction(JNIEnv* /*env*/, jclass /*clazz*/) { + return static_cast(reinterpret_cast(&NativeThemeDestroy)); +} + static void NativeThemeApplyStyle(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong theme_ptr, jint resid, jboolean force) { // AssetManager is accessed via the theme, so grab an explicit lock here. @@ -1474,7 +1478,7 @@ static const JNINativeMethod gAssetManagerMethods[] = { // Theme related methods. {"nativeThemeCreate", "(J)J", (void*)NativeThemeCreate}, - {"nativeThemeDestroy", "(J)V", (void*)NativeThemeDestroy}, + {"nativeGetThemeFreeFunction", "()J", (void*)NativeGetThemeFreeFunction}, {"nativeThemeApplyStyle", "(JJIZ)V", (void*)NativeThemeApplyStyle}, {"nativeThemeRebase", "(JJ[I[ZI)V", (void*)NativeThemeRebase},