简体   繁体   中英

Getting java.lang.OutOfMemoryError: Java heap space in following code

Whenever I'm calling the getExcelData() method I'm getting java.lang.OutOfMemoryError: Java heap space error . As you can see I don't have any loop also in my code. But still facing problem.

ExcelLibrary exlb= new ExcelLibrary();
customerId+=exlb.getExcelData("ListCustomer", 1, 0);   

And, class

public class ExcelLibrary {
    String filepath;
    public ExcelLibrary()
    {       
        filepath="C:\\Users\\Use Me\\Desktop\\Test Data.xlsx";
    }
    public Sheet getSheet(String sheetName) throws InvalidFormatException, IOException
    {
        FileInputStream fis= new FileInputStream(filepath);
        Workbook wb=WorkbookFactory.create(fis);
        Sheet sh=wb.getSheet(sheetName);
        return sh;
    }
    public String getExcelData(String sheetName, int rowNo,int colNo) throws InvalidFormatException, IOException
    {
        String value="";
        Sheet sh=getSheet(sheetName);
        Row row=sh.getRow(rowNo);
        Cell cell=row.getCell(colNo,Row.CREATE_NULL_AS_BLANK);
        switch (cell.getCellType()) {

        case Cell.CELL_TYPE_NUMERIC:
            value = "NUMERIC value=" + cell.getNumericCellValue();
            break;

        case Cell.CELL_TYPE_STRING:
            value = "STRING value=" + cell.getStringCellValue();
            break;

        case Cell.CELL_TYPE_FORMULA:
            value = "FORMULA value=" + cell.getCellFormula();
            break;

        case Cell.CELL_TYPE_BLANK:
            Reporter.log("No values Exist for the cell",true);
            value="";
            break;

        case Cell.CELL_TYPE_ERROR:
            Reporter.log("Cell Value Format Error",true);
            value="error";
            break;

        default:
            break;
    }

    Reporter.log("CELL col=" + cell.getColumnIndex() + " VALUE=" + value, true);
    return value;
    }

If you're on a 32-bit architecture, you can increase the heap to 4GB. 64-bit architectures can go much higher. If you choose too high a value, the JVM will fail to launch and alert of the problem.

In any event, to start your program with, for example, 6 GB reserved for the heap, use the following -X option:

java -Xmx6g myprogram

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