title: java.lang.OutOfMemoryError: Java heap space
body: <p>I've the error described below in the trace, when I try upload an 80,193KB FITS file for processing, in order to display select fields. Basically I have a mock web interface that allows a user to select up to 6 FITS files for uploading and processing. I don't get an error when I upload two [different] FITS files, roughly 54,574KB  each, at a go for processing. The fields are displayed/printed on console. However on uploading a single 80,193KB file I get the error below. How do I resolve it?</p>
  
  <p>I initially thought that the iteration was being computationally expensive but I suspect it occcurs on invoking the readHDU for the 80MB file:</p>
  
  <pre><code>while ((newBasicHDU = newFits.readHDU()) != null) { 
  </code></pre>
  
  <p>How do I resolve efficiently resolve it? I'm running the program on Windows 7. Cheers</p>
  
  <p>Trace:</p>
  
  <pre><code>SEVERE: Servlet.service() for servlet FitsFileProcessorServlet threw exception
  java.lang.OutOfMemoryError: Java heap space
      at java.lang.reflect.Array.multiNewArray(Native Method)
      at java.lang.reflect.Array.newInstance(Unknown Source)
      at nom.tam.util.ArrayFuncs.newInstance(ArrayFuncs.java:1028)
      at nom.tam.fits.ImageData.read(ImageData.java:258)
      at nom.tam.fits.Fits.readHDU(Fits.java:573)
      at controller.FITSFileProcessor.processFITSFile(FITSFileProcessor.java:79)
      at controller.FITSFileProcessor.doPost(FITSFileProcessor.java:53)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>/**
       * 
       * @param
       * @return
       */
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
  
          // Check that we have a file upload request
          boolean isMultipart = ServletFileUpload.isMultipartContent(request);
  
          if (isMultipart) {
  
              Fits newFits = new Fits();
              BasicHDU newBasicHDU = null;
              ServletFileUpload upload = new ServletFileUpload();                     // Create a new file upload handler
  
              // Parse the request
              try {
                  //List items = upload.parseRequest(request);                        // FileItem
                  FileItemIterator iter = upload.getItemIterator(request);
  
                  // iterate through the number of FITS FILES on the Server
                  while (iter.hasNext()) {
                      FileItemStream item = (FileItemStream) iter.next();
                      if (!item.isFormField()) {
                          this.processFITSFile(item, newFits,newBasicHDU );
                      }
                  }
              } catch (FileUploadException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }       
          }
      }
  
      /**
       * 
       * @param
       * @return
       */
      public void processFITSFile(FileItemStream item, Fits newFits, BasicHDU newBasicHDU) throws IOException {
  
          // Process the fits file
          if (!item.isFormField()) {
              String fileName = item.getName();                                       //name of the FITS File
              try {   
                  System.out.println("Fits File Fields Printout: " +  fileName);
                  InputStream fitsStream = item.openStream();                             
                  newFits = new Fits(fitsStream);
                  System.out.println( "number of hdu's if: " + newFits.getNumberOfHDUs());
  
                  while ((newBasicHDU = newFits.readHDU()) != null)  {                //line 76
                      System.out.println("Telescope Used: " + newBasicHDU.getTelescope());
                      System.out.println("Author: " + newBasicHDU.getAuthor());
                      System.out.println("Observer: " + newBasicHDU.getObserver() );
                      System.out.println("Origin: " + newBasicHDU.getOrigin() );
                      System.out.println("End of Printout for: \n" + fileName);
                      System.out.println();               
                  }
  
                  fitsStream.close();
  
              } catch (Exception e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }       
      }
  </code></pre>