Android: Uploading number of images causing Heap size to grow big - how to solve that? -



Android: Uploading number of images causing Heap size to grow big - how to solve that? -

i'm writing app user can take bunch of pictures (up 20) , upload server. images need uploaded together.

here logic:

take each picture, display thumb on screen , resize image on sd 800x600 90 quality create object, populate properties (images) base64 string serialize object using gson upload string

while testing getting errors "out of memory" when processing images. thought , stackoverflow complains - it's bug bitmapfactory. yes, error shows while resizing image not related operation.

while take pictures , process them (resize, etc) - heap size stays below 7-8mb. it's 2-3mb more usual app state.

when submit images server , gson + base64 encoder comes play - "explodes" , this:

well - see - after process completed allocated memory get's downwards expected heap size stays. now, when take more pictures or app - start out of memory errors.

here code upload json. suggestions on improving or handling that? maybe can stream json file , http file or something?

while (!c.isafterlast()) { string info = c.getstring(colobjectdata); trailerinspection trailerinspection = mygsonwrapper.getmygson().fromjson(data, trailerinspection.class); //load image info (trailerunitinspection trailerunitinspection : trailerinspection.unitinspections) { (filecontainer filecontainer : trailerunitinspection.images) { filecontainer.datafromfile(mcontext); } } info = mygsonwrapper.getmygson().tojson(trailerinspection); myhttpresponse response = processpost("/trips/" + c.getstring(coltripid) + "/trailerinspection", data); if (response.code == httpurlconnection.http_ok) { processed.add(c.getstring(colgid)); } c.movetonext(); } c.close();

the problem you're creating , keeping whole string, prepared sending, in internal memory.

string info = mygsonwrapper.getmygson().tojson(trailerinspection);

this string may large. should stream info in chunks server. haven't used gson yet, in docs found jsonwriter. take @ class.

update:

contentproducer cp = new contentproducer() { public void writeto(outputstream outstream) throws ioexception { jsonwriter author = new jsonwriter(new outputstreamwriter(outstream, "utf-8")); // write code here writer.flush(); } }; httpentity entity = new entitytemplate(cp); httppost httppost = new httppost("http://server.address"); httppost.setentity(entity);

android

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -