【发布时间】:2017-12-18 22:01:15
【问题描述】:
我正在处理一个大型 CSV (约 200 mb 的文本文件),我想将其转换为 excel 工作表,但工作簿变得非常消耗内存,以至于在处理过程中,Java抛出“超出 GC 开销限制”! 如果我正在生成虚拟引用,我已经检查了代码,但我认为不存在。
在我看来,来自 Apache - POI 的那些库调用可能会生成一些引用,从而使垃圾收集器如此忙碌。
我的问题是我是否可以像文本文件一样将工作簿逐块写入文件中,例如附加到文本文件而不将其放入内存中。有什么解决方案吗?还是我在这里遗漏了什么?
GC 在以下代码中抛出异常:
private void updateExcelWorkbook(String input, String fileName, Workbook workbook) {
try {
Sheet sheet = workbook.createSheet(fileName);
// Create a new font and alter it.
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 11);
font.setBold(true);
// Fonts are set into a style so create a new one to use.
CellStyle style = workbook.createCellStyle();
style.setFont(font);
Row row;
Cell cell;
String[] columns;
String[] lines = input.split("\n");
int colIndex;
int rowIndex = 1;
for (String line : lines) {
row = sheet.createRow(rowIndex++);
columns = line.split("\t");
colIndex = 0;
for (String column: columns) {
cell = row.createCell(colIndex++);
if (rowIndex == 1)
cell.setCellStyle(style);
cell.setCellValue(column);
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
【问题讨论】:
标签: java excel garbage-collection apache-poi large-data