Skip to content Skip to sidebar Skip to footer

Outofmemoryerror Bytearrayoutputstream Sending Large File To Ws

im sending videos to webservice and works ok with videos less than 10MB, if the video is about 12MB give me outofmemoryerror: This is my code: FileInputStream fileInputStream = ne

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

  1. Make use of the constructor-specified buffer size, or
  2. Extend ByteArrayOutputStream and 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"