Skip to content

Instantly share code, notes, and snippets.

@sensen
Created September 4, 2015 03:33
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save sensen/a61eb1e7a1b7ea649cb0 to your computer and use it in GitHub Desktop.
Save sensen/a61eb1e7a1b7ea649cb0 to your computer and use it in GitHub Desktop.
Fresco's custom BitmapMemoryCacheParamsSupplier for Lollipop devices
public class LollipopBitmapMemoryCacheParamsSupplier implements Supplier<MemoryCacheParams> {
private ActivityManager activityManager;
public LollipopBitmapMemoryCacheParamsSupplier(ActivityManager activityManager) {
this.activityManager = activityManager;
}
@Override
public MemoryCacheParams get() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return new MemoryCacheParams(getMaxCacheSize(), 1, 1, 1, 1);
} else {
return new MemoryCacheParams(
getMaxCacheSize(),
256,
Integer.MAX_VALUE,
Integer.MAX_VALUE,
Integer.MAX_VALUE);
}
}
private int getMaxCacheSize() {
final int maxMemory =
Math.min(activityManager.getMemoryClass() * ByteConstants.MB, Integer.MAX_VALUE);
if (maxMemory < 32 * ByteConstants.MB) {
return 4 * ByteConstants.MB;
} else if (maxMemory < 64 * ByteConstants.MB) {
return 6 * ByteConstants.MB;
} else {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD) {
return 8 * ByteConstants.MB;
} else {
return maxMemory / 4;
}
}
}
}
@sensen
Copy link
Author

sensen commented Sep 4, 2015

This is just a workaround for the OOM issue when using Fresco on Lollipop devices.

Usage:

private void initFresco() {
    ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    ImagePipelineConfig imagePipelineConfig = ImagePipelineConfig
            .newBuilder(getApplicationContext())
            .setBitmapMemoryCacheParamsSupplier(new LollipopBitmapMemoryCacheParamsSupplier(activityManager))
            .build();

    Fresco.initialize(getApplicationContext(), imagePipelineConfig);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment