In this snippet of code, I load an excel file of size 10MB using Apache POI library. This consumes almost 2GB of Memory. Having iterated over all of the rows, I finally call close method. However, it seems GC does not free up spaces consumed by this stream and object. And still using 2GB + 400MB of memory.
Any ideas?
Here is my Code:
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;
}