Android Memory Management Research

Source: Internet
Author: User

1. Basic knowledge of memory management

Http://www.cnblogs.com/xingfuzzhd/p/3485924.html

    • 1. mImageView.setImageResource(R.drawable.my_image); This code calls Bitmapfactory.decodestream () to generate a Bitmap. So don't think it saves memory than creating Bitmap yourself.

3. Actual test:

I have used several methods of invoking images to test:

The first type:

Load resource ID directly
ImageView image = (ImageView) Findviewbyid (r.id.imageview1)

The second type:

Create a function to load the picture according to the ID. Recycle Bitmap when killing Activity

 Public Bitmapdrawable getimagebitmap (int= Bitmapfactory.decoderesource (context.getresources (),  Idimage);  Images.add (bitmap);  returnnew  bitmapdrawable (context.getresources (), bitmap);}

The third type:

Create a class

/*** Solution for OOM------> Compress bitmap*/ Public classBitmaputils {Private Static FinalString TAG = "Bitmaputils"; /*** Calulate the Compress rate*/    Private Static intCalculateinsamplesize (Bitmapfactory.options opts,intReqheight,intreqwidth) {        if(opts = =NULL) {            return-1; }        intwidth =Opts.outwidth; intHeight =Opts.outheight; intSampleSize = 1; if(Width > Reqwidth | | height >reqheight) {            intHeightRatio = (int) (height/(float) reqheight); intWidthRatio = (int) (width/(float) reqwidth); SampleSize= (HeightRatio > WidthRatio)?Widthratio:heightratio; }        returnsamplesize; }        /*** Compress an image refer to the goal dimension *@paramRes *@paramResId *@paramReqwidth *@paramReqheight *@returnBitmap*/     Public StaticBitmap Decodesampledbitmapfromresource (Resources res,intResId,intReqwidth,intreqheight) {Bitmapfactory.options opts=Newbitmapfactory.options (); Opts.injustdecodebounds=true;        Bitmapfactory.decoderesource (res, resId, opts); intSampleSize =calculateinsamplesize (opts, reqheight, reqwidth); LOG.I (TAG,"Before[width:" +opts.outwidth+ ", Height:" +opts.outheight+ "]"); Opts.injustdecodebounds=false; Opts.insamplesize=samplesize; /*newly added*/Opts.inpreferredconfig=Bitmap.Config.RGB_565; Opts.inpurgeable=true; Opts.ininputshareable=true; /*newly added*/log.i (TAG,"Insamplesize=" +samplesize); Bitmap Bitmap=Bitmapfactory.decoderesource (res, resId, opts); LOG.I (TAG,"After[width:" +bitmap.getwidth () + ", Height:" +bitmap.getheight () + "]"); returnbitmap; }    /*** Compress an image refer to the goal dimension * *@paramdata Image byte array *@paramReqwidth *@paramReqheight *@return     */     Public StaticBitmap Decodesampledbitmapfrombytearray (byte[] Data,intReqwidth,intreqheight) {Bitmapfactory.options opts=Newbitmapfactory.options (); Opts.injustdecodebounds=true; Bitmapfactory.decodebytearray (data,0, Data.length, opts); LOG.I (TAG,"Before[width:" +opts.outwidth+ ", Height:" +opts.outheight+ "]"); Opts.insamplesize=calculateinsamplesize (opts, reqheight, reqwidth); LOG.I (TAG,"Insamplesize=" +opts.insamplesize); Opts.injustdecodebounds=false; Bitmap Bitmap= Bitmapfactory.decodebytearray (data, 0, Data.length, opts); LOG.I (TAG,"After[width:" +bitmap.getwidth () + ", Height:" +bitmap.getheight () + "]"); returnbitmap; }         Public StaticBitmap Decoderesourcewithlowmemory (Context context,intresId) {bitmapfactory.options opt=Newbitmapfactory.options (); //bitmap.config.alpha_8,bitmap.config.argb_4444,bitmap.config.rgb_565//set These parameters the effect is similar, it is quite save memory Ah, but also with the original clarity is quiteOpt.inpreferredconfig =Bitmap.Config.RGB_565; Opt.inpurgeable=true; Opt.ininputshareable=true;        returnBitmapfactory.decoderesource (Context.getresources (), resId, opt); }         Public StaticBitmap Loadtoimageviewfromresource (Resources res,intResid,imageview ImageView) {Bitmapfactory.options opts=Newbitmapfactory.options (); Opts.injustdecodebounds=true;        Bitmapfactory.decoderesource (res, resId, opts); intReqheight =imageview.getlayoutparams (). Height; intReqwidth =imageview.getlayoutparams (). width; intSampleSize =calculateinsamplesize (opts, reqheight, reqwidth); LOG.I (TAG,"Before[width:" +opts.outwidth+ ", Height:" +opts.outheight+ "]"); Opts.injustdecodebounds=false; Opts.insamplesize=samplesize; /*newly added*/Opts.inpreferredconfig=Bitmap.Config.RGB_565; Opts.inpurgeable=true; Opts.ininputshareable=true; /*newly added*/log.i (TAG,"Insamplesize=" +samplesize); Bitmap Bitmap=Bitmapfactory.decoderesource (res, resId, opts); LOG.I (TAG,"After[width:" +bitmap.getwidth () + ", Height:" +bitmap.getheight () + "]"); returnbitmap; }}

4. Test process:

Create two activity, each loaded with several large images, and then switch between the two activity.

The first method, switching multiple times after OOM (out of memory). The second method, the faster OOM.

Third, if the compression rate is high enough, there will be almost no OOM. For example, the original image is 2048 * 1600, then the target ImageView size is 200* 150. The last memory consumption will be greatly reduced. And if

The resolution gap between the two is small, the final memory consumption will not be optimized, and finally will be OOM.

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.