测试poi的XSSFworkbook导出极限及内存溢出现象。
计划导出50000行,23列的xlsx文件
测试代码如下:
/**
* XSSFWorkbook导出
* @return
* @throws IOException
*/
@GetMapping("excel2")
public String exportExcel2() throws IOException {
System.out.println("开始");
long starttime = System.currentTimeMillis();
FileInputStream in = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\test\\wechatdetail_.xlsx"));
OutputStream out = new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\test\\wechatdetail_3.xlsx"));
XSSFWorkbook wo = new XSSFWorkbook(in);
XSSFSheet sheet = wo.getSheetAt(0);
for (int i = 0; i <50000 ; i++) {
XSSFRow row = sheet.createRow(i + 2);
for (int j = 0; j <23 ; j++) {
XSSFCell cell = row.createCell(j);
cell.setCellValue("value:"+j);
}
}
wo.write(out);
wo.close();
in.close();
out.close();
long endtime = System.currentTimeMillis();
System.out.println("结束耗时(ms):"+(endtime-starttime));
return "over";
}
导出结束,耗时39s
2020-11-08 15:46:41.379 INFO 14476 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 4 ms
开始
结束耗时(ms):38869
看jconsole,内存飙升到2.5g,导出后资源也没有被回收,内存持续维持在2.5g之间。
尝试导出10万看看:
2020-11-08 16:01:14.894 ERROR 11924 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/test01] threw exception [Handler dispatch failed; nested exception is java.lang.OutOfMemoryError: GC overhead limit exceeded] with root cause
导出失败了,所以以后还是尽量其他方式导出把。这个方式导出的数量级太少而且非常消耗内存和cpu资源。相比它的升级版SXSSFworkbook,差太远了。用它就是给自己挖坑了。