Friday, February 15, 2013

Java Multipart upload OutOfMemory error - Java heap space

When you are uploading a large file under a low memory machine, you may get an OutOfMemory error - "java.lang.OutOfMemoryError: Java heap space".

When you are using a library that first saves the uploading file to memory, you will need ample amount of RAM to upload it.

For example, if you upload something that is 64MB, the machine will need 64MB of RAM. If two files of 64MB are being uploaded simultaneously, the machine will need 128MB of RAM.

We will present two solutions below:

  1. increase the heap size
  2. read and upload files in chunks


1. Increase the heap size - Temporally Solution

If you have a memory leak, this is just treating the symptom and not curing the disease. RAM will eventually run out.

To give more RAM to Tomcat, put the following in ~/.bashrc.
export CATALINA_OPTS="-Xms512m -Xmx512m"
If you are installing from the Ubuntu distribution, put the following inside /etc/init.d/tomcat7. Use echo to make sure you can
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms512m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+DisableExplicitGC"
Restart Tomcat.


2. Read and upload files in chunks

If you are not doing this already, please do.

No comments:

Post a Comment