In my Android application the requirement is to fetch the images from the server and cache them into the heap memory.
On receiving the request, the server first encodes the byte[]
into Base64String
and returns that string. And, at the time of rendering it into ImageView
the Android application decodes the Base64String
back to the byte[]
, creates a Bitmap
, and puts it on ImageView
.
As everything is in cache, there are chances the Application to go out of memory at some point, and crash critically.
To prevent the out of memory situation, I have defined a safety quantum (E.g. 5 MB) in my application. If at any point the available memory goes below to this safety quantum, user would then need to mark some of the images as the candidates to be deleted. Along with, the application would show the estimated memory going to be released once the selected items are cleared.
The Bitmap
has been recycled once user moves away from the image, so the Bitmap
effectively isn't holding any memory as long as we are away.
In a particular test, I download 55 images, and my heap grows from 16 MB
to 42 MB
. That means, 55 images occupy 26 MB
. After I clear all of them, the heap shrinks back to 16 MB
.
But, when I take a cumulative sum of lengths of all Base64String
it comes to 11983840
. And if I consider one character as 1 byte
the 11983840 bytes
makes 11.4 MB
The problem is, cumulative sum of the lengths of Base64String
is the only measure available to me, that helps to let user know how much memory can be released by his selection.
I have also read the following question, which mentions, for each 3 Bytes
of original data the Base64String
will have 4 Characters
.
The question is,
a single character in Base64String
holds how many bytes? 1, 2 or more
If 1 character
is 1 byte
,
and in my test the heap grows and shrinks by 26 MB. Then why the cumulative sum of the lengths of Base64String
is only 11.4 MB?
Updated
This means 1 Byte
per character.
The default CharacterSet here is UTF-8