eclipse - android - heap size error -


i developed android game using eclipse, have many resources , images in it, using sdp size unit sizing images , fine in devices:

https://github.com/intuit/sdp

also picasso library load images on imageviews,

the main problem in devices out of heap size memory error.

question that, have declare different images different sizes in subfolders ldpi, mdpi , so... if using sdp , picasso?

android when loading large background or lot of pictures, leading memory overflow (out of memory error), based on experience have dealt these issues , other developer experience, finishing solution follows (parts of code , text source can not verified) :

option one, calling attention method of reading image, appropriate compression try not use or setimageresource setimagebitmap bitmapfactory.decoderesource or set large image, because these functions after completion of decode, createbitmap java layer complete, need consume more memory. instead, use first method bitmapfactory.decodestream, create bitmap, set imageview source, decodestream biggest secret lies in direct call jni >> nativedecodeasset () complete decode, no longer need use createbitmap java layer , saving space java layer.

    inputstream = this.getresources () openrawresource (r.drawable.pic1).;      bitmapfactory.options options = new bitmapfactory.options ();      options.injustdecodebounds = false;      options.insamplesize = 10; // width, hight tenth of original      bitmap btp = bitmapfactory.decodestream (is, null, options); 

if add config parameter image when reading, can reduce load memory, thereby preventing throwing out of memory exception.

/ **

 * in provinces memory reads local resources image   * @param context   * @param resid   * @return   * /  public static bitmap readbitmap (context context, int resid) {      bitmapfactory.options opt = new bitmapfactory.options ();      opt.inpreferredconfig = bitmap.config.rgb_565;      opt.inpurgeable = true;      opt.ininputshareable = true;      // resource image      . inputstream = context.getresources () openrawresource (resid);      return bitmapfactory.decodestream (is, null, opt);      } 

in addition, decodestream take pictures directly read bytecode, not automatically adapt according various resolutions of machine, use decodestream, need hdpi , mdpi, ldpi configured corresponding image resources, or in different resolutions machines same size (number of pixels), displayed size not right.

option ii, when appropriate , timely recovery of memory image typically activity or fragment in onstop / ondestroy pictures when can release resources:

if (imageview! = null && imageview.getdrawable ()! = null) {

  bitmap oldbitmap = ((bitmapdrawable) imageview.getdrawable ()) getbitmap ().;    imageview.setimagedrawable (null);    if (oldbitmap! = null) {          oldbitmap.recycle ();          oldbitmap = null;    } 

}

// other code.

system.gc ();

upon release of resources, need pay attention release of associated drawable bitmap or whether there reference other classes. if normal call, can bitmap.isrecycled () method determine whether there marked recovery; , if used ui thread interface-related code, need careful avoid possible recycling of resources used, or throw system abnormalities: e / androidruntime: java.lang.illegalargumentexception: can not draw recycled bitmaps , exception can not captured , processed.

option three, avoid unnecessary when loading full image need know size of image under situation, can not load image memory intact. image compression using bitmapfactory time, bitmapfactory.options injustdecodebounds after setting true, use decodefile () , other methods, can calculate size of picture in state not allocate space. example:

bitmapfactory.options opts = new bitmapfactory.options ();

// set true injustdecodebounds

opts.injustdecodebounds = true;

// use decodefile obtained image width , height

bitmapfactory.decodefile (path, opts);

// print out picture width , height

log.d ( "example", opts.outwidth + "," + opts.outheight); (ps: in fact, principle of header information read image of basic image information)

option iv, optimized dalvik vm heap memory allocation heap (heap) vm memory part, allocated dynamically. heap size not static, there distribution mechanism control size. example, initial heap 4m large, when 4m space occupied more 75% of time, re-allocate heap 8m big; when 8m occupied more 75% allocated heap 16m large. upside down, when 16m heap use less 30%, reduce size 8m big. reset heap size, in compression, related memory copy, change heap size has adverse effect on efficiency. heap utilization heap utilization. when actual utilization percentage deviation time, virtual memory heap size adjust time in gc, actual occupancy rate percentage closer. settargetheaputilization method dalvik.system.vmruntime class can provide enhanced processing efficiency program heap memory.

private final static float target_heap_utilization = 0.75f;

// when program can call oncreate

vmruntime.getruntime () settargetheaputilization (target_heap_utilization).;

programme v, custom heap (heap) memory size android projects, affect performance bottleneck android own memory management issues, current mobile phone manufacturers more stingy ram software fluency sensitive impact on performance of ram, in addition optimized dalvik vm heap memory foreign assignment, can define own software force memory size, use dalvik.system.vmruntime class dalvik provided set minimum heap memory, example:

private final static int cwj_heap_size = 6 * 1024 * 1024;

vmruntime.getruntime () setminimumheapsize (cwj_heap_size);. // set minimum heap memory 6mb in size.

however, above method still problem, in fact, change function setminimumheapsize lower limit of heap, prevents frequent heap memory allocation, when setting minimum heap size exceeds upper limit (max heap size) while still using stack limit, lack of memory no effect.

finally, tell picture takes memory algorithm process. android in base class image processing bitmap, definition, bitmap. memory-intensive algorithms such as: image width * height * config. if config set argb_8888, above config 4. 480 * 320 image memory usage 480 * 320 * 4 byte. default android memory footprint process of 16m, bitmap held in addition java data, underlying c ++ graphics library hold skia skbitmap objects, recommended memory image size should not exceed 8m. can adjusted, can set parameters when compiling source code.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -