One character in Base64String contains how many bytes? 1, 2 or more

In my Android application, I need to get images from the server and cache them in heap memory.

Upon receipt of the request, the server first encodes byte[] in Base64String and returns this string. And while rendering in ImageView , the Android application decodes Base64String back to byte[] , creates a Bitmap and puts it in ImageView .

Since everything is in the cache, it is likely that the application will at some point go out of memory and is critically critical.

To prevent a memory situation, in my application I defined security (for example, 5 MB). If at any time the available memory falls below this protective quantum , the user will need to mark some images as candidates for deletion. In addition, the application will display the estimated memory that will be released after the removal of the selected items.

Bitmap been redesigned as soon as the user moves away from the image, so Bitmap does not effectively hold memory until we leave.

In a specific test, I upload 55 images, and my heap grows from 16 MB to 42 MB . This means that 55 images occupy 26 MB . After I cleaned them, the heap is compressed to 16 MB .

But, when I take the cumulative sum of the lengths of all Base64String , it comes to 11983840 . And if I consider one character as 1 byte , 11983840 bytes makes 11.4 MB

The problem is that the total amount of Base64String lengths is the only measure available to me that allows the user to know how much memory can be released at his option.

I also read the following question, which mentions that for every 3 Bytes source data, Base64String will have 4 Characters .

Calculation of base length64?

The question is, Base64String one character in Base64String contain the number of bytes? 1, 2 or more

If 1 character is 1 byte , and in my test the heap grows and shrinks 26 MB . Then why is the total length of Base64String only 11.4 MB ?

Update

enter image description here

This means 1 byte per character.

enter image description here

The default character set is UTF-8

+6
source share
1 answer

Let's start from the beginning ...

Base64 is an encoding format based on a set of 64 characters - each character costs 6 data bits (2 ^ 6 = 64). Therefore, when converting, each 8-bit input byte requires (8/6) = 1.333333 ... characters.

But since computers store only whole bytes, it’s easier to say that for every 3 bytes (3 * 8 = 24 bits) of the input, 4 basic 64 characters are created (4 * 6 = 24 bits).

Now you say that the total length of the base-64 string is 11 million characters. In Java, strings are stored in 16-bit Unicode format: 1 character = 2 bytes.

Thus, 11 million characters will use more than 20 MB only in raw character data. This, combined with the objects, variables and other state necessary to store information, means that 26 MB is a reasonable value for the amount of data that you use.

So, the general rule that you might want to use to evaluate memory usage is (((input_data_in_bytes * 4/3) * 2) + several MB)

+2
source

All Articles