OutOfMemory errors during Struts file upload

17 January 2007 » Java, Web design, Web development

I ran into an issue today that I couldn’t find an existing answer to. Hopefully this solution helps anyone else who’s still using Struts 1.x and running into errors when uploading large files through a Web form. It’s a setting for the Apache Jakarta Commons FileUpload library that Struts uses rather than internal to Struts itself.

Occasionally during the submission of forms I’d see errors in the console which began with the following stack trace.

java.lang.OutOfMemoryError at
java.io.ByteArrayOutputStream.<init>
(ByteArrayOutputStream.java(Compiled
Code)) at org.apache.commons.fileupload.
DeferredFileOutputStream

You might think that it makes sense to increase the buffer size to fix a problem like that. You might even try increasing any of the other Struts RequestProcessor settings.

In retrospect it makes sense that you don’t tell something to allocate more of a resource that it can’t already get enough of, but it took me a cup of coffee or two to realize I had to reduce the buffer size, not increase it.

Anyway, lesson learned.

bufferSize – The size (in bytes) of the input buffer used when processing file uploads.” Lowering this may affect performance, the documentation says, but that’s a better option than having it not perform at all.

memFileSize – The maximum size (in bytes) of a file whose contents will be retained in memory after uploading. Files larger than this threshold will be written to some alternative storage medium, typically a hard disk. Can be expressed as a number followed by a “K”, “M”, or “G”, which are interpreted to mean kilobytes, megabytes, or gigabytes, respectively.”

Default <controller> element in struts-config.xml:

<controller bufferSize="4096" memFileSize="256K"/>

What I tweaked to eliminate the error.

<controller bufferSize="1024" memFileSize="128K"/>

Your mileage may vary.

  

One Response to 'OutOfMemory errors during Struts file upload'

Subscribe to comments with RSS

  1. jklp said,

    11 December 2007 at 12:50 am

    Thanks was racking my brains out for ages with this!

Leave a Reply