com.facebook.common.memory.MemoryTrimType Java Examples

The following examples show how to use com.facebook.common.memory.MemoryTrimType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BitmapMemoryCacheTrimStrategy.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void trimCache(CountingMemoryCache<?, ?, ?> cache, MemoryTrimType trimType) {
  switch (trimType) {
    case OnCloseToDalvikHeapLimit:
  	  //Build.VERSION_CODES.LOLLIPOP
      if (Build.VERSION.SDK_INT >= 21) {
        final double cacheTrimTarget =
            1 - MemoryTrimType.OnCloseToDalvikHeapLimit.getSuggestedTrimRatio();
        final int cacheSizeTarget = (int) (cacheTrimTarget * cache.getSizeInBytes());
        cache.trimCacheTo(Integer.MAX_VALUE, cacheSizeTarget);
      }
      break;
    case OnAppBackgrounded:
    case OnSystemLowMemoryWhileAppInForeground:
    case OnSystemLowMemoryWhileAppInBackground:
      cache.clearEvictionQueue();
      break;
    default:
      FLog.wtf(TAG, "unknown trim type: %s", trimType);
      break;
  }
}
 
Example #2
Source File: NativeMemoryCacheTrimStrategy.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void trimCache(CountingMemoryCache<?, ?, ?> cache, MemoryTrimType trimType) {
  switch (trimType) {
    case OnCloseToDalvikHeapLimit:
      // Cached resources do not consume dalvik heap. We should not clear the
      // cache in case of OnCloseToDalvikHeapLimit.
      break;
    case OnAppBackgrounded:
    case OnSystemLowMemoryWhileAppInForeground:
    case OnSystemLowMemoryWhileAppInBackground:
      cache.clearEvictionQueue();
      break;
    default:
      FLog.wtf(TAG, "unknown trim type: %s", trimType);
      break;
  }

}
 
Example #3
Source File: CountingMemoryCache.java    From fresco with MIT License 6 votes vote down vote up
/** Trims the cache according to the specified trimming strategy and the given trim type. */
@Override
public void trim(MemoryTrimType trimType) {
  ArrayList<Entry<K, V>> oldEntries;
  final double trimRatio = mCacheTrimStrategy.getTrimRatio(trimType);
  synchronized (this) {
    int targetCacheSize = (int) (mCachedEntries.getSizeInBytes() * (1 - trimRatio));
    int targetEvictionQueueSize = Math.max(0, targetCacheSize - getInUseSizeInBytes());
    oldEntries = trimExclusivelyOwnedEntries(Integer.MAX_VALUE, targetEvictionQueueSize);
    makeOrphans(oldEntries);
  }
  maybeClose(oldEntries);
  maybeNotifyExclusiveEntryRemoval(oldEntries);
  maybeUpdateCacheParams();
  maybeEvictEntries();
}
 
Example #4
Source File: BitmapMemoryCacheTrimStrategy.java    From fresco with MIT License 6 votes vote down vote up
@Override
public double getTrimRatio(MemoryTrimType trimType) {
  switch (trimType) {
    case OnCloseToDalvikHeapLimit:
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return MemoryTrimType.OnCloseToDalvikHeapLimit.getSuggestedTrimRatio();
      } else {
        // On pre-lollipop versions we keep bitmaps on the native heap, so no need to trim here
        // as it wouldn't help Dalvik heap anyway.
        return 0;
      }
    case OnAppBackgrounded:
    case OnSystemMemoryCriticallyLowWhileAppInForeground:
    case OnSystemLowMemoryWhileAppInForeground:
    case OnSystemLowMemoryWhileAppInBackground:
      return 1;
    default:
      FLog.wtf(TAG, "unknown trim type: %s", trimType);
      return 0;
  }
}
 
Example #5
Source File: NativeMemoryCacheTrimStrategy.java    From fresco with MIT License 6 votes vote down vote up
@Override
public double getTrimRatio(MemoryTrimType trimType) {
  switch (trimType) {
    case OnCloseToDalvikHeapLimit:
      // Resources cached on native heap do not consume Dalvik heap, so no trimming here.
      return 0;
    case OnAppBackgrounded:
    case OnSystemMemoryCriticallyLowWhileAppInForeground:
    case OnSystemLowMemoryWhileAppInForeground:
    case OnSystemLowMemoryWhileAppInBackground:
      return 1;
    default:
      FLog.wtf(TAG, "unknown trim type: %s", trimType);
      return 0;
  }
}
 
Example #6
Source File: SingleByteArrayPool.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Responds to memory pressure by simply 'discarding' the local byte array
 * @param trimType kind of trimming to perform (ignored)
 */
@Override
public synchronized void trim(MemoryTrimType trimType) {
  if (!mInUse && mByteArraySoftRef.get() != null) {
    if (FLog.isLoggable(FLog.VERBOSE)) {
      FLog.v(TAG, "Discarding existing buffer of size %d", mByteArraySoftRef.get().length);
    }
    mSingleByteArrayPoolStatsTracker.onMemoryTrimmed(mByteArraySoftRef.get().length);
    clearByteArraySoftRef();
  }
}
 
Example #7
Source File: LruBitmapPoolTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testBitmapWasTrimmed() {
  Bitmap expected = Bitmap.createBitmap(128, 128, Bitmap.Config.RGB_565);
  mPool.release(expected);

  assertEquals(1, ((LruBucketsPoolBackend) mPool.mStrategy).valueCount());

  mPool.trim(MemoryTrimType.OnAppBackgrounded);

  Bitmap actual = mPool.get(128 * 128 * 2);

  assertNotSame(actual, expected);
  assertEquals(0, ((LruBucketsPoolBackend) mPool.mStrategy).valueCount());
}
 
Example #8
Source File: FlexByteArrayPoolTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testTrim() {
  mPool.get(7).close();
  assertEquals(1, mDelegatePool.getBucket(8).getFreeListSize());

  // now trim, and verify again
  mDelegatePool.trim(MemoryTrimType.OnCloseToDalvikHeapLimit);
  assertEquals(0, mDelegatePool.getBucket(8).getFreeListSize());
}
 
Example #9
Source File: SharedByteArrayTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testTrimUnsuccessful() {
  CloseableReference<byte[]> arrayRef = mArray.get(7);
  mArray.trim(MemoryTrimType.OnCloseToDalvikHeapLimit);
  assertSame(arrayRef.get(), mArray.mByteArraySoftRef.get());
  assertEquals(0, mArray.mSemaphore.availablePermits());
}
 
Example #10
Source File: SharedByteArrayTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testTrim() {
  mArray.get(7).close();
  assertEquals(8, mArray.mByteArraySoftRef.get().length);

  // now trim, and verify again
  mArray.trim(MemoryTrimType.OnCloseToDalvikHeapLimit);
  assertNull(mArray.mByteArraySoftRef.get());
  assertEquals(1, mArray.mSemaphore.availablePermits());
}
 
Example #11
Source File: SharedByteArray.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Responds to memory pressure by simply 'discarding' the local byte array if it is not used at
 * the moment.
 *
 * @param trimType kind of trimming to perform (ignored)
 */
@Override
public void trim(MemoryTrimType trimType) {
  if (!mSemaphore.tryAcquire()) {
    return;
  }
  try {
    mByteArraySoftRef.clear();
  } finally {
    mSemaphore.release();
  }
}
 
Example #12
Source File: DummyBitmapPool.java    From fresco with MIT License 4 votes vote down vote up
@Override
public void trim(MemoryTrimType trimType) {
  // nop
}
 
Example #13
Source File: DummyTrackingInUseBitmapPool.java    From fresco with MIT License 4 votes vote down vote up
@Override
public void trim(MemoryTrimType trimType) {
  // nop
}
 
Example #14
Source File: CountingMemoryCacheTest.java    From fresco with MIT License 4 votes vote down vote up
@Test
public void testTrimming() {
  MemoryTrimType memoryTrimType = MemoryTrimType.OnCloseToDalvikHeapLimit;
  mParams = new MemoryCacheParams(1100, 10, 1100, 10, 110, PARAMS_CHECK_INTERVAL_MS);
  when(mParamsSupplier.get()).thenReturn(mParams);
  PowerMockito.when(SystemClock.uptimeMillis()).thenReturn(PARAMS_CHECK_INTERVAL_MS);
  InOrder inOrder = inOrder(mReleaser);

  // create original references
  CloseableReference<Integer>[] originalRefs = new CloseableReference[10];
  for (int i = 0; i < 10; i++) {
    originalRefs[i] = newReference(100 + i);
  }
  // cache items & close the original references
  CloseableReference<Integer>[] cachedRefs = new CloseableReference[10];
  for (int i = 0; i < 10; i++) {
    cachedRefs[i] = mCache.cache(KEYS[i], originalRefs[i]);
    originalRefs[i].close();
  }
  // cache should keep alive the items until evicted
  inOrder.verify(mReleaser, never()).release(anyInt());

  // trimming cannot evict shared entries
  when(mCacheTrimStrategy.getTrimRatio(memoryTrimType)).thenReturn(1.00);
  mCache.trim(memoryTrimType);
  assertSharedWithCount(KEYS[0], 100, 1);
  assertSharedWithCount(KEYS[1], 101, 1);
  assertSharedWithCount(KEYS[2], 102, 1);
  assertSharedWithCount(KEYS[3], 103, 1);
  assertSharedWithCount(KEYS[4], 104, 1);
  assertSharedWithCount(KEYS[5], 105, 1);
  assertSharedWithCount(KEYS[6], 106, 1);
  assertSharedWithCount(KEYS[7], 107, 1);
  assertSharedWithCount(KEYS[8], 108, 1);
  assertSharedWithCount(KEYS[9], 109, 1);
  assertTotalSize(10, 1045);
  assertExclusivelyOwnedSize(0, 0);

  // close 7 client references
  cachedRefs[8].close();
  cachedRefs[2].close();
  cachedRefs[7].close();
  cachedRefs[3].close();
  cachedRefs[6].close();
  cachedRefs[4].close();
  cachedRefs[5].close();
  assertSharedWithCount(KEYS[0], 100, 1);
  assertSharedWithCount(KEYS[1], 101, 1);
  assertSharedWithCount(KEYS[9], 109, 1);
  assertExclusivelyOwned(KEYS[8], 108);
  assertExclusivelyOwned(KEYS[2], 102);
  assertExclusivelyOwned(KEYS[7], 107);
  assertExclusivelyOwned(KEYS[3], 103);
  assertExclusivelyOwned(KEYS[6], 106);
  assertExclusivelyOwned(KEYS[4], 104);
  assertExclusivelyOwned(KEYS[5], 105);
  assertTotalSize(10, 1045);
  assertExclusivelyOwnedSize(7, 735);

  // Trim cache by 45%. This means that out of total of 1045 bytes cached, 574 should remain.
  // 310 bytes is used by the clients, which leaves 264 for the exclusively owned items.
  // Only the two most recent exclusively owned items fit, and they occupy 209 bytes.
  when(mCacheTrimStrategy.getTrimRatio(memoryTrimType)).thenReturn(0.45);
  mCache.trim(memoryTrimType);
  assertSharedWithCount(KEYS[0], 100, 1);
  assertSharedWithCount(KEYS[1], 101, 1);
  assertSharedWithCount(KEYS[9], 109, 1);
  assertExclusivelyOwned(KEYS[4], 104);
  assertExclusivelyOwned(KEYS[5], 105);
  assertNotCached(KEYS[8], 108);
  assertNotCached(KEYS[2], 102);
  assertNotCached(KEYS[7], 107);
  assertNotCached(KEYS[3], 103);
  assertNotCached(KEYS[6], 106);
  assertTotalSize(5, 519);
  assertExclusivelyOwnedSize(2, 209);
  inOrder.verify(mReleaser).release(108);
  inOrder.verify(mReleaser).release(102);
  inOrder.verify(mReleaser).release(107);
  inOrder.verify(mReleaser).release(103);
  inOrder.verify(mReleaser).release(106);

  // Full trim. All exclusively owned items should be evicted.
  when(mCacheTrimStrategy.getTrimRatio(memoryTrimType)).thenReturn(1.00);
  mCache.trim(memoryTrimType);
  assertSharedWithCount(KEYS[0], 100, 1);
  assertSharedWithCount(KEYS[1], 101, 1);
  assertSharedWithCount(KEYS[9], 109, 1);
  assertNotCached(KEYS[8], 108);
  assertNotCached(KEYS[2], 102);
  assertNotCached(KEYS[7], 107);
  assertNotCached(KEYS[3], 103);
  assertNotCached(KEYS[6], 106);
  assertNotCached(KEYS[6], 104);
  assertNotCached(KEYS[6], 105);
  assertTotalSize(3, 310);
  assertExclusivelyOwnedSize(0, 0);
  inOrder.verify(mReleaser).release(104);
  inOrder.verify(mReleaser).release(105);
}
 
Example #15
Source File: LruBitmapPool.java    From fresco with MIT License 4 votes vote down vote up
@Override
public void trim(MemoryTrimType trimType) {
  trimTo((int) (mMaxPoolSize * (1f - trimType.getSuggestedTrimRatio())));
}
 
Example #16
Source File: CountingMemoryCache.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void trim(MemoryTrimType trimType) {
  FLog.v(TAG, "Trimming cache, trim type %s", String.valueOf(trimType));
  mCacheTrimStrategy.trimCache(this, trimType);
}
 
Example #17
Source File: FlexByteArrayPoolTest.java    From fresco with MIT License 4 votes vote down vote up
@Test
public void testTrimUnsuccessful() {
  CloseableReference<byte[]> arrayRef = mPool.get(7);
  mDelegatePool.trim(MemoryTrimType.OnCloseToDalvikHeapLimit);
  assertNotNull(arrayRef.get());
}
 
Example #18
Source File: MemoryTrimmer.java    From react-native-image-filter-kit with MIT License 4 votes vote down vote up
synchronized void trim() {
  for (MemoryTrimmable trimmable : mTrimmables) {
    trimmable.trim(MemoryTrimType.OnCloseToDalvikHeapLimit);
  }
}
 
Example #19
Source File: BasePool.java    From fresco with MIT License 2 votes vote down vote up
/**
 * Trims the pool in response to low-memory states (invoked from MemoryManager) For now, we'll do
 * the simplest thing, and simply clear out the entire pool. We may consider more sophisticated
 * approaches later. In other words, we ignore the memoryTrimType parameter
 *
 * @param memoryTrimType the kind of trimming we want to perform
 */
public void trim(MemoryTrimType memoryTrimType) {
  trimToNothing();
}
 
Example #20
Source File: BasePool.java    From FanXin-based-HuanXin with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Trims the pool in response to low-memory states (invoked from MemoryManager)
 * For now, we'll do the simplest thing, and simply clear out the entire pool. We may consider
 * more sophisticated approaches later.
 * In other words, we ignore the memoryTrimType parameter
 * @param memoryTrimType the kind of trimming we want to perform
 */
public void trim(MemoryTrimType memoryTrimType) {
  trimToNothing();
}
 
Example #21
Source File: MemoryCache.java    From fresco with MIT License votes vote down vote up
double getTrimRatio(MemoryTrimType trimType); 
Example #22
Source File: CountingMemoryCache.java    From FanXin-based-HuanXin with GNU General Public License v2.0 votes vote down vote up
void trimCache(CountingMemoryCache<?, ?, ?> cache, MemoryTrimType trimType);