java - 内存不足错误:java heap

标签 java apache-poi out-of-memory

public class seventhma {

    XSSFSheet m_sheet;
    int m_iNbRows;
    int m_iCurrentRow = 0;
    private static final String JAVA_TOSTRING = "EEE MMM dd HH:mm:ss zzz yyyy";

    public seventhma(XSSFSheet sheet) {
        m_sheet = sheet;
        m_iNbRows = sheet.getPhysicalNumberOfRows();
    }

    /*
     * Returns the contents of an Excel row in the form of a String array.
     * 
     * @see com.ibm.ccd.common.parsing.Parser#splitLine()
     */
    public String[] splitLine() throws Exception {
        // if (m_iCurrentRow == m_iNbRows)
        // return null;

        XSSFRow row = m_sheet.getRow(m_iCurrentRow);
        if (row == null) {
            return null;
        } else {
            int cellIndex = 0;
            int noOfCells = row.getPhysicalNumberOfCells();
            String[] values = new String[noOfCells];
            short firstCellNum = row.getFirstCellNum();
            short lastCellNum = row.getLastCellNum();

            if (firstCellNum >= 0 && lastCellNum >= 0) {
                for (short iCurrent = firstCellNum; iCurrent < lastCellNum; iCurrent++) {
                    XSSFCell cell = (XSSFCell) row.getCell(iCurrent);
                    if (cell == null) {
                        values[iCurrent] = "";
                        cellIndex++;
                        continue;
                    } else {
                        switch (cell.getCellType()) {
                            case XSSFCell.CELL_TYPE_NUMERIC:
                                double value = cell.getNumericCellValue();
                                if (DateUtil.isCellDateFormatted(cell))

                                {
                                    if (DateUtil.isValidExcelDate(value)) {
                                        Date date = DateUtil.getJavaDate(value);
                                        SimpleDateFormat dateFormat = new SimpleDateFormat(JAVA_TOSTRING);
                                        values[iCurrent] = dateFormat.format(date);
                                    } else {
                                        // throw new
                                        // Exception("Invalid Date value found at row number "
                                        // +
                                        // row.getRowNum()+" and column number "+cell.getCellNum());
                                    }
                                } else {
                                    values[iCurrent] = value + "";
                                }
                                break;

                            case XSSFCell.CELL_TYPE_STRING:
                                values[iCurrent] = cell.getStringCellValue();
                                break;

                            case XSSFCell.CELL_TYPE_BLANK:
                                values[iCurrent] = null;
                                break;

                            default:
                                values[iCurrent] = null;
                        }
                    }
                }
            }
            m_iCurrentRow++;
            return values;
        }

    }

    public static void main(String args[]) {
        XSSFWorkbook workBook = null;
        File file = new File("E:\\Local\\Local2.xlsx");
        InputStream excelDocumentStream = null;
        try {
            excelDocumentStream = new FileInputStream(file);
            // POIFSFileSystem fsPOI = new POIFSFileSystem(new
            // BufferedInputStream(excelDocumentStream));
            BufferedInputStream bfs = new BufferedInputStream(excelDocumentStream);
            workBook = new XSSFWorkbook(bfs);
            seventhma parser = new seventhma(workBook.getSheetAt(0));
            String[] res = null;
            while ((res = parser.splitLine()) != null) {
                for (int i = 0; i < res.length; i++) {
                    System.out.println("[" + res[i] + "]" + "\t");

                }
                System.out.println(res.length);

            }
            bfs = null;
            excelDocumentStream.close();

        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }

    }
}

此程序导致 java 堆空间不足,并且当上传包含​​ 16 列的 Excel 工作表时,会出现 ArrayIndexOutOfBoundException
I将 eclipse 的内存增加到 -Xmx1600m 但这也不起作用。

最佳答案

您在 values 数组上收到 ArrayIndexOutOfBoundException,因为您使用 row.getPhysicalNumberOfCells() 来确定其大小。但是 row.getPhysicalNumberOfCells() 只会计算文件中实际填充的单元格。

例如,如果您创建一个 Excel 工作表并且仅填充 A、C 和 F 列,并且完全不接触其他单元格,row.getPhysicalNumberOfCells() 将返回 3。
但是您通过获取 row.getFirstCellNum() 和 row.getLastCellNum() 来迭代所有单元格。因此,一旦到达单元格 F,values[iCurrent] 肯定会超出范围。

关于内存不足问题: XSSF 使用大量内存。尝试将您的虚拟机添加到您的机器尽可能多的内存中。或者,如果您只是阅读文件,请尝试使用 eventmodel API 而不是 usermodel(想想 SAX 与 DOM)。 Apache POI Streaming vs. In memory
(来源:apache.org)

关于java - 内存不足错误:java heap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9597355/

相关文章:

java - 尝试在 mac 上获取 libGDX

java - Apache POI : Update cells in a named range

java - Intellij IDEA 中 Apache POI 的依赖关系存在问题。不一致的错误

java - Netbeans 默认选项,使用更多内存启动 jvm

java - Java 中的大矩阵

java - 是否可以将列表转换为 JCR 值?

javascript - 如何避免 firefox 确认消息 "to display this page firefox must send information that will repeat any action"?

java - 如何访问远程 Java 类中的 boolean 标志

java - NoClassDefFoundError org/apache/poi/ss/usermodel/Workbook

java - Android 内存不足错误