我們發現,當使用JXL生成xls時,調用WritableWorkbook.close()將暫停系統。似乎資源沒有被釋放。避免在JXL調用時暫停WritableWorkbook.close()
Someone has also encountered this problem but no answer was given
我們測試JXL的幾個版本,但沒有任何改變。問題在哪裏?
book.write();
//close() caused halting
book.close();
我們發現,當使用JXL生成xls時,調用WritableWorkbook.close()將暫停系統。似乎資源沒有被釋放。避免在JXL調用時暫停WritableWorkbook.close()
Someone has also encountered this problem but no answer was given
我們測試JXL的幾個版本,但沒有任何改變。問題在哪裏?
book.write();
//close() caused halting
book.close();
調查的源代碼之後,我們發現,調用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(這是理想的下大多數情況下)。WorkbookSettings workbookSettings = new WorkbookSettings();
workbookSettings.setGCDisabled(true);
readwb = Workbook.getWorkbook(file.getInputStream(), workbookSettings);
你能詳細說明一下這段代碼在做什麼嗎?雖然它可能(或可能不)解決問題,但目前它並未顯示如何或爲何。 – Holloway