A few days ago, we released an app in Play store, which deals with high quality bitmaps and is all about editing them.
Everything was going well when we realized that 20% of devices were giving out of memory errors. So we checked our code and found out that Android was not releasing native memory used to store bitmap data on some devices. In this case, we welcomed the recycle command.
The memory errors disappeared (at least in hd devices). Anyway, we were happy. But today we started to see that 50% of devices started to give another error: "Can't copy a recycled bitmap"
We were bummed. On two bitmap.copy() lines in our code, half of the devices cannot do this two lines in synchronous:
Bitmap anotherBitmap = bitmap.copy( bitmap.getConfig(), true );
bitmap.recycle();
So we removed the recycle and releasing another update, decided to limit the device screen sizes, so small ones do not give us a bad rating.
Here is my question. Why some devices can copy before recycling, and half cannot?
I read Google's bitmap related documentation and already knew how bitmap is stored on both vm's heap and native heap, how garbage collection works on out of memory errors, etc. The example code Google provides to load and edit large bitmaps is nearly same as ours.
Read a lot of blogs, google group threads, github code samples... I think I still need a good documentation/book about Android bitmaps.
PS: We are already using inSampleSize to scale bitmaps while decoding them.
EDIT -- Here are some data from the crash reports:
All devices are non-rooted. In most cases used memory is between 25% and 35%.
Manufacturers:
57% LG
31% Samsung
10% Casper Via V5 (Turkey based company, sells rebranded Chinese phones)
Devices:
81% LG D855 (G3)
18% LG D802TR (G2)
----
66% Samsung SM N910C (Galaxy Note 4)
20% Samsung SM A700F (Galaxy A7)
Operating Systems:
68% Android 5
31% Android 4
OS 5 Details
69% Android 5.0
30% Android 5.0.1
OS4 Details
66% Android 4.4.2
33% Android 4.4.4