Skip to content

Commit

Permalink
Merge pull request #15 from livefront/fix-aggressive-data-clearing
Browse files Browse the repository at this point in the history
Do not automatically remove "stale" data when targets go out of memory
  • Loading branch information
brian-livefront committed Jun 4, 2018
2 parents 94d6a14 + 3bcb5ce commit 5b63064
Showing 1 changed file with 0 additions and 37 deletions.
37 changes: 0 additions & 37 deletions bridge/src/main/java/com/livefront/bridge/BridgeDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
import com.livefront.bridge.wrapper.WrapperUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.WeakHashMap;

Expand All @@ -28,18 +26,11 @@ class BridgeDelegate {
private static final String KEY_BUNDLE = "bundle_%s";
private static final String KEY_UUID = "uuid_%s";

/**
* Time (in milliseconds) to wait between attempts to automatically clear stale data.
*/
private static final int AUTO_DATA_CLEAR_INTERVAL_MS = 100;

private boolean mIsClearAllowed = false;
private boolean mIsFirstRestoreCall = true;
private long mLastClearTime;
private Map<String, Bundle> mUuidBundleMap = new HashMap<>();
private Map<Object, String> mObjectUuidMap = new WeakHashMap<>();
private SavedStateHandler mSavedStateHandler;
private Set<String> mRecentUuids = new HashSet<>();
private SharedPreferences mSharedPreferences;

BridgeDelegate(@NonNull Context context,
Expand All @@ -61,7 +52,6 @@ void clear(@NonNull Object target) {
}

void clearAll() {
mRecentUuids.clear();
mUuidBundleMap.clear();
mObjectUuidMap.clear();
mSharedPreferences.edit()
Expand All @@ -70,7 +60,6 @@ void clearAll() {
}

private void clearDataForUuid(@NonNull String uuid) {
mRecentUuids.remove(uuid);
mUuidBundleMap.remove(uuid);
clearDataFromDisk(uuid);
}
Expand All @@ -81,30 +70,6 @@ private void clearDataFromDisk(@NonNull String uuid) {
.apply();
}

/**
* Attempts to clear data associated with references held weakly that have been cleared. Note
* that there is no guarantee of success, as some references may still be held by the system
* longer than is actually necessary.
*/
private void clearStaleData() {
long currentTime = System.currentTimeMillis();
if (currentTime - mLastClearTime < AUTO_DATA_CLEAR_INTERVAL_MS) {
// Avoid too many checks during the same stop event
return;
}
mLastClearTime = currentTime;
System.gc();

// Remove all the remaining UUIDs in the object map from the recent UUID list. Anything
// left represents objects that were garbage collected, so we should clear up any saved
// state associated with them.
Set<String> staleUuids = new HashSet<>(mRecentUuids);
staleUuids.removeAll(mObjectUuidMap.values());
for (String uuid : staleUuids) {
clearDataForUuid(uuid);
}
}

private String getKeyForEncodedBundle(@NonNull String uuid) {
return String.format(KEY_BUNDLE, uuid);
}
Expand Down Expand Up @@ -195,10 +160,8 @@ void saveInstanceState(@NonNull Object target, @NonNull Bundle state) {
return;
}
WrapperUtils.wrapOptimizedObjects(bundle);
mRecentUuids.add(uuid);
mUuidBundleMap.put(uuid, bundle);
writeToDisk(uuid, bundle);
clearStaleData();
}

private void writeToDisk(@NonNull String uuid,
Expand Down

0 comments on commit 5b63064

Please sign in to comment.