find memory cache size in bytes in android application

Rajat Kumar
1 min readApr 16, 2020

--

public static final int MAX_MEMORY_CACHE_SIZE = 30 * 1024 *1024;   

public static int memoryCacheSizeBytes(final Context context) {
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
long memoryClass = activityManager.getMemoryClass();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
try {
final int flagLargeHeap = ApplicationInfo.class.getDeclaredField("FLAG_LARGE_HEAP").getInt(null);
if (StringUtils.bitMaskContainsFlag(context.getApplicationInfo().flags, flagLargeHeap)) {
memoryClass = (Integer) new Reflection.MethodBuilder(activityManager, "getLargeMemoryClass").execute();
}
} catch (Exception e) {
Log.internal("DeviceUtils", "Unable to reflectively determine large heap size on Honeycomb and above.");
}
}



long result = Math.min(MAX_MEMORY_CACHE_SIZE, memoryClass / 8 * 1024 * 1024);
return (int) result;
}

--

--

No responses yet