Excel POI 3.5 WorkBook Java Heap Space Exception?
I am using the latest POI 3.5 for Excel reading . I have Excel MS office 2007 installed and for that poi is providing XSSF for executing the data.
For 15000 lines of data it is executing properly, but when exceeding the limit till 30000 or 100000 or 200000, it is prone to a Java heap space Exception.
Code is below :
UATinput = new FileInputStream(UATFilePath); uatBufferedInputStream = new BufferedInputStream(UATinput); UATworkbook = new XSSFWorkbook(uatBufferedInputStream);
I am getting the Exception in the last line for Java heap size. I have increased the size using -Xms256m -Xmx1536m, but still for more data it is giving the Java heap space Exception.
Can anybody help me out for this Exception for the XSSFWorbook?
Instead of reading the entire file in memory try using the eventusermodel api
This is a very memory efficient way to read large files. It works on the principle of SAX parser (as opposed to DOM) in the sense that it will call callback methods when particular data structures are encountered. It might get a little tricky as it expects you to know the nitty-gritty of the underlying data
Here you can find a good tutorial on this topic
Hope this helps!
Its true guys, after using the UserEventModel, my performance was awesome. Please write to me, if you guys have any issues. email@example.com
If you use XSSFWorkbook, POI has to create a memory model containing your whole Excel file, thus a huge memory consumption. Maybe you could use the Event API which isn't as simple as the user API but allows lower memory consumption.
By the way you could also set a bigger value for -Xmx...
The other thing to watch in your own code is how many objects you are "new"ing. If you are creating a lot of objects as you read through cells, it could exhaust the heap as well. Make sure you are being careful with the number of objects you create.
As others have said, your best bet is to switch over the the Event API
One thing that'll make a small difference though is to not wrap your file in an input stream! XSSF will happily accept a File as the input, and that's a lower memory footprint than an InputStream. That's because POI needs random access to the contents, and with an input stream the only way to do that is to buffer the whole contents into memory. With a File, it can just seek around. Using a File rather than an InputStream will save you a little over the size of the file worth of memory.
If you can, you should pass a File. If memory is tight, write your InputStream to a file and use that!
Try this one: -Xms256m -Xmx512m.
You should really look forward to process the XML data grid behind XLSX technology. You will be liberated from the heap space problems. Here is the tutorial: Check both links below.
Some basic knowledge of parsing and the use of the SAX-XML project is required.
The JVM runs with fixed available memory. Once this memory is exceed you will receive "java.lang.OutOfMemoryError". The JVM tries to make an intelligent choice about the available memory at startup (see Java settings for details) but you can overwrite the default with the following settings.
To turn performance you can use certain parameters in the JVM. Xms1024m - Set the minimum available memory for the JVM to 1024 Megabyte Xmx1800m - Set the maximum available memory for the JVM to 1800 Megabyte. The Java application cannot use more heap memory then defined via this parameter.
If you start your Java program from the command line use for example the following setting: java -Xmx1024m YourProgram.
You can use SXSSF, A low-memory footprint SXSSF API built on top of XSSF. "http://poi.apache.org/spreadsheet/how-to.html#sxssf"