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
This commit is contained in:
Ryan Mitchell
2021-06-08 10:58:59 -07:00
parent 767e34fb17
commit 7b8091ad7c
3 changed files with 18 additions and 4 deletions

View File

@@ -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,

View File

@@ -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

View File

@@ -1244,10 +1244,14 @@ static jlong NativeThemeCreate(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) {
return reinterpret_cast<jlong>(assetmanager->NewTheme().release());
}
static void NativeThemeDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong theme_ptr) {
static void NativeThemeDestroy(jlong theme_ptr) {
delete reinterpret_cast<Theme*>(theme_ptr);
}
static jlong NativeGetThemeFreeFunction(JNIEnv* /*env*/, jclass /*clazz*/) {
return static_cast<jlong>(reinterpret_cast<uintptr_t>(&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},