title: Why is my program getting slower and slower?
body: <p>I'm using the program to send data from database to the Excel file . 
  It works fine at the beginning and then becomes more and more slowly,finally it run out of the memory and the following error ocurrs: "java.lang.OutOfMemoryError: Java heap space...".</p>
  
  <p>The problem can be resolved by adding the jvm heap sapce.But the question is that it spends too much time to run out the program.  </p>
  
  <p>After several minutes,it finished a loop with 4 seconds which can be finished with 0.5 seconds at the beginning . I can't found a solution  to make it always run in a certain speed.</p>
  
  <p>Is it my code problem?</p>
  
  <p>Any clues on this?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public void addAnswerRow(List&lt;FinalUsers&gt; finalUsersList,WritableWorkbook book){
  
     if (finalUsersList.size() &gt;0 ) {
      try {
         WritableSheet  sheet = book.createSheet("Answer", 0);
         int colCount = 0;
         sheet.addCell(new Label(colCount++,0,"Number"));
         sheet.addCell(new Label(colCount++,0,"SchoolNumber"));
         sheet.addCell(new Label(colCount++,0,"District"));
         sheet.addCell(new Label(colCount++,0,"SchoolName"));
         sheet.setColumnView(1, 15);
         sheet.setColumnView(3, 25);
  
         List&lt;Elements&gt; elementsList = this.elementsManager.getObjectElementsByEduTypeAndQuestionnaireType(finalUsersList.get(0).getEducationType().getId(),     this.getQuestionnaireByFinalUsersType(finalUsersList.get(0).getFinalUsersType().getId()));
  
         Collections.sort(elementsList, new Comparator&lt;Elements&gt;(){
  
             public int compare(Elements o1, Elements o2) {
  
               for(int i=0; i&lt; ( o1.getItemNO().length()&gt;o2.getItemNO().length()?  o2.getItemNO().length(): o1.getItemNO().length());i++){
                    if (CommonFun.isNumberic(o1.getItemNO().substring(0, o1.getItemNO().length()&gt;3? 4: o1.getItemNO().length()-1)) &amp;&amp; !CommonFun.isNumberic(o2.getItemNO().substring(0, o2.getItemNO().length()&gt;3? 4: o2.getItemNO().length()-1))){
                   return 1;
                    }
                   if (!CommonFun.isNumberic(o1.getItemNO().substring(0, o1.getItemNO().length()&gt;3? 4: o1.getItemNO().length()-1)) &amp;&amp; CommonFun.isNumberic(o2.getItemNO().substring(0,o2.getItemNO().length()&gt;3? 4:o2.getItemNO().length()-1))){ 
                   return -1;
                   }  
                  if ( o1.getItemNO().charAt(i)!=o2.getItemNO().charAt(i) ){
  
                      return   o1.getItemNO().charAt(i)-o2.getItemNO().charAt(i);
                   }
             }
             return  o1.getItemNO().length()&gt; o2.getItemNO().length()? 1:-1;
         }});
  
         for (Elements elements : elementsList){
             sheet.addCell(new Label(colCount++,0,this.getTitlePre(finalUsersList.get(0).getFinalUsersType().getId(), finalUsersList.get(0).getEducationType().getId())+elements.getItemNO()+elements.getItem().getStem())); 
         }
  
         int sheetRowCount =1;
         int sheetColCount =0;
  
         for(FinalUsers finalUsers : finalUsersList){
  
            sheetColCount =0;
  
            sheet.addCell(new Label(sheetColCount++,sheetRowCount,String.valueOf(sheetRowCount)));
            sheet.addCell(new Label(sheetColCount++,sheetRowCount,finalUsers.getSchool().getSchoolNumber()));
            sheet.addCell(new Label(sheetColCount++,sheetRowCount,finalUsers.getSchool().getDistrict().getDistrictNumber().toString().trim()));
            sheet.addCell(new Label(sheetColCount++,sheetRowCount,finalUsers.getSchool().getName()));
  
            List&lt;AnswerLog&gt; answerLogList = this.answerLogManager.getAnswerLogByFinalUsers(finalUsers.getId());
  
  
            Map&lt;String,String&gt; answerMap = new HashMap&lt;String,String&gt;();
  
            for(AnswerLog answerLog :answerLogList ){
               if (answerLog.getOptionsId() != null)
               {
                  answerMap.put(answerLog.getElement().getItemNO(), this.getOptionsAnswer(answerLog.getOptionsId()));
               }else if (answerLog.getBlanks()!= null){
  
                  answerMap.put(answerLog.getElement().getItemNO(), answerLog.getBlanks());
               }else{
  
                  answerMap.put(answerLog.getElement().getItemNO(), answerLog.getSubjectiveItemContent());  
               }   
            }
            for (Elements elements : elementsList){
  
               sheet.addCell(new Label(sheetColCount++,sheetRowCount,null==answerMap.get(elements.getItemNO())?"0":answerMap.get(elements.getItemNO())));
  
            }
  
           sheetRowCount++; 
         }
  
         book.write();
         book.close();
  
       } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
       } catch (RowsExceededException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       } catch (WriteException e) {
  
        // TODO Auto-generated catch block
        e.printStackTrace();
     }
  </code></pre>
  
  <p>}
   }</p>