inputstream 占用内存吗_java - 关闭java InputStream后未释放内存[重复] - 堆栈内存溢出...

这个问题已经在这里有了答案:

在此代码段中,我使用Apache POI库加载大小为10MB的Excel文件。 这将消耗近2GB的内存。 遍历所有行之后,我终于调用了close方法。 但是,似乎GC不能释放此流和对象占用的空间。 而且仍在使用2GB + 400MB的内存。

有任何想法吗?

这是我的代码:

public List loadFile(File excelFile) throws IOException, InvalidFormatException {

List allMeters = new ArrayList<>();

InputStream inputStream = new FileInputStream(excelFile);

XSSFWorkbook workbook = new XSSFWorkbook(inputStream);

Sheet sheet1 = workbook.getSheetAt(0);

Iterator 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;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值