Apache POI autoColumnWidth java.lang.OutOfMemoryError:超出了GC开销限制

Apache POI autoColumnWidth java.lang.OutOfMemoryError:超出了GC开销限制,java,out-of-memory,apache-poi,Java,Out Of Memory,Apache Poi,当对我的excel中包含1,28237行和20列的所有列调用autoColumnWidth函数时,我得到了OutOfMemoryError。我正在使用SXSSFapi并将-Xms2048m-Xmx4096m-XX:PermSize=1024m-XX:MaxPermSize=2048m分配给JVM,但仍然没有成功。 CPU利用率高达100%,我的系统RAM为6gb,在此过程中使用了高达5.8gb的内存,然后我得到OutOfMemoryError。有什么解决办法吗 有了这个,我可以想到一个解决方案,

当对我的excel中包含1,28237行和20列的所有列调用
autoColumnWidth
函数时,我得到了
OutOfMemoryError
。我正在使用
SXSSF
api并将
-Xms2048m-Xmx4096m-XX:PermSize=1024m-XX:MaxPermSize=2048m
分配给JVM,但仍然没有成功。 CPU利用率高达100%,我的系统RAM为6gb,在此过程中使用了高达5.8gb的内存,然后我得到
OutOfMemoryError
。有什么解决办法吗

有了这个,我可以想到一个解决方案,即为每列取一个变量,找到最大长度,最后在输入所有数据后将该列设置为相应的变量

还有其他解决办法吗

创建Excel的我的代码

public static void createVideodataExcelFile(String excelPath, int maxRiskArea){

    FileOutputStream fileOut = null;
    XSSFWorkbook workbook = new XSSFWorkbook();

    CellStyle style = workbook.createCellStyle();
    XSSFFont font = workbook.createFont();
    font.setColor(HSSFColor.BLACK.index);
    font.setBold(true);
    style.setFont(font);
    int cellCount = 0;

    try {
        File file = new File(excelPath);

        if(file.createNewFile()){
            SXSSFWorkbook wb = new SXSSFWorkbook(workbook); 
            wb.setCompressTempFiles(true);
            Sheet sheet = wb.createSheet("GLE Video Data");
            int rowCount = sheet.getPhysicalNumberOfRows();
            //System.out.println("Row Count ="+rowCount);
            Row row = sheet.createRow(rowCount);

            Cell cell1 = row.createCell(cellCount);
            createCell(cell1, row, "CourseContent name", style, cellCount);
            createCell(cell1, row, "lang", style, ++cellCount);
            createCell(cell1, row, "CourseTitle", style, ++cellCount);
            createCell(cell1, row, "Audience", style, ++cellCount);
            createCell(cell1, row, "ContentRegion", style, ++cellCount);
            createCell(cell1, row, "CourseTitle", style, ++cellCount);
            createCell(cell1, row, "DeprecatedId", style, ++cellCount);
            createCell(cell1, row, "GleCode", style, ++cellCount);
            createCell(cell1, row, "Guid", style, ++cellCount);
            createCell(cell1, row, "LearningFormat", style, ++cellCount);

            for(int i = 0 ; i < maxRiskArea ; ++i){
                createCell(cell1, row, "RiskArea", style, ++cellCount);
            }

            createCell(cell1, row, "SalesforceId", style, ++cellCount);
            createCell(cell1, row, "Setting", style, ++cellCount);
            createCell(cell1, row, "SmsCode", style, ++cellCount);
            createCell(cell1, row, "pageID", style, ++cellCount);
            createCell(cell1, row, "title", style, ++cellCount);
            createCell(cell1, row, "ID", style, ++cellCount);
            createCell(cell1, row, "media_src", style, ++cellCount);
            createCell(cell1, row, "cuePoint", style, ++cellCount);
            createCell(cell1, row, "character", style, ++cellCount);
            createCell(cell1, row, "line", style, ++cellCount);

            fileOut = new FileOutputStream(excelPath);
            wb.write(fileOut);
            System.out.println("Excel Of GLE Data Created.");
        }


    } catch (IOException e) {
        errorLog.error("Excel Read/Write Error ="+e.getMessage());
        throw new GLEException(e);
    }
    finally{
        try {
            if(fileOut!=null)
            fileOut.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            errorLog.error("Excel Read/Write Error ="+e.getMessage());
            throw new GLEException(e);      

        }
    }
}

private static void createCell(Cell cell , Row row, String name, CellStyle style, int cellCount ){
    cell = row.createCell(cellCount);
    cell.setCellStyle(style);
    cell.setCellValue(name);
}
公共静态void createVideodataExcelFile(字符串excelPath,int-maxRiskArea){
FileOutputStream fileOut=null;
XSSFWorkbook工作簿=新XSSFWorkbook();
CellStyle style=workbook.createCellStyle();
XSSFFont font=workbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setBold(true);
style.setFont(字体);
int-cellCount=0;
试一试{
文件文件=新文件(excelPath);
if(file.createNewFile()){
SXSSFWorkbook wb=新的SXSSFWorkbook(工作簿);
wb.setCompressTempFiles(true);
Sheet Sheet=wb.createSheet(“GLE视频数据”);
int rowCount=sheet.getPhysicalNumberOfRows();
//System.out.println(“行计数=”+rowCount);
Row Row=sheet.createRow(rowCount);
Cell cell1=row.createCell(cellCount);
createCell(单元格1,行,“CourseContent名称”,样式,单元格计数);
createCell(cell1,行,“lang”,样式,++cellCount);
createCell(单元格1,行,“CourseTile”,样式,++cellCount);
createCell(单元格1,行,“观众”,样式,++cellCount);
createCell(cell1,行,“ContentRegion”,样式,++cellCount);
createCell(单元格1,行,“CourseTile”,样式,++cellCount);
createCell(cell1,行,“DeprecatedId”,样式,++cellCount);
createCell(cell1,行,“GleCode”,样式,++cellCount);
createCell(cell1,行,“Guid”,样式,++cellCount);
createCell(cell1,行,“LearningFormat”,样式,++cellCount);
对于(int i=0;i
在调用autoSizeColumn(columnIndex)之前,尝试调用SXSSFSheet对象上的TrackAllColumnsFrautosizing(),它将是固定的

您使用SXSSF指定的分页大小是多少?页面越大,需要的内存越多,但大小也越精确…必须是默认值,我没有设置页面大小可以在创建SXSSFWorkbook的地方发布几行吗?请检查编辑的帖子为什么要从空的XSSFWorkbook创建SXSSFWorkbook?对于一个新文件,只需直接创建一个,很可能设置一个较小的窗口大小