tuntuntut ufnmgjg
tuntuntut ufnmgjg

Reputation: 29

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. 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

     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

                 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

Upvotes: 0

Views: 2960

Answers (3)

Umesh Markande
Umesh Markande

Reputation: 309

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.

Upvotes: 0

YAS_Bangamuwage
YAS_Bangamuwage

Reputation: 194

That exception means that your java does not have enough memory to allocate in order to run this.

can use java -Xms -Xmx on the command line.

If you run the application using Eclipse ,

Right click on project -> Run As -> Run Configurations..-> Select Arguments tab -> In VM Arguments you can increase your JVM memory allocation

refer this documentation for learn more about vmoptions oracle - vmoptions

Upvotes: 0

cahen
cahen

Reputation: 16706

It's not a POI internal exception, you did run out of memory.

You can try to increase to -Xmx2048m or more, but consider a different approach, like the one suggested by @sibnick in the comments:

Processing large xlsx file in Java

Upvotes: 1

Related Questions