Fix Oom Small note

Source: Internet
Author: User

The same as the conjecture is oom. Come back to meet a little familiar SB, give me gas .... Never mind..... Brother is also a variety of reasons back to Hefei. Keep looking at the problem.

The interface of this place is like this

The red line is a three linearlayout, each onclick will popupwindow an interface to prompt the user.

An oom occurred while loading the second pop-up Popupwindow

Let's see the size of the piece.

These three pictures were originally about 350KB, now more than 100 KB is already compressed.

But let's start with the basics and turn to JPG.

Let's try it again, Note3 is no longer a problem.

If it's just work, it's over, but I'm going to go on with it.

Let's figure out how much memory it takes to approximate the hand.

BMP: For high-quality storage for computers
JPG: Save for computer or network with good quality
PNG: For high-quality storage for computers or networks
The size of the picture = total pixels of the picture * size of each pixel

Monochrome: Up to 2 colors per pixel then only one bits of length 1 is required to represent 1 pixels of 1/8 bytes
16 colors: Each pixel can represent a maximum of 16 colors, 0-15 0000-1111 then you only need to use a length of 4 bits to indicate that one pixel accounted for 1/2
256 colors: Each pixel can represent up to 256 kinds of 0-255 0000 0000-1111 1111 Only one bits with a length of 8 is required to represent a pixel of 1byte
24-bit: R 1 byte 0-255
: G 1 byte 0-255
: B 1 byte 0-255 so one pixel is 3 bytes
JPG: similar to RAR compression
PNG: Also compressed according to a special algorithm

That's what we wrote in the XML layout.

4 bytes required by default for argb_8888 mode

Second Click appears oom popup_two picture details

Then his memory footprint is the size of the picture = total pixels of the picture * size of each pixel

1080*1860*4=8035200 bytes

And the error is more than 8,035,212 out of 12 bytes is to save the picture information data

Then the conversion into megabytes is 7.6629639 megabytes (MB)

So look again.

When this image is loaded into memory, 8035212 bytes is 7.6629639 megabytes (MB) exceeding the remaining 6498924 free byte size and 6MB head memory. Then the memory overflows.

That said so much, although the above has solved the problem, but beyond this, we can do something?

1. Layout in Go android:background and Android: Src These properties actually invoke the View.setbackgroundresource and View.setimageresource methods, which are actually drawable to get the resource ID and then get the resource. They will decode the picture and end up with the Java layer CreateBitmap, which will consume more memory.

We can actually replace it with Decodestream because Decodestream calls Jni>>nativedecodeasset () directly to complete the decode, eliminating the need to use the Java layer's CreateBitmap, This saves the Java layer of space. In addition, we can set the parameters of the picture, for example, set to Bitmap.Config.RGB_565 to reduce memory overhead. Because the Android document describes Bitmap.Config.RGB_565 each pixel exists in 2 bytes, and the default Bitmap.Config.ARGB_8888 per pixel requires 4 bytes, theoretically saving half the space.

The android:background in the layout file is removed and the background is set in the Java file.

As follows:

Bitmapfactory.options opt = newbitmapfactory.options ();

Opt.inpreferredconfig = Bitmap.Config.RGB_565;

Opt.inpurgeable = true;

Opt.ininputshareable = true;

Get a picture of a resource

InputStream is = Context.getresources (). Openrawresource (ResId);

Bitmap Bitmap = Bitmapfactory.decodestream (is,null, opt);

Is.close ();

Returnnew bitmapdrawable (Context.getresources (), bitmap);

2. Zoom load Large image

IV = (ImageView) Findviewbyid (R.ID.IV);

(1) Get phone width and height windowmanager

WindowManager wm = (WindowManager) getsystemservice (Window_service);

ScreenWidth = Wm.getdefaultdisplay (). GetWidth ();

ScreenHeight = Wm.getdefaultdisplay (). GetHeight ();

To create a bitmap configuration parameter

Bitmapfactory.options opts = new Options ();

Do not go to the real parse bitmap returns no bitmap but can get the width and height of the picture

Opts.injustdecodebounds = true;

Bitmapfactory.decodefile ("/mnt/sdcard/xxx.jpg", opts);

(2) Get the width and height of the image

int imgwidth = Opts.outwidth;

int imgheight = Opts.outheight;

System.out.println ("ImgWidth of the Picture:" +imgwidth+ "-----" +imgheight);

(3) Calculate the zoom ratio

int scale = 1;

int scalex = Imgwidth/screenwidth;

int ScaleY = Imgheight/screenheight;

if (ScaleX >=scaley && ScaleX >1) {

scale = ScaleX;

}

if (ScaleY > ScaleX && scaley >1) {

scale = ScaleY;

}

System.out.println ("Zoom ratio:" +scale);

(4) Display the picture according to the actual calculated zoom ratio

Opts.insamplesize = scale;

(5) True load bitmap

Opts.injustdecodebounds = false;

Bitmap Bitmap = Bitmapfactory.decodefile ("/mnt/sdcard/xxx.jpg", opts);

(6) Display the bitmap on the picture

Iv.setimagebitmap (bitmap);

3. Moderate Recovery

if (bitmapobject.isrecycled () ==false) {//If not recycled

Bitmapobject.recycle ();

System.GC ();

}

4. Custom Minimum head memory size

5. Use a soft reference.



Well, the gas also disappear, do not write so much, in fact, as long as the normal running of the basic problem is nothing, but occasionally review the basic knowledge of the future is still helpful.

Fix Oom Small note

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.