OutputStream from a URLConnection produces an OutOfMemory Exception on
large files
I need to upload large files (up to 2 gigabytes) to an Apache
webserver. This works fine with smaller files, however as soon as they
go over a certain size, java dumps with a: java.lang.OutOfMemoryError:
Java heap space.
Apparently this is a feature. The error results from HttpURLConnection
returning a ByteArrayOutputStream, so the file is stored in memory
before being send to the webserver - and no amount of flushing is
going to alter that. See e.g.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4212479
- this bug/feature is fairly old and here is suggested a more recent
workaround: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5026745
. But this too produces an out-of-memory exception with me (and I'm not
sure the users will have the absolutely latest Java version installed)
Is there a way for to get this to work so I can upload larger files
without running out of memory?
Else I'd start experiment with this package: HTTPClient :
http://www.innovation.ch/java/HTTPClient/
Perhaps this can handle larger files. but I already have everything
else working, so I'd be loathe to start to replace it all with a new
library if there was an acceptable way to implement uploads of larger
files just with the standard Java libraries.
Thomas Weidenfeller - 25 Oct 2006 14:17 GMT
> Is there a way for to get this to work so I can upload larger files
> without running out of memory?
http://jakarta.apache.org/commons/httpclient/
/Thomas

Signature
The comp.lang.java.gui FAQ:
http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
power_generator_set@yahoo.com - 25 Oct 2006 17:45 GMT
Winston,
My guess is that one way of solving this would be to throw memory at
your machine;
at least a few gigs. Then you need to modify the initial an maximum
heap
size when you launch your application:
java -Xms3000m -Xmx3000m -classpath rt.jar:someclasspath
YourApplication
If you don't have memory, then I would think about redesigning your
code
to see if you could do it with some object like a BufferedOutputStream
with
a FileWriter.
Morris
Generatormart, LLC.
http://www.generatormart.com
Diesel generator classified ads.
> OutputStream from a URLConnection produces an OutOfMemory Exception on
> large files
[quoted text clipped - 24 lines]
> library if there was an acceptable way to implement uploads of larger
> files just with the standard Java libraries.