From 28b2c8a86cf9d1ff60d7bc4404eda6791a23db83 Mon Sep 17 00:00:00 2001 From: Kweku Adams Date: Thu, 22 Jun 2023 21:13:04 +0000 Subject: [PATCH] Reduce memory usage. 1. Make sure strings are interned so that memory isn't wasted on duplicate strings. 2. Most of the memory used by the TARE system was made up of the Transaction objects. The Transaction history is only kept for debugging purposes, so it can be reduced without affecting the policy. Reduce the number of transactions kept in memory by 92% for user builds to reduce the memory used by the TARE system by 90-92%. Bug: 288479057 Test: atest frameworks/base/services/tests/mockingservicestests/src/com/android/server/tare Test: atest frameworks/base/services/tests/servicestests/src/com/android/server/tare Change-Id: Ic12a54a3d12be60a7fe20bb62931e8f2e62f1475 --- .../service/java/com/android/server/tare/Ledger.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/apex/jobscheduler/service/java/com/android/server/tare/Ledger.java b/apex/jobscheduler/service/java/com/android/server/tare/Ledger.java index a68170c9bac7b..92b21e10d142e 100644 --- a/apex/jobscheduler/service/java/com/android/server/tare/Ledger.java +++ b/apex/jobscheduler/service/java/com/android/server/tare/Ledger.java @@ -25,7 +25,9 @@ import static com.android.server.tare.TareUtils.getCurrentTimeMillis; import android.annotation.CurrentTimeMillisLong; import android.annotation.NonNull; import android.annotation.Nullable; +import android.os.Build; import android.util.IndentingPrintWriter; +import android.util.Log; import android.util.SparseLongArray; import android.util.TimeUtils; @@ -38,6 +40,10 @@ import java.util.List; * Ledger to track the last recorded balance and recent activities of an app. */ class Ledger { + private static final String TAG = "TARE-" + Ledger.class.getSimpleName(); + private static final boolean DEBUG = InternalResourceService.DEBUG + || Log.isLoggable(TAG, Log.DEBUG); + /** The window size within which rewards will be counted and used towards reward limiting. */ private static final long TOTAL_REWARD_WINDOW_MS = 24 * HOUR_IN_MILLIS; /** The number of buckets to split {@link #TOTAL_REWARD_WINDOW_MS} into. */ @@ -51,7 +57,7 @@ class Ledger { TOTAL_REWARD_WINDOW_MS / NUM_REWARD_BUCKET_WINDOWS; /** The maximum number of transactions to retain in memory at any one time. */ @VisibleForTesting - static final int MAX_TRANSACTION_COUNT = 50; + static final int MAX_TRANSACTION_COUNT = Build.IS_ENG || Build.IS_USERDEBUG || DEBUG ? 32 : 4; static class Transaction { public final long startTimeMs; @@ -67,7 +73,7 @@ class Ledger { this.startTimeMs = startTimeMs; this.endTimeMs = endTimeMs; this.eventId = eventId; - this.tag = tag; + this.tag = tag == null ? null : tag.intern(); this.delta = delta; this.ctp = ctp; }