Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fbcde60f authored by sangyun's avatar sangyun Committed by Sangyun Yun
Browse files

Reduce resource cache size and use LruCache

 android.util.LruCache provides least recent used algorithm and has
an advantage of not calculating the size every time to find eldest
value of LinkedMap. So changed from LinkedMap to LruCache.
 Size of key configuration, package name, and value Resource are
about 300 bytes per one item on average, so even if the cache has
1000 items, it does not use that much heap memory. And the
Configuration is logically hard to exceed 1000 types, so it can be
considered large enough.

Bug: 289045241
Test: atest CtsTelephonyTestCases
Test: atest CellBroadcastServiceTests
Test: atest GoogleCellBroadcastReceiverUnitTests
Test: manually perform telephony tests and validty checks
Change-Id: I606013e0a5a69355cdaa9453842ca63fd08d1126
parent c993acbf
Loading
Loading
Loading
Loading
+14 −15
Original line number Diff line number Diff line
@@ -64,6 +64,7 @@ import android.telephony.ims.ImsMmTelManager;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.util.LruCache;
import android.util.Pair;

import com.android.internal.telephony.ISetOpportunisticDataCallback;
@@ -85,7 +86,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -1332,20 +1332,15 @@ public class SubscriptionManager {
     * In order to prevent the overflow of the heap size due to an indiscriminate increase in the
     * cache, the heap size of the resource cache is set sufficiently large.
     */
    private static final int MAX_RESOURCE_CACHE_ENTRY_COUNT = 10_000;
    private static final int MAX_RESOURCE_CACHE_ENTRY_COUNT = 1_000;

    /**
     * Cache of Resources that has been created in getResourcesForSubId. Key contains package name,
     * and Configuration of Resources. If more than the maximum number of resources are stored in
     * this cache, the least recently used Resources will be removed to maintain the maximum size.
     */
    private static final Map<Pair<String, Configuration>, Resources> sResourcesCache =
            Collections.synchronizedMap(new LinkedHashMap<>(16, 0.75f, true) {
                @Override
                protected boolean removeEldestEntry(Entry eldest) {
                    return size() > MAX_RESOURCE_CACHE_ENTRY_COUNT;
                }
            });
    private static final LruCache<Pair<String, Configuration>, Resources> sResourcesCache =
            new LruCache<>(MAX_RESOURCE_CACHE_ENTRY_COUNT);

    /**
     * A listener class for monitoring changes to {@link SubscriptionInfo} records.
@@ -2841,12 +2836,14 @@ public class SubscriptionManager {
                configurationKey.setLocale(Locale.ROOT);
            }
            cacheKey = Pair.create(context.getPackageName(), configurationKey);
            synchronized (sResourcesCache) {
                Resources cached = sResourcesCache.get(cacheKey);
                if (cached != null) {
                    // Cache hit. Use cached Resources.
                    return cached;
                }
            }
        }

        final SubscriptionInfo subInfo =
                SubscriptionManager.from(context).getActiveSubscriptionInfo(subId);
@@ -2876,9 +2873,11 @@ public class SubscriptionManager {
        Resources res = newContext.getResources();

        if (cacheKey != null) {
            synchronized (sResourcesCache) {
                // Save the newly created Resources in the resource cache.
                sResourcesCache.put(cacheKey, res);
            }
        }
        return res;
    }