title: Java Excel POI stops after multiple execution by quartz
body: <p>I'd like to have a couple of insights on this.</p>
  
  <p>I have a program that reads and writes from a database to an excel file. It's execution is based on a timer using Quartz api and triggered every tuesday of the week. The problem is, when i tested it by scheduling it to execute the job every hour, the program suddenly stops after a few executions inside the process of writing the excel file. Here is my write to excel code.</p>
  
  <pre><code>try {
          FileInputStream file = new FileInputStream(excelFile);
          POIFSFileSystem myFileSystem = new POIFSFileSystem(file);
          HSSFWorkbook workbook = new HSSFWorkbook(myFileSystem);
          HSSFSheet worksheet = workbook.getSheetAt(0);
          this.cellStyle00 = workbook.createCellStyle();
          HSSFDataFormat df = workbook.createDataFormat();
          this.cellStyle00.setDataFormat(df.getFormat("00"));
  
  for(int i = 0;i&lt;Access.size();i++){
           AccessorMethods SetGet = (AccessorMethods)
                      InstlibAccessor.get(i);
  
      HSSFRow row = worksheet.createRow(worksheet.getPhysicalNumberOfRows());
      HSSFCell cell = row.createCell(0);
  
      cell.setCellValue(new Double(SetGet.getOne()));
      cell.setCellStyle(cellStyle00);
  
  
    //other set value codes....
  
  }
  FileOutputStream fileOut = new FileOutputStream(fileName + ".xls");
  workbook.write(fileOut);
   fileOut.flush();
   fileOut.close(); 
  
   //catch statements follow
   //end
  </code></pre>
  
  <p>The command line output and netbeans output does not indicate any error like out of memory, etc etc.. The program does not end.. it just, stops.. Like the jvm is working on a infinite loop...  To shed more light on the topic, here is the brief flow of my program.</p>
  
  <ol>
  <li>The user executes the scheduler</li>
  <li>At the desired time the scheduler executes the program (the scheduler and "the program" are two different programs/jar files. The scheduler just calls the jar)</li>
  <li>The program starts by creating the excel file </li>
  <li>Then reads database 1. The database contains 80K rows</li>
  <li>For each row, if a certain condition is met, it reads database 2 and 3</li>
  <li>It then stores it inside an arraylist object 1000 at a time (Im trying to avoid any memory issues so i store it by batch)</li>
  <li>Then i write it by batch, 1000 at a time to excel (this is the part it stops)</li>
  <li>after it completes the reading and writing, it waits until the scheduler calls it again... If it reaches this step I'm a happy programmer =)</li>
  </ol>
  
  <p>Here are some observations I've found;</p>
  
  <ol>
  <li>The program usually stops at the 4th to 6th execution of the program (that's after 4 or 6 hours of running the scheduler program non-stop)</li>
  <li>It stops at a random point of write in excel like at the 34th thousand row or 24th or 15th etc etc etc...</li>
  <li>This error does not occur when i execute the program without a scheduler. I could manually execute it all day (I did, it was not very fun) without any errors. </li>
  <li>The output excel file displays 0bytes as size</li>
  <li>If for example i scheduled it to run every hour and for this hour it stopped. It will still run for the succeeding hours but will stop, and stop at a different point compared to the previous run. </li>
  </ol>
  
  <p>What could have caused this problem. A memory leak perhaps or something more simple?</p>
  
  <p><strong>Additional info</strong></p>
  
  <p>I implemented the Quartz scheduler by importing the class of the other program and run it as a job. here is the code for the trigger</p>
  
  <pre><code>JobDetail job = newJob(ExtractorSchedulerJobUtilTester.class)
              .withIdentity("job1", "group1")
              .build();
  
      CronTrigger trigger = newTrigger()
              .withIdentity("trigger1", "group1")
              .withSchedule(cronSchedule("0 0/2 * 1/1 * ? *"))
              .build();
  
      Date ft = sched.scheduleJob(job, trigger);
          sched.start();
  </code></pre>
  
  <p>and the job</p>
  
  <pre><code>public class ExtractorSchedulerJobUtilTester implements Job {
      public void execute(JobExecutionContext context)
          throws JobExecutionException {
      theProgram program= new theProgram();
  
      program.main();
  
      JobKey jobKey = context.getJobDetail().getKey();
      }
  }
  </code></pre>
  
  <p>Is it possible that;</p>
  
  <ol>
  <li>The application is maxing out my memory and crashes</li>
  <li>I'm only using one instance of "program" in my Quartz job which is initialized at the first run of the job and all subsequent executions of the job is referenced from that one instance thus maxing out memory.</li>
  <li>It's database (AS400) related (I doubt because it stops while it's writing the excel).</li>
  <li>The computer becomes too tired and decides to take a break. </li>
  </ol>
  
  <p><strong>UPDATE - 12/28/2012</strong></p>
  
  <p><strong>Happy New Year Guys/Gals!!</strong></p>
  
  <p>Sorry it took me some time to get back into this.. (Why bother wasting time on this when the world is gonna end on the 21st. It was bittersweet when it didn't)</p>
  
  <p>I profiled my program with netbeans profiler and got the following figures with the memory analyzer</p>
  
  <p><img src="https://i.stack.imgur.com/Yj5Kn.jpg" alt="memory dump"></p>
  
  <p>I noticed in the first graph that my program consumes around 75MB in heap size for every iteration (as indicated by the pink shade). Does this mean that the consumed memory of the program increases by 75mb per iteration? Which, after a few iterations, will consume so much memory that will effect the execution of the program. I'm currently trying to take a heap dump.. I'll post it as soon as I manage to get it running.</p>
  
  <p>Additional Info: I tried using the profiler with only the Quartz running (does not trigger anything) and the system usage is relatively low and the size does not increase for each iteration.</p>
  
  <p>I finally managed to get a heap dump. I took 2 dumps, first is when the first iteration took place and the second is the next iteration. I noticed that there is a big difference between the instances of two of my classess as indicated below</p>
  
  <p><img src="https://i.stack.imgur.com/JnLrn.jpg" alt="hash dump"> </p>
  
  <p>Thanks!</p>