[英]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
. 每当我调用getExcelData()
方法时,都会得到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. 如果您使用的是32位架构,则可以将堆增加到4GB。 64-bit architectures can go much higher. 64位体系结构可能更高。 If you choose too high a value, the JVM will fail to launch and alert of the problem. 如果选择的值太高,JVM将无法启动并警告该问题。
In any event, to start your program with, for example, 6 GB reserved for the heap, use the following -X
option: 无论如何,要使用例如为堆保留的6 GB来启动程序,请使用以下-X
选项:
java -Xmx6g myprogram
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.