在这段代码中,我使用 Apache POI 库加载了一个大小为 10MB 的 Excel 文件。这消耗了近 2GB 的内存。遍历所有行后,我终于调用了 close 方法。但是,GC 似乎没有释放此流和对象消耗的空间。并且仍然使用 2GB + 400MB 的内存。
有任何想法吗?
这是我的代码:
public List<Meter> loadFile(File excelFile) throws IOException, InvalidFormatException {
List<Meter> allMeters = new ArrayList<>();
InputStream inputStream = new FileInputStream(excelFile);
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet1 = workbook.getSheetAt(0);
Iterator<Row> rows_sheet1 = sheet1.iterator();
if (rows_sheet1.hasNext()) {
rows_sheet1.next(); //skip header
}
while (rows_sheet1.hasNext()) {
try {
Row currentRow = rows_sheet1.next();
Cell meterNoCell = currentRow.getCell(0);
Cell startPeriodCell = currentRow.getCell(1);
Cell endPeriodCell = currentRow.getCell(2);
Cell previousConsumption = currentRow.getCell(3);
Cell currentConsumption = currentRow.getCell(4);
Cell periodConsumptionCell = currentRow.getCell(5);
meterNoCell.setCellType(CellType.STRING);
startPeriodCell.setCellType(CellType.STRING);
endPeriodCell.setCellType(CellType.STRING);
//Reading values from above_defined cells and filling allMeters list (defined at the begining of the function).
//......
//Done
}
catch (Exception ex) {
Logger.getLogger(MetersList.class.getName()).log(Level.SEVERE, null, ex);
}
}
workbook.close();
inputStream.close();
return allMeters;
}
摇曳的蔷薇
倚天杖
相关分类