@VisibleForTesting
  synchronized void trimToSize(int targetSize) {
    // find how much we need to free
    int bytesToFree = Math.min(mUsed.mNumBytes + mFree.mNumBytes - targetSize, mFree.mNumBytes);
    if (bytesToFree <= 0) {
      return;
    }
    if (FLog.isLoggable(FLog.VERBOSE)) {
      FLog.v(
          TAG,
          "trimToSize: TargetSize = %d; Initial Size = %d; Bytes to free = %d",
          targetSize,
          mUsed.mNumBytes + mFree.mNumBytes,
          bytesToFree);
    }
    logStats();

    // now walk through the buckets from the smallest to the largest. Keep freeing things
    // until we've gotten to what we want
    for (int i = 0; i < mBuckets.size(); ++i) {
      if (bytesToFree <= 0) {
        break;
      }
      Bucket<V> bucket = Preconditions.checkNotNull(mBuckets.valueAt(i));
      while (bytesToFree > 0) {
        V value = bucket.pop();
        if (value == null) {
          break;
        }
        free(value);
        bytesToFree -= bucket.mItemSize;
        mFree.decrement(bucket.mItemSize);
      }
    }

    // dump stats at the end
    logStats();
    if (FLog.isLoggable(FLog.VERBOSE)) {
      FLog.v(
          TAG,
          "trimToSize: TargetSize = %d; Final Size = %d",
          targetSize,
          mUsed.mNumBytes + mFree.mNumBytes);
    }
  }
(Try to) trim the pool until its total space falls below the max size (soft cap). This will get
rid of values on the free list, until the free lists are empty, or we fall below the max size;
whichever comes first. NOTE: It is NOT an error if we have eliminated all the free values, but
the pool is still above its max size (soft cap)

<p>The approach we take is to go from the smallest sized bucket down to the largest sized
bucket. This may seem a bit counter-intuitive, but the rationale is that allocating
larger-sized values is more expensive than the smaller-sized ones, so we want to keep them
around for a while.

@param targetSize target size to trim to
imagepipeline/src/main/java/com/facebook/imagepipeline/memory/BasePool.java