2015-10-07 57 views
0

每當我打電話給getExcelData()方法時,我都會收到java.lang.OutOfMemoryError: Java heap space error。正如你所看到的,我的代碼中也沒有任何循環。但仍面臨問題。獲取java.lang.OutOfMemoryError:以下代碼中的Java堆空間

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

而且,類

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; 
    } 
+0

請告訴我測試Data.xlsx的大小,什麼是當前的堆尺寸? –

+0

如何知道堆大小? –

+0

Excel的大小是5.35MB,會影響到我的代碼嗎? –

回答

0

如果您使用的是32位架構,可以增加堆到4GB。 64位體系結構可以更高。如果您選擇的值過高,則JVM將無法啓動並警告問題。

在任何情況下,開始與您的程序,例如,6 GB保留給堆,請使用以下-X選項:

java -Xmx6g myprogram 
相關問題