[英]Out of memory when opening a spreadsheet with Apache POI
i am trying to read an excel that is of 50Mb in size and containg various sheets in it through poi in java but the issue is that when i try to creat the object i got the exception of which the stack trace I have posted below. 我试图读取大小为50Mb的Excel,并通过java中的poi在其中包含各种工作表,但是问题是,当我尝试创建对象时,得到了我在下面发布的堆栈跟踪的例外。 for out of memory space error i have configured this
对于内存不足错误,我已对此进行了配置
-Xmx1024m -Duser.timezone=GMT0 already
below is the snapshot in which i am trying to read the excel fist and then convrting it into byte array later on passing it as bytr stream where i detect it extension and it is of .xlsx type so the momnet i try to creat the object i got the below exception please advise how to overcome from this 下面是快照,在其中我尝试读取excel拳头,然后将其作为字节流传递到字节数组中,并在其中检测到扩展名,并且它是.xlsx类型,因此我尝试在momnet中创建对象i遇到以下异常,请告知如何克服此异常
String fileName = "C:\\abc\\xret.xlsx";
FileInputStream fis = new FileInputStream(fileName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] bFile = new byte[(int) fileName.length()];
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum);
}
} catch (IOException ex) {
ex.printStackTrace();
}
byte[] bytes = bos.toByteArray();
processExcelObjects( bytes);
later on below i am creating the objects further as shown below 稍后在下面,我将进一步创建对象,如下所示
byteArrayInputStream = new ByteArrayInputStream(bytes);
if (byteArrayInputStream.available() != -1 )
{
if (filename.lastIndexOf("."))).equalsIgnoreCase(".xlsx") )
{
XSSFWorkbook workbookXlsx = new XSSFWorkbook();
//**** got the exception ********//
workbookXlsx = new XSSFWorkbook(byteArrayInputStream); // ****** on this line I got the exception *********//
}
the exception that i got while creating the above workbookXlsx object is 我在创建上述workbookXlsx对象时遇到的异常是
org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62)
Caused by: java.lang.OutOfMemoryError: Java heap space
at org.apache.xmlbeans.impl.store.Cur$CurLoadContext.attr(Cur.java:3039)
at org.apache.xmlbeans.impl.store.Cur$CurLoadContext.attr(Cur.java:3060)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
please advise is it poi internal exception or heap space one 请告知它是poi内部异常还是堆空间之一
It's not a POI internal exception, you did run out of memory. 这不是 POI内部异常,您确实耗尽了内存。
You can try to increase to -Xmx2048m
or more, but consider a different approach, like the one suggested by @sibnick in the comments: 您可以尝试增加到
-Xmx2048m
或更多,但可以考虑使用另一种方法,例如@sibnick在评论中建议的方法:
That exception means that your java does not have enough memory to allocate in order to run this. 该异常意味着您的Java没有足够的内存来运行该内存。
can use java -Xms -Xmx on the command line. 可以在命令行上使用java -Xms -Xmx。
If you run the application using Eclipse , 如果您使用Eclipse运行该应用程序,
Right click on project -> Run As -> Run Configurations..-> Select Arguments tab -> In VM Arguments you can increase your JVM memory allocation 右键单击项目->运行方式->运行配置。.->选择参数选项卡->在VM参数中,您可以增加JVM内存分配
refer this documentation for learn more about vmoptions oracle - vmoptions 请参考本文档以了解有关vmoptions oracle的更多信息-vmoptions
I think there is some memory allocation problem you should use 我认为您应该使用一些内存分配问题
for (int readNum; (readNum = fis.read(bFile)) != -1;) {
bos.write(bFile, 0, readNum);
}
replace buf variable to bFile variable and check it. 将buf变量替换为bFile变量并进行检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.