Skip to content

Commit

Permalink
Cut the default BitmapPool size down to 1 for O+ and 0 for O+ with is…
Browse files Browse the repository at this point in the history
…LowRam.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=165717599
  • Loading branch information
sjudd committed Aug 18, 2017
1 parent d83de42 commit bb5c391
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
7 changes: 6 additions & 1 deletion library/src/main/java/com/bumptech/glide/GlideBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.bumptech.glide.load.engine.Engine;
import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPoolAdapter;
import com.bumptech.glide.load.engine.bitmap_recycle.LruArrayPool;
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.load.engine.cache.DiskCache;
Expand Down Expand Up @@ -308,7 +309,11 @@ public Glide build(Context context) {

if (bitmapPool == null) {
int size = memorySizeCalculator.getBitmapPoolSize();
bitmapPool = new LruBitmapPool(size);
if (size > 0) {
bitmapPool = new LruBitmapPool(size);
} else {
bitmapPool = new BitmapPoolAdapter();
}
}

if (arrayPool == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface ScreenDimensions {

private MemorySizeCalculator(MemorySizeCalculator.Builder builder) {
this.context = builder.context;

arrayPoolSize =
isLowMemoryDevice(builder.activityManager)
? builder.arrayPoolSizeBytes / LOW_MEMORY_BYTE_ARRAY_POOL_DIVISOR
Expand All @@ -43,13 +44,14 @@ private MemorySizeCalculator(MemorySizeCalculator.Builder builder) {
builder.screenDimensions.getHeightPixels() *
BYTES_PER_ARGB_8888_PIXEL;

int targetPoolSize = Math.round(screenSize * builder.bitmapPoolScreens);
int targetBitmapPoolSize = Math.round(screenSize * builder.bitmapPoolScreens);

int targetMemoryCacheSize = Math.round(screenSize * builder.memoryCacheScreens);
int availableSize = maxSize - arrayPoolSize;

if (targetMemoryCacheSize + targetPoolSize <= availableSize) {
if (targetMemoryCacheSize + targetBitmapPoolSize <= availableSize) {
memoryCacheSize = targetMemoryCacheSize;
bitmapPoolSize = targetPoolSize;
bitmapPoolSize = targetBitmapPoolSize;
} else {
float part = availableSize / (builder.bitmapPoolScreens + builder.memoryCacheScreens);
memoryCacheSize = Math.round(part * builder.memoryCacheScreens);
Expand All @@ -67,7 +69,7 @@ private MemorySizeCalculator(MemorySizeCalculator.Builder builder) {
+ ", byte array size: "
+ toMb(arrayPoolSize)
+ ", memory class limited? "
+ (targetMemoryCacheSize + targetPoolSize > maxSize)
+ (targetMemoryCacheSize + targetBitmapPoolSize > maxSize)
+ ", max size: "
+ toMb(maxSize)
+ ", memoryClass: "
Expand Down Expand Up @@ -127,7 +129,15 @@ private static boolean isLowMemoryDevice(ActivityManager activityManager) {
public static final class Builder {
// Visible for testing.
static final int MEMORY_CACHE_TARGET_SCREENS = 2;
static final int BITMAP_POOL_TARGET_SCREENS = 4;

/**
* On Android O+, we use {@link android.graphics.Bitmap.Config#HARDWARE} for all reasonably
* sized images unless we're creating thumbnails for the first time. As a result, the Bitmap
* pool is much less important on O than it was on previous versions.
*/
static final int BITMAP_POOL_TARGET_SCREENS =
Build.VERSION.SDK_INT > Build.VERSION_CODES.O ? 4 : 1;

This comment has been minimized.

Copy link
@SUPERCILEX

SUPERCILEX Sep 2, 2017

Contributor

Shouldn't this be >=?

This comment has been minimized.

Copy link
@sjudd

sjudd Sep 5, 2017

Author Collaborator

Yes, thanks for pointing this out, filed: #2332


static final float MAX_SIZE_MULTIPLIER = 0.4f;
static final float LOW_MEMORY_MAX_SIZE_MULTIPLIER = 0.33f;
// 4MB.
Expand All @@ -151,6 +161,14 @@ public Builder(Context context) {
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
screenDimensions =
new DisplayMetricsScreenDimensions(context.getResources().getDisplayMetrics());

// On Android O+ Bitmaps are allocated natively, ART is much more efficient at managing
// garbage and we rely heavily on HARDWARE Bitmaps, making Bitmap re-use much less important.
// We prefer to preserve RAM on these devices and take the small performance hit of not
// re-using Bitmaps and textures when loading very small images or generating thumbnails.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && isLowMemoryDevice(activityManager)) {
bitmapPoolScreens = 0;
}
}

/**
Expand Down

0 comments on commit bb5c391

Please sign in to comment.