Memory overflow caused by bitmap outofmemory Solution

Source: Internet
Author: User
Memory overflow caused by bitmapOutofmemory Solution Author: lashuai

I. Problem description:When the android camera is used on its own, there is no problem with taking a picture. It works normally when we call our code, but the tablet of different manufacturers is changed. After the Rom is changed from android4.0 to android4.1, an outofmemory exception occurred while taking the picture, and the program was interrupted and exited. How can this problem be solved?

2. Let's take a look at the code we wrote before.

1) Call the System camera (no error occurred here, Code omitted)

2) display images

Mimageview = (imageview) findviewbyid (R. Id. imageview );

Filename = mdata. Get (0). tostring ();

Bitmap bitmap = bitmapfactory. decodefile (filename );

Mimageview. setimagebitmap (Bitmap );

Iii. Problem Analysis

After debugging and troubleshooting, we found that our bitmap image reaches 3 mb. If it is 3 kb, no error occurs. What is the principle?

4. Let's take a look at how Android memory overflow occurs?

The android virtual machine is a register-based Dalvik. Its maximum heap size is generally 16 m, and some machines are 24 m. Therefore, the memory space we can use is limited. If our memory usage exceeds a certain level, an outofmemory error occurs.

Why is memory insufficient? I think there are two main reasons:

The program itself occupies a certain amount of memory, and the program requires a larger memory space when using a large bitmap. Improper control may easily cause inner outofmemory.

5. Android restrictions on application memory

The memory available for a single process on different Android devices is different. You can view the/system/build. Prop file.

Dalvik. VM. heapsize = 24 m

Dalvik. VM. heapgrowthlimit = 16 m

You can change this restriction on your own. Of course, you need to root the device first.

Vi. Analysis of Bitmap Loading Principles

1. bitmapfactory provides several decoding methods (decodebytearray (), decodefile (), decoderesource (), and so on) to create a bitmap (Bitmap) object from multiple resources. You can select the most suitable decoding method based on your image data source. These method views allocate memory to construct bitmap objects, so it is easy to cause outofmemory (OOM) exceptions. Each decoding method has additional features. You can specify the decoding method through the bitmapfactory. Options class.

2. Try not to use setimagebitmap, setimageresource, or bitmapfactory. decoderesource directly uses the image path to set a large image, because after decode is completed, these functions are ultimately completed through the createbitmap on the Java layer, requiring more memory consumption. Instead, use bitmapfactory. decodestream to create a bitmap, and then call the above method to set it as the source of imageview. The biggest secret of decodestream is that it directly calls JNI> nativedecodeasset () to complete the decode, and does not need to use the createbitmap on the Java layer, thus saving the Java Layer Space. Below are several ways to load images using inputstream:

Method 1: load the specified image in the resource file

Inputstream is = getresources (). openrawresource (R. drawable. Temp );

Method 2: load the image under the assest directory

Assetmanager ASM = getassetmg ();

Inputstream is = ASM. Open (name); // name: image name

Method 3: load images under the SD card directory

String Path = environment. getexternalstoragedirectory (). tostring () + "/dcim/device.png ";

Inputstream is = new fileinputstream (PATH)

VII. Solutions

Private imageview preview;

// 1. Load bitmap

String Path = environment. getexternalstoragedirectory (). tostring () + "/dcim/device.png ";

Inputstream is = new fileinputstream (PATH)

// 2. Set K cache for bitmap

Bitmapfactory. Options opts = new bitmapfactory. Options ();

Opts. intempstorage = new byte [100*1024];

// 3. Set the bitmap color display optimization mode

// Alpha_8: each pixel occupies 1 byte memory (8 bits)

// Argb_4444: each pixel occupies 2 bytes of memory (16 bits)

// Argb_8888: each pixel occupies 4 bytes of memory (32 bits)

// Rgb_565: each pixel occupies 2 bytes of memory (16 bits)

// The default color mode for Android is argb_8888. The color mode features the most delicate colors and the highest display quality. However, the memory used is also the largest. This means that a pixel occupies 4 bytes of memory. Let's make a simple calculation question: 3200*2400*4 bytes // = 30 m. So amazing number! Even if the lifecycle exceeds 10 s, Android will not agree.

Opts. inpreferredconfig = bitmap. config. rgb_565;

// 4. Set the image to be recycled. Create a bitmap to store pixel's memory space.

Opts. inpurgeable = true;

// 5. Set the bitmap zoom Ratio

// Width, hight is set to the original four-point one (use an integer multiple of 2 for this parameter), which also reduces the memory size occupied by the bitmap; for example, an image with a resolution of 2048 * 1536px is decoded using the insamplesize value of 4. The generated bitmap size is approximately 512 * 384px. This method only requires MB of memory (assuming bitmap is configured as // argb_8888) compared to the full image memory usage of 12 Mb ).

Opts. insamplesize = 4;

// 6. Set the size of the decoded bitmap

Opts. ininputretriable = true;

// 7. Decode bitmap

Bitmap BTP = bitmapfactory. decodestream (is, null, opts );

// 8. Display bitmap

Preview. setimagebitmap (Bitmap );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.