Servlet File Upload Memory Consumption

Posted by Scott on Stack Overflow See other posts from Stack Overflow or by Scott
Published on 2010-06-09T21:25:15Z Indexed on 2010/06/09 21:32 UTC
Read the original article Hit count: 186

Filed under:

Hi,

I'm using a servlet to do a multi fileupload (using apache commons fileupload to help). A portion of my code is posted below. My problem is that if I upload several files at once, the memory consumption of the app server jumps rather drastically. This is probably OK if it were only until the file upload is finished, but the app server seems to hang on to the memory and never return it to the OS. I'm worried that when I put this into production I'll end up getting an out of memory exception on the server. Any ideas on why this is happening? I'm thinking the server may have started a session and will give the memory back after it expires, but I'm not 100% positive.

if(ServletFileUpload.isMultipartContent(request)) {
   ServletFileUpload upload = new ServletFileUpload();
   FileItemIterator iter = upload.getItemIterator(request);
   while(iter.hasNext()) {
    FileItemStream license = iter.next();
    if(license.getFieldName().equals("upload_button") || license.getName().equals(""))
     continue;
    //DataInputStream stream = new DataInputStream(license.openStream());
    InputStream stream = license.openStream();
    ArrayList<Integer> byteArray = new ArrayList<Integer>();
    int tempByte;
    do {
     tempByte = stream.read();
     byteArray.add(tempByte);
    }while(tempByte != -1);
    stream.close();
    byteArray.remove(byteArray.size()-1);
    byte[] bytes = new byte[byteArray.size()];
    int i = 0;
    for(Integer tByte : byteArray) {
     bytes[i++] = tByte.byteValue();
    }

Thanks in advanced!!

© Stack Overflow or respective owner

Related posts about java