简体   繁体   中英

outOfMemoryError:java heap

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();
        }

    }
}

This program gives java heap out of space and when excel sheet containing 16 columns is uploaded it gives ArrayIndexOutOfBoundException .
I had increased memory of eclipse upto -Xmx1600m but that also didnt work.

You get the ArrayIndexOutOfBoundException on the values array because you use the row.getPhysicalNumberOfCells() to determine its size. But row.getPhysicalNumberOfCells() will only count the cells that are actually filled in the file.

For example if you create an Excel sheet and only fill columns A, C and F and don't touch the other cells at all row.getPhysicalNumberOfCells() will return 3.
But you are iterating over all the cells by getting row.getFirstCellNum() and row.getLastCellNum() . So values[iCurrent] will sure be out of bounds once you reach cell F.

Regarding the OutOfMemory issue: XSSF uses a LOT of memory. Try pushing your VM to as much memory as is possible for your machine. Or if you are just reading the files then try to go with the eventmodel API instead of the usermodel (think SAX vs. DOM). Apache POI流媒体与内存
(source: apache.org )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM