Trending Articles

Tech

How Does A Bitmap Object?

Bitmap –  Different Directories Cause Different Memory Usage of bitmap. Suppose an image is 400 pixels long and 300 pixels wide in jpg format. We put it in the asset directory.

For the occupied memory of the image, we can use the get Byte Count method of Bitmap. Before running the program, we first calculate the memory usage of the graph according to the formula. Since Android uses ARGB_8888 format decoding by default, i.e., one pixel occupies 4 bytes:

Image Occupies Memory = 400 * 300 * 4 = 480000 A Bitmap

The test model is Mi 6, and the phone’s screen DPI is 480. The rationale is that when the image is loaded by default, the screen density of the file directory where the image is located is compared with the screen density mobile phone current.

Therefore, In Our Example, The Memory Occupied When The Image Is Placed In The Hope Folder Is:

scale = 480 / 240 = 2;

Memory = int (400 * 2 + 0.5) * int (300 * 2 + 0.5) * 4 = 1920000

The memory occupied when the image is placed in the xhdpi folder:

scale = 480 / 320 = 1.5;

Memory = int(400*1.5+0.5)*int(300*1.5+0.5)*4=1080000

2 Various ways to solve OOM load A Bitmap

Loading large images to avoid OOM is an old topic. There are many materials online. Here I only introduce some methods. OOM is called a buffer overflow. Before avoiding buffer overflow, we first discovered a problem: Why is there a buffer overflow?

Big Method A Bitmap

When our program requests memory space, to ensure that it can request memory space successfully, we must ensure that the total size of currently allocated memory plus the current memory value to be distributed cannot exceed the current maximum memory value of the program. This should start from android memory management. The Android system mobile phone specifies the upper limit of the heap memory at the bottom of the system. Heap (Mobile phone manufacturers will adjust it according to the mobile phone settings).

If the size of the explained image plus the currently allocated heap memory exceeds the maximum heap memory, the system will first perform a GC operation. An OOM that we often say happened. If the whole heap is still exceeding after GC, an exception will throw at this point.

  • Use The Appropriate Image Decoding Format A Bitmap

In many cases, images downloaded from the Internet use the jpg format. Using the RGB_565 form for decoding for jpg images will save more memory than ARGB_8888 decoding.

  • Problem Location A Bitmap

A bug was discover during the development process. The performance is: long images could not be load on models like Huawei mate10 pro, there will be a black screen flickering, and then the idea will not come out. The image specifications are about 480*10000+. At first, it was suspect that it was too large after Glide Load A Bitmap download the image, and memory could not be load. But after locating

  • Solution A Bitmap

  1. Disable hardware acceleration. Android defaults to hardware acceleration after api14. But without using hardware acceleration, it does not limit the size of the image loaded simultaneously. But the performance hit is just too big
  2. When the image is obtain. first compare it with the systems OpenGL drawable size. Once it is larger than the system size, the idea is scale.

How To Load A Minified Type A Bitmap?

Now that the image sizes are know, they can be use to decide whether the entire image should be upload into memory or a subsample version.

Now Are Some Factors To Consider in A Bitmap:

  • Estimated memory usage when uploading an image as the whole to memory.
  • Amount of memory you want to commit to loading this image, given any other memory requirements of your application.

Imageries can be of various shapes and sizes. They are larger than necessary for a typical application user interface (UI) in many cases. For example, the system Gallery app displays photos taken with the Android device’s camera that typically have a much higher resolution than the device’s screen density.

Ideally, since you are working with limited memory, you should only upload a lower-resolution version to memory. The lower resolution version must match the size of the UI component displaying it. A higher resolution image provides no visible benefit but still occupies valuable memory and incurs additional performance overhead due to different scaling.

Also Read: What Is A Server? – Work, Physical, and More

Related posts