It's the end of 2018 so things have changed.
First of all: run your app and open Android Profiler tab in Android Studio.
You will see how much memory it consumes, you will be surprised but it can allocate a lot of RAM.
Also here is a great article in official docs with detailed instructions on how to use Memory Profiler which can give you an in-depth look of your memory management.
But in most of the cases, your regular Android Profiler will be enough for you.
Usually, an app starts with 50Mb of RAM allocation but instantly jumps up to 90Mb when you start loading some photos in memory. When you open Activity with a ViewPager with preloaded photos (3,5Mb each) you can get 190Mb easily in seconds.
But this doesn't mean you have issues with memory management.
The best advice I can give is to follow the guidelines and best practices, use top libraries for image loading (Glide, Picasso) and you'll be OK.
But if you need to tailor something and you really need to know how much memory you can allocate manually you can get total free memory and calculate a pre-determined portion (in %) out of it.
In my case, I needed to cache decrypted photos in memory so I don't need to decrypt them everytime user slides through the list.
For this purpose you can use ready to use LruCache class. It's a cache class which automatically tracks of how much memory your objects allocate (or the number of instances) and removes the oldest to keep the recent by their usage history.
Here is a great tutorial on how to use it.
In my case, I created 2 instances of caches: for thumbs and attachments.
Made them static with singleton access so they are available globally throughout the app.
cache class:
public class BitmapLruCache extends LruCache<Uri, byte[]> {
private static final float CACHE_PART_FOR_THUMBS_PRC = 0.01f; // 1% (Nexus 5X - 5Mb)
private static final float CACHE_PART_FOR_ATTACHMENTS_PRC = 0.03f;// 3% (Nexus 5X - 16Mb)
private static BitmapLruCache thumbCacheInstance;
private static BitmapLruCache attachmentCacheInstance;
public static synchronized BitmapLruCache getDecryptedThumbCacheInstance() {
if (thumbCacheInstance == null) {
int cacheSize = getCacheSize(CACHE_PART_FOR_THUMBS_PRC);
//L.log("creating BitmapLruCache for Thumb with size: " + cacheSize + " bytes");
thumbCacheInstance = new BitmapLruCache(cacheSize);
return thumbCacheInstance;
} else {
return thumbCacheInstance;
}
}
public static synchronized BitmapLruCache getDecryptedAttachmentCacheInstance() {
if (attachmentCacheInstance == null) {
int cacheSize = getCacheSize(CACHE_PART_FOR_ATTACHMENTS_PRC);
// L.log("creating BitmapLruCache for Attachment with size: " + cacheSize + " bytes");
attachmentCacheInstance = new BitmapLruCache(cacheSize);
return attachmentCacheInstance;
} else {
return attachmentCacheInstance;
}
}
private BitmapLruCache(int maxSize) {
super(maxSize);
}
public void addBitmap(Uri uri, byte[] bitmapBytes) {
if (get(uri) == null && bitmapBytes != null)
put(uri, bitmapBytes);
}
public byte[] getBitmap(Uri uri) {
return get(uri);
}
@Override
protected int sizeOf(Uri uri, byte[] bitmapBytes) {
// The cache size will be measured in bytes rather than number of items.
return bitmapBytes.length;
}
}
This is how I calculate available free RAM and how much I can bite out of it:
private static int getCacheSize(float partOfTotalFreeMemoryToUseAsCache){
final long maxMemory = Runtime.getRuntime().maxMemory();
//Use ... of available memory for List Notes thumb cache
return (int) (maxMemory * partOfTotalFreeMemoryToUseAsCache);
}
And this is how I use it in Adapters to get cached image:
byte[] decryptedThumbnail = BitmapLruCache.getDecryptedThumbCacheInstance().getBitmap(thumbUri);
and how I set it into cache in background thread (regular AsyncTask):
BitmapLruCache.getDecryptedThumbCacheInstance().addBitmap(thumbUri, thumbBytes);
My app targets API 19+ so devices are not old and these portions of available RAM are good enough for cache in my case (1% and 3%).
Fun fact: Android does not have any APIs or other hacks to get the amount of memory allocated to your app, it's calculated on the fly based on various factors.
P.S. I'm using a static class field to hold a cache but as per the latest Android guidelines its recommended to use ViewModel architecture component for that purpose.