JXL调用WritableWorkbook.close()时避免停止

时间:2014-01-23 07:19:36

标签: java resources jxl

我们发现在使用JXL生成xls时,调用WritableWorkbook.close()将暂停系统。似乎没有释放资源。

Someone has also encountered this problem but no answer was given

我们测试了几个版本的JXL,但没有任何改变。问题出在哪里?

book.write();
//close() caused halting
book.close(); 

2 个答案:

答案 0 :(得分:1)

在调查源代码后,我们发现在调用WritableWorkbook.close()时,默认行为每次调用System.gc()!

void close(boolean cs) throws IOException, JxlWriteException {
    CompoundFile cf = new CompoundFile(data, 
                                   data.getPosition(), 
                                   outputStream, 
                                   readCompoundFile);
    cf.write();

    outputStream.flush();
    data.close();

    if (cs) {
      outputStream.close();
    }

    // Cleanup the memory a bit
    data = null;

    if (!workbookSettings.getGCDisabled()) {
      System.gc();
    }
}

有两种方法可以通过系统属性来避免这种情况

  • jxl.nogc=true,禁用JXL的gc(将-Djxl.nogc=true添加到jvm参数)
  • XX:+DisableExplicitGC,禁用所有显式GC(在大多数情况下这是理想的)。

答案 1 :(得分:0)

WorkbookSettings workbookSettings = new WorkbookSettings();
        workbookSettings.setGCDisabled(true);
        readwb = Workbook.getWorkbook(file.getInputStream(), workbookSettings);