Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of memory on a byte allocation (Bitmap as String to webservice using soap)

Am having a bitmap , so I want to upload a webserivceas string and want to retrive the string.

For converting bitmap to string am using:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

strBase64 = Base64.encodeToString(byteArray, Base64.URL_SAFE);

this above String is using as property to in soapobject to upload.

But am getting Out of memory on a 11674900-byte allocation, while print and uploading.

And if i debugged the issue, without printing am getting

com.sun.jdi.InvocationException occurred invoking method.

on soaprequest.

How to resolve this issue and to upload image to webservice as string ?

like image 890
Udaykiran Avatar asked Nov 10 '11 11:11

Udaykiran


1 Answers

You are creating 3 copies of an 11MB image(bitmap, stream, strBase64). So reduce the memory usage by calling

bitmap.recycle();

below this line:

bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

Also close the stream when you are done with it(below stream.toByteArray();):

stream.close();
stream = null;

Remember that there is no guarantee that memory will be cleaned immediately after these calls. Proper way to handle this type of situation is to transfer large files chunk by chunk.

like image 91
Caner Avatar answered Nov 08 '22 05:11

Caner