OMM in Android 5.0 -


in application capturing images , creating bitmap, below code

byte[] data; options=new bitmapfactory.options();// create object of bitmapfactory's option method further option use options.inpurgeable = true;  cleaned=bitmapfactory.decodebytearray(data, 0, data.length,options); 

this working fine in kitkat , below , able captured more 30 images, in lollipop when captured 19 images no issue, after app crashed oom.

can 1 give me solution, how handle oom in lollipop.

i capturing image bycalling imagecleanuptask follwoing code

private camera.parameters previewparams=null;    previewparams=camera.getparameters();            camera.parameters pictureparams=camera.getparameters();          camera.size picturesize=              xact.host.getpicturesize(xact, pictureparams);                    system.out.println("picture size : "+picturesize.width+ " x "+picturesize.height);            //pictureparams.setpicturesize(picturesize.width,picturesize.height);          list<camera.size> sizes = pictureparams.getsupportedpreviewsizes();          for(camera.size s:sizes)          {          	system.out.println(s.height+"///"+s.width);          }          camera.size cs = sizes.get(0);          pictureparams.setpreviewsize(cs.width, cs.height);          //pictureparams.setpicturesize(320,460);                    pictureparams.setpictureformat(imageformat.jpeg);            if (xact.flashmode != null) {            pictureparams.setflashmode(xact.flashmode);          }            if (!onorientationchange.isenabled()) {            setcamerapictureorientation(pictureparams);          }            camera.setparameters(xact.host.adjustpictureparameters(xact,                                                                 pictureparams));          xact.cameraview=this;            postdelayed(new runnable() {            @override            public void run() {              try {              	if(picturetransactioncallback!=null)              		picturetransactioncallback=null;              	picturetransactioncallback=new picturetransactioncallback(xact);                camera.takepicture(xact, null,              		  picturetransactioncallback);                picturetransactioncallback=null;              }              catch (exception e) {                android.util.log.e(getclass().getsimplename(),                                   "exception taking picture", e);                // todo out library clients              }            }          }, 1000);      private class picturetransactioncallback implements        camera.picturecallback {      picturetransaction xact=null;        picturetransactioncallback(picturetransaction xact) {        this.xact=xact;      }        @override      public void onpicturetaken(byte[] data, camera camera) {        camera.setparameters(previewparams);          if (data != null) {      	  if(imagecleanuptask!=null)      		  imagecleanuptask=null;      	  imagecleanuptask=  new imagecleanuptask(getcontext(), data, cameraid, xact);      	  imagecleanuptask.start();      	  data=null;        }          if (!xact.usesingleshotmode()) {          startpreview();        }      }    }

and imagecleanuptask thread.

public class imagecleanuptask extends thread {    private byte[] data;    private int cameraid;    private picturetransaction xact=null;    private boolean applymatrix=true;   static bitmapfactory.options options;   static matrix matrix=null;   static bitmap cleaned=null;   static exifinterface exif=null;   static activitymanager am;   static bitmap original;   public imagecleanuptask(context ctxt, byte[] data, int cameraid,                     picturetransaction xact) {      this.data=data;      this.cameraid=cameraid;      this.xact=xact;        float heappct=(float)data.length / calculateheapsize(ctxt);        applymatrix=(heappct < xact.host.maxpicturecleanupheapusage());      activitymanager mgr = (activitymanager)ctxt.getsystemservice(context.activity_service);  	activitymanager.memoryinfo info = new activitymanager.memoryinfo();  	mgr.getmemoryinfo(info);  	system.out.println("available ememory app: "+info.availmem/(1024*1024));      //  double available = new double(debug.getnativeheapsize())/1048576;       options=new bitmapfactory.options();// create object of bitmapfactory's option method further option use      options.inpurgeable = true;    }      @override    public void run() {      camera.camerainfo info=new camera.camerainfo();        camera.getcamerainfo(cameraid, info);               if (applymatrix) {        if (info.facing == camera.camerainfo.camera_facing_front) {          if (xact.host.getdeviceprofile().portraitffcflipped()              && (xact.displayorientation == 90 || xact.displayorientation == 270)) {            matrix=flip(new matrix());          }          else if (xact.mirrorffc()) {            matrix=mirror(new matrix());          }        }          try {          int imageorientation=0;            if (xact.host.getdeviceprofile().usedeviceorientation()) {            imageorientation=xact.displayorientation;          }          else {            exif=new exifinterface();            exif.readexif(data);              integer exiforientation=                exif.gettagintvalue(exifinterface.tag_orientation);              if (exiforientation != null) {              if (exiforientation == 6) {                imageorientation=90;              }              else if (exiforientation == 8) {                imageorientation=270;              }              else if (exiforientation == 3) {                imageorientation=180;              }              else if (exiforientation == 1) {                imageorientation=0;              }              else {                // imageorientation=                // xact.host.getdeviceprofile().getdefaultorientation();                //                // if (imageorientation == -1) {                // imageorientation=xact.displayorientation;                // }              }            }          }          if (imageorientation != 0) {            matrix=                rotate((matrix == null ? new matrix() : matrix),                       imageorientation);          }        }        catch (ioexception e) {          log.e("cwac-camera", "exception parsing jpeg", e);          // todo: ripple client        }        if (matrix != null) {      	  if( options==null){      		  options=new bitmapfactory.options();// create object of bitmapfactory's option method further option use            options.inpurgeable = true;      	  }           original=              bitmapfactory.decodebytearray(data, 0, data.length,options);            cleaned=              bitmap.createbitmap(original, 0, 0, original.getwidth(),                                  original.getheight(), matrix, true);          original.recycle();        }      }      if (xact.needbitmap) {        if (cleaned == null) {      	  if( options==null){      	   options=new bitmapfactory.options();// create object of bitmapfactory's option method further option use            options.inpurgeable = true;      	  }          cleaned=bitmapfactory.decodebytearray(data, 0, data.length,options);        }          xact.host.saveimage(xact, cleaned);      }      if (xact.needbytearray) {        if (matrix != null) {          bytearrayoutputstream out=new bytearrayoutputstream();            // if (exif == null) {          cleaned.compress(bitmap.compressformat.jpeg, 100, out);          // }          // else {          // exif.deletetag(exifinterface.tag_orientation);          //          // try {          // exif.writeexif(cleaned, out);          // }          // catch (ioexception e) {          // log.e("cwac-camera", "exception writing jpeg",          // e);          // // todo: ripple client          // }          // }            data=out.tobytearray();            try {          	out.flush();            out.close();          }          catch (ioexception e) {            log.e(cameraview.tag, "exception in closing baos???", e);          }        }          xact.host.saveimage(xact, data);      }      try{       if(cleaned!=null)  	  cleaned.recycle();      data=null;      system.gc();      }catch(exception e)      {      	e.printstacktrace();      }    }      // http://stackoverflow.com/a/8347956/115145      private matrix mirror(matrix input) {      float[] mirrory= { -1, 0, 0, 0, 1, 0, 0, 0, 1 };      matrix matrixmirrory=new matrix();        matrixmirrory.setvalues(mirrory);      input.postconcat(matrixmirrory);        return(input);    }      private matrix flip(matrix input) {      float[] mirrory= { -1, 0, 0, 0, 1, 0, 0, 0, 1 };      matrix matrixmirrory=new matrix();        matrixmirrory.setvalues(mirrory);      input.prescale(1.0f, -1.0f);      input.postconcat(matrixmirrory);        return(input);    }      private matrix rotate(matrix input, int degree) {      input.setrotate(degree);        return(input);    }      @targetapi(build.version_codes.honeycomb)    private  int calculateheapsize(context ctxt) {     am=          (activitymanager)ctxt.getsystemservice(context.activity_service);      int memoryclass=am.getmemoryclass();        if (build.version.sdk_int >= build.version_codes.honeycomb) {        if ((ctxt.getapplicationinfo().flags & applicationinfo.flag_large_heap) != 0) {          memoryclass=am.getlargememoryclass();        }      }        return(memoryclass * 1048576); // mb * bytes in mb    }    }
after capturing image added bitmap image editing image line. xact.host.saveimage(xact, cleaned);

in nexus7(kitkat) able take more images , editable images,but when run app in samsung tablet(lollipop) app crashed.

for every capture image log 05-15 15:52:43.204: i/art(4858): explicit concurrent mark sweep gc freed 2656(235kb) allocspace objects, 2(25mb) los objects, 8% free, 173mb/189mb, paused 566us total 34.267ms 05-15 15:52:43.229: i/art(4858): explicit concurrent mark sweep gc freed 10(384b) allocspace objects, 0(0b) los objects, 8% free, 173mb/189mb, paused 554us total 24.596ms

here 173mb/189mb showing used heap size , 20mb 30mb incresing every captured image, when reached 512 app crashed in samsung


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -