Bitmapfactory. decoderesource and bitmapfactory. decodestream are familiar to anyone who has experience developing Android apps. There are also many problems with the OOM of Bitmap on the Internet.ArticleAfter analysis, many articles have suggested bitmapfactory to avoid oom. decodestream, but I have not found the reason for the specific explanation so far, so I will investigate it when I am free during the 10.1 period. I hope the results will be helpful to everyone.
Let's take a time sequence diagram first. After reading this diagram, it is estimated that many problems need not be clarified:
Here are some instructions:
1. For details about the memory calculation method of the function externalbytesavailable (...), see the article 《Android bitmap memory limit";
2. bitmap. createscaledbitmap may be scaled. For the options used for scaling, see the following section.Code:
[Java] View plaincopy
-
- Public StaticBitmap decoderesourcestream (Resources res, typedvalue value,
- Inputstream is, rect pad, options opts ){
-
-
- If(OPTs =Null){
-
- Opts =NewOptions ();
-
- }
-
- If(OPTs. indensity =0& Value! =Null){
-
- Final IntDensity = value. density;
-
- If(Density = typedvalue. density_default ){
-
- Opts. indensity = displaymetrics. density_default;
- }Else If(Density! = Typedvalue. density_none ){
-
- Opts. indensity = density;
-
- }
-
- }
-
-
- If(OPTs. intargetdensity =0& Res! =Null){
- Opts. intargetdensity = res. getdisplaymetrics (). densitydpi;
-
- }
-
-
- ReturnDecodestream (is, pad, opts );
-
- }
When it is enlarged, it will increase the possibility of oom;
3, bitmapfactory. decoderesource is better than bitmapfactory. for decodestream, if the options opts parameter is not set, the memory usage is high because bitmapfactory is used. in finishdecode, a bitmap is added here. createscaledbitmap operation;
4. For Efficiency Comparison Between bitmapfactory. decoderesource and bitmapfactory. decodestream, see the article 《Android Memory Optimization Test"
5. About the article 《Android Memory Optimization TestAs mentioned inBitmapdrawable is the most memory-saving method, because frameworks \ base \ core \ Java \ Android \ content \ res \ resources. java caches bitmap. All arrays in the text actually point to the same bitmap. This can also be done through bitmap. tostring output information to prove.
Note: The sequence chart cannot be clearly displayed. Download the source image at http://download.csdn.net/detail/imyfriend/4617329.
In the previous article, we talked about the generation of Bitmap. Now, let's talk about the release of Bitmap.
The release of bitmap is relatively simple. Just upload a time series diagram first!
Notes:
1. androidpixelref is set in graphicsjni: setjavapixelref generated by bitmap. For details, see bitmapfactory. decoderesource vs bitmapfactory. decodestream. The Code is as follows:
[CPP] View plaincopy
-
- BoolGraphicsjni: setjavapixelref (jnienv * ENV, skbitmap * bitmap,
- Skcolortable * ctable,BoolReportsizetovm ){
-
- ......
-
- Skpixelref * Pr = reportsizetovm?
-
- NewAndroidpixelref (ENV, ADDR, size, ctable ):
- NewSkmallocpixelref (ADDR, size, ctable );
-
-
-
- ......
-
- }
2. vmruntime. trackexternalfree returns the heap memory occupied by bitmap. For more information, see Dalvik \ VM \ native \ dalvik_system_vmruntime.c.