Outofmemoryerror Bytearrayoutputstream Sending Large File To Ws
Solution 1:
ByteArrayOutputStream starts out by allocating either 32 bytes or a constructor-specified amount of memory by default. When that buffer has been filled-up, ByteArrayOutputStreamdoubles the size of the buffer. For large objects, this can be a real problem. Your best alternative is to either
- Make use of the constructor-specified buffer size, or
- Extend
ByteArrayOutputStreamand override the write methods so that reallocation is more advantageous for your stream.
Solution 2:
Instead of writing the code to copy the stream yourself, you might try using a library class to do it.
In Guava, the ByteStreams class is available. If you're a Commons IO kind of person, there's IOUtils.
In IOUtils, your code would look something like this:
FileInputStreamfileInputStream=newFileInputStream(fichero);
OutputStreamdos= ...
IOUtils.copy(fileInputStream, dos);
I've left out the necessary exception handling and stream closing.
Solution 3:
This is happening because - If neither setFixedLengthStreamingMode(int) when the body length is known in advance, nor setChunkedStreamingMode(int) is set. In that case HttpURLConnection is forced to buffer the complete request body in memory before it is transmitted, wasting (and possibly exhausting) heap and increasing latency. Very well explained in the link - http://developer.android.com/reference/java/net/HttpURLConnection.html
Kindly add below line to your code - HttpUrlConnectionObject.setChunkedStreamingMode(maxBufferSize); for default system value set 0 HttpUrlConnectionObject.setChunkedStreamingMode(0);
This works for me.
Post a Comment for "Outofmemoryerror Bytearrayoutputstream Sending Large File To Ws"