alibaba/easyexcel

使用Web导出文件是,大对象无法被回收,导致OOM

Open

#3,533 建立於 2023年11月2日

在 GitHub 查看
 (4 留言) (0 反應) (0 負責人)Java (7,599 fork)batch import
help wanted

倉庫指標

Star
 (33,728 star)
PR 合併指標
 (30 天內沒有已合併 PR)

描述

问题描述,使用Web导出文件是,大对象无法被回收,导致OOM

快速开始常见问题

异常代码

 public void downloadDebugInfo(
            HttpServletResponse response,
            @RequestParam(name = "taskId") String taskId,
            @RequestParam(name = "versionId") String versionId) throws IOException {
        long startTime = System.currentTimeMillis();
        ExportResult exportResult = diffService.getDebugResult(Long.valueOf(taskId), Long.valueOf(versionId));
        long endTime = System.currentTimeMillis();
        long elapsedTime = endTime - startTime;
        log.info("导出计算运行时间:" + elapsedTime + "ms");
        // 接口测试 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode(exportResult.getFileName(), "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build()) {
            if (!CollectionUtils.isEmpty(exportResult.getNoteDebugInfoList())) {
                List<NoteSearchDataExpExcel> noteDebugInfoList = exportResult.getNoteDebugInfoList();
                WriteSheet gsbWriteSheet = EasyExcel.writerSheet(0, "Diff结果").head(NoteSearchDataExpExcel.class).build();
                excelWriter.write(noteDebugInfoList, gsbWriteSheet);
            } else {
                List<NoteFeedDataExpExcel> noteDebugInfoList = exportResult.getNoteFeedDataExpExcelList();
                WriteSheet gsbWriteSheet = EasyExcel.writerSheet(0, "Diff结果").head(NoteFeedDataExpExcel.class).build();
                excelWriter.write(noteDebugInfoList, gsbWriteSheet);
            }
        } catch (Exception e) {
            log.error("scoring export error. ", e);
            throw new DroomException("导出Diff异常");
        } finally {
            exportResult.clear();
            exportResult = null;
            System.gc();
        }
    }

问题描述

使用Web导出文件是,大对象无法被回收,导致OOM, 通过分析代码里面的 List noteDebugInfoList 无法被回收

貢獻者指南