OutputStream OutOfMemoryError when sending HTTP

The HttpUrlConnection is buffering the data so that it can set the Content-Length header (per HTTP spec).

One alternative, if your destination server supports it, is to use “chunked” transfers. This will buffer only a small portion of data at a time. However, not all services support it (Amazon S3, for example, doesn’t).

Another alternative (and imo a better one) is to use Jakarta HttpClient. You can set the “entity” in a request from a file, and the connection code will set request headers appropriately.


Edit: nos commented that the OP could call HttpURLConnection.setFixedLengthStreamingMode(long length). I was unaware of this method; it was added in 1.5, and I haven’t used this class since then.

However, I still suggest using Jakarta HttpClient, for the simple reason that it reduces the amount of code that the OP has to maintain. Code that is boilerplate, yet still has the potential for errors:

  • The OP correctly handles the loop to copy between input and output. Usually when I see an example of this, the poster either doesn’t properly check the returned buffer size, or keeps re-allocating the buffers. Congratulations, but you now have to ensure that your successors take as much care.
  • The exception handling isn’t quite so good. Yes, the OP remembers to close the connections in a finally block, and again, congratulations on that. Except that either of the close() calls could throw IOException, keeping the other from executing. And the method as a whole throws Exception, so that the compiler isn’t going to help catch similar errors.
  • I count 31 lines of code to setup and execute the response (excluding the response code check and the URL computation, but including the try/catch/finally). With HttpClient, this would be somewhere in the range of a half dozen LOC.

Even if the OP had written this code perfectly, and refactored it into methods similar to those in Jakarta Commons IO, s/he shouldn’t do that. This code has been written and tested by others. I know that it’s a waste of my time to rewrite it, and suspect that it’s a waste of the OP’s time as well.

Leave a Comment