我们发现在使用JXL生成xls时,调用WritableWorkbook.close()将暂停系统。似乎没有释放资源。
Someone has also encountered this problem but no answer was given
我们测试了几个版本的JXL,但没有任何改变。问题出在哪里?
book.write();
//close() caused halting
book.close();
答案 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);