2016-12-06 40 views
0

我正在維護一個swing應用程序。應用程序從訪問數據庫中獲取數據並將其填充到excel文件中。 直到100條記錄工作正常時填充超過200個記錄應用程序減慢,當我看到任務管理器內存消耗增加,直到700 mbs。前面的程序員正在使用正確關閉的Apache poi和FileOutputStrean。 我注意到能夠明白的地方是內存泄漏problem.My代碼段是POI和FileOutputPutStream的Swing應用程序性能問題

   final ArrayList<LinkedHashMap<String, Object>> arrlist_b1 = new Model() 
       .getAllowanceDetails1(
       currency, 
       type, 
       arrListYear, 
       arrList_survey_name, 
       arrList_country, 
       arrList_company); 

      final DateFormat dateFormat = new SimpleDateFormat(
       "yyyy-MM-dd_HH_mm_ss"); 
      final Date date = new Date(); 
      XSSFWorkbook workbook1 = new XSSFWorkbook(); 

      FileOutputStream out1 = null; 
      try { 
      out1 = new FileOutputStream(
       new File(
       filepath)); 
      } catch (FileNotFoundException e2) { 
      e2.printStackTrace(); 
      } 
      XSSFSheet spreadsheet1 = workbook1 
       .createSheet("Sheet Name"); 

      XSSFRow row1 = spreadsheet1 
       .createRow(0); 
      CellStyle style = workbook1 
       .createCellStyle(); 

      style = new Style() 
       .a_b_and_p_heading(workbook1); 
      int count_column1 = 0; 
      int count_row = 1; 

      pb.setMaximum(arrlist_b1 
       .size() - 1); 
      int total=arrlist_b1 
       .size(); 
      for (int a = 0; a < arrlist_b1 
       .size(); a++) { 
      pb.setValue(a); 

      download_status 
       .setText(a 
       + 1 
       + "/" 
       + total); 
      System.out 
       .println(a); 

      LinkedHashMap<String, Object> tmpData = (LinkedHashMap<String, Object>) arrlist_b1 
       .get(a); 
      Set<String> key = tmpData 
       .keySet(); 

      Iterator it = key 
       .iterator(); 
      String hmData, created_by; 
      XSSFRow row_tbl_heading = spreadsheet1 
       .createRow(count_row); 

      int count_column = 0; 
      while (it 
       .hasNext()) { 
       String hmKey = (String) it 
       .next(); 

       if (a == 0) { 
       spreadsheet1 
       .autoSizeColumn(count_column1); 
       XSSFCell cell_row = row1 
       .createCell(count_column1); 
       cell_row.setCellStyle(style); 
       hmKey = hmKey 
       .replaceAll(
        "_", 
        " ") 
       .toLowerCase(); 
       hmKey = StringUtils 
       .capitalize(hmKey); 

       cell_row.setCellValue(hmKey); 
       count_column1++; 

       } 
       DecimalFormat formatter = new DecimalFormat(
       "##,###"); 
       XSSFCell cell_row = row_tbl_heading 
       .createCell(count_column); 
       spreadsheet1 
       .autoSizeColumn(count_column); 

       CellStyle cellStyle = workbook1 
       .createCellStyle(); 
       cellStyle 
       .setDataFormat(workbook1 
        .getCreationHelper() 
        .createDataFormat() 
        .getFormat(
        "##,###")); 

       Object obj = tmpData 
       .get(hmKey); 
       if (obj instanceof Integer) { 

       if ((Integer) tmpData 
       .get(hmKey) == -1) { 
       cell_row.setCellValue(""); 
       } else { 
       cell_row.setCellValue((Integer) tmpData 
        .get(hmKey)); 

       } 

       } else if (obj instanceof Double) { 

       if (Double 
       .parseDouble(tmpData 
        .get(hmKey) 
        .toString()) == -1.0) { 
       cell_row.setCellValue(""); 
       } else { 

       int round_val = new Validation() 
        .count_digit(tmpData 
        .get(hmKey) 
        .toString()); 
       cell_row.setCellFormula(("ROUND(" 
        + Double.parseDouble(tmpData 
        .get(hmKey) 
        .toString()) 
        + ",-" 
        + round_val + ")")); 
       cell_row.setCellStyle(cellStyle); 

       } 

       } else { 
       hmData = (String) tmpData 
       .get(hmKey); 
       cell_row.setCellValue(hmData); 
       } 
       count_column++; 
      } 

      count_row++; 
      arrlist_b1.remove(a); 
      tmpData.remove(key); 
      it.remove(); 

      } 
      try { 
      workbook1 
       .write(out1); 

      } catch (Exception e1) { 
      System.out 
       .println("hello+" 
       + e1.getMessage()); 
      e1.printStackTrace(); 
      } 
      try { 
      out1.close(); 
      } catch (Exception e1) { 
      // TODO 
      // Auto-generated 
      // catch 
      // block 
      System.out 
       .println("hello+" 
       + e1.getMessage()); 
      e1.printStackTrace(); 
      } 

應用程序沒有經過400點的記錄作出迴應。 我使用Jprofiler跟蹤內存使用情況,其中char []和String消耗大量內存。 請讓我知道是否存在內存泄漏問題。提前致謝。 我附上jprofiler快照。 enter image description here

+0

''workbook1.write(out1)''後面也許'workbook1.close()'? – 2016-12-06 05:58:35

+0

它沒有工作,進展正常,直到100個記錄後,它減慢,幾分鐘後應用程序不響應。 –

+1

你可以發佈[mcve] – 2016-12-06 08:04:36

回答

0

獲取調試器並逐步循環。我的猜測是其中一個對象沒有清除每次迭代的內存。如果您找不到它,您可能需要一次加載它,像一次有200條記錄。

通常將數據預取到數組中並處理最快。 Java中的對象可能非常重。

相關問題