Tomcat – JVM/Tomcat crashes when to many uploads occur in parallel (Heap Space)

javaservletspringframeworktomcat

I have a Servlet application running on Tomcat 7. I set the following in setenv.sh

CATALINA_OPTS="
-server 
-Xms1G 
-Xmx5G
-XX:MaxPermSize=512m
-Dfile.encoding=UTF-8";

The server runs on Ubuntu 12.04LTS, has 6 Cores and 8GB of RAM.
My application is a Grails/Spring/Java app where users can upload images. Sometime it occurs that 3-5 users start uploading images at the same time. In these situations my Tomcat crashes because of the following error:

java.lang.OutOfMemoryError: Java heap space

I know that I have to increase the Xmx parameter of my Tomcat to prevent this problem. But seriously what is wrong in my case?

  1. Is there a best practice to handle file uploads like in my scenario?

  2. Can I prevent this problem some how? I mean when 5GB are not enough for 3-5 parallel uploads how much resources do I need for hundreds of parallel uploads?

  3. Is my app so bad or is it normal that file upload takes so much resources?

Here is the code that handles the upload in a Grails Controller:

CommonsMultipartFile file = (CommonsMultipartFile) request.getFile('image')
file.transferTo(new File("${storagePath}${fullFileName}"))

After the upload I do a lot of Image Processing. Is there a way to queue the process?

Best Answer

To be honest, 5G's of heap is already quite a lot. Show us the code path that handles the upload and provide some info about how large and many files are the users typically uploading.

This might also help you (if you are using commons fileupload with Spring's MultipartFile): https://stackoverflow.com/questions/1693810/how-can-i-avoid-outofmemoryerrors-when-using-commons-fileuploads-diskfileitem-t

Related Topic