Excel POI 3.5工作簿Java堆空间异常?

时间:2021-03-07 20:24:29

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.

我正在使用最新的POI 3.5进行Excel阅读。我已经安装了Excel MS office 2007,为此poi提供了XSSF来执行数据。

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.

对于15000行数据,它正在正确执行,但是当超过3万或10万或20万时,它很容易出现Java堆空间异常。

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.

我在最后一行中得到了Java堆大小的异常。我已经使用-Xms256m -Xmx1536m增加了大小,但是对于更多的数据,它仍然给出了Java堆空间异常。

Can anybody help me out for this Exception for the XSSFWorbook?

有人能帮我解决XSSFWorbook的这个例外吗?

9 个解决方案

#1


6  

Instead of reading the entire file in memory try using the eventusermodel api

不要在内存中读取整个文件,请使用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

这是一种非常节省内存的读取大型文件的方法。它的工作原理是SAX解析器(与DOM相反),当遇到特定的数据结构时,它将调用回调方法。它可能会变得有点棘手,因为它期望您了解底层数据的真相

Here you can find a good tutorial on this topic

在这里你可以找到一个关于这个主题的很好的教程

Hope this helps!

希望这可以帮助!

#2


1  

Its true guys, after using the UserEventModel, my performance was awesome. Please write to me, if you guys have any issues. djeakandane@gmail.com

真的,在使用UserEventModel之后,我的表现棒极了。如果你们有什么问题,请写信给我。djeakandane@gmail.com

#3


0  

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.

如果您使用XSSFWorkbook, POI必须创建一个包含整个Excel文件的内存模型,因此会消耗大量内存。也许您可以使用事件API,它不像用户API那样简单,但允许更低的内存消耗。

By the way you could also set a bigger value for -Xmx...

顺便说一下,你也可以为-Xmx设置一个更大的值……

#4


0  

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.

在您自己的代码中要注意的另一件事是有多少对象是“新”的。如果您在遍历单元格时创建了许多对象,那么它也可能耗尽堆。确保您在创建对象的数量上非常小心。

#5


0  

As others have said, your best bet is to switch over the the Event API

正如其他人所说,最好的办法是切换事件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.

但是有一件事会有一点不同,那就是不要将文件包装在一个输入流中!XSSF很乐意接受一个文件作为输入,这比InputStream占用的内存要少。这是因为POI需要对内容进行随机访问,对于输入流,惟一的方法是将整个内容缓冲到内存中。有了文件,它就可以四处寻找。使用文件而不是InputStream将为您节省略多于文件大小的内存。

If you can, you should pass a File. If memory is tight, write your InputStream to a file and use that!

如果可以,您应该传递一个文件。如果内存不足,将InputStream写入文件并使用它!

#6


0  

Try this one: -Xms256m -Xmx512m.

试试这个:-Xms256m -Xmx512m。

#7


0  

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.

您应该非常期待在XLSX技术后面处理XML数据网格。您将从堆空间问题中解放出来。下面是教程:检查下面的两个链接。

http://poi.apache.org/spreadsheet/how-to.html

http://poi.apache.org/spreadsheet/how-to.html

http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java

http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java

Some basic knowledge of parsing and the use of the SAX-XML project is required.

需要一些解析和SAX-XML项目使用的基本知识。

#8


0  

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.

JVM使用固定的可用内存运行。一旦超过此内存,您将收到“java.lang.OutOfMemoryError”。JVM尝试在启动时对可用内存进行智能选择(详细信息请参阅Java设置),但您可以使用以下设置覆盖默认内存。

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.

要转换性能,可以在JVM中使用某些参数。Xms1024m—将JVM可用内存的最小值设置为1024兆x1800m—将JVM可用内存的最大值设置为1800兆。Java应用程序不能使用通过此参数定义的更多堆内存。

If you start your Java program from the command line use for example the following setting: java -Xmx1024m YourProgram.

如果您从命令行开始使用Java程序,例如以下设置:Java -Xmx1024m您的程序。

#9


0  

You can use SXSSF, A low-memory footprint SXSSF API built on top of XSSF. "http://poi.apache.org/spreadsheet/how-to.html#sxssf"

您可以使用SXSSF,这是一个构建在XSSF之上的低内存占用的SXSSF API。“http://poi.apache.org/spreadsheet/how-to.html # sxssf”

#1


6  

Instead of reading the entire file in memory try using the eventusermodel api

不要在内存中读取整个文件,请使用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

这是一种非常节省内存的读取大型文件的方法。它的工作原理是SAX解析器(与DOM相反),当遇到特定的数据结构时,它将调用回调方法。它可能会变得有点棘手,因为它期望您了解底层数据的真相

Here you can find a good tutorial on this topic

在这里你可以找到一个关于这个主题的很好的教程

Hope this helps!

希望这可以帮助!

#2


1  

Its true guys, after using the UserEventModel, my performance was awesome. Please write to me, if you guys have any issues. djeakandane@gmail.com

真的,在使用UserEventModel之后,我的表现棒极了。如果你们有什么问题,请写信给我。djeakandane@gmail.com

#3


0  

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.

如果您使用XSSFWorkbook, POI必须创建一个包含整个Excel文件的内存模型,因此会消耗大量内存。也许您可以使用事件API,它不像用户API那样简单,但允许更低的内存消耗。

By the way you could also set a bigger value for -Xmx...

顺便说一下,你也可以为-Xmx设置一个更大的值……

#4


0  

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.

在您自己的代码中要注意的另一件事是有多少对象是“新”的。如果您在遍历单元格时创建了许多对象,那么它也可能耗尽堆。确保您在创建对象的数量上非常小心。

#5


0  

As others have said, your best bet is to switch over the the Event API

正如其他人所说,最好的办法是切换事件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.

但是有一件事会有一点不同,那就是不要将文件包装在一个输入流中!XSSF很乐意接受一个文件作为输入,这比InputStream占用的内存要少。这是因为POI需要对内容进行随机访问,对于输入流,惟一的方法是将整个内容缓冲到内存中。有了文件,它就可以四处寻找。使用文件而不是InputStream将为您节省略多于文件大小的内存。

If you can, you should pass a File. If memory is tight, write your InputStream to a file and use that!

如果可以,您应该传递一个文件。如果内存不足,将InputStream写入文件并使用它!

#6


0  

Try this one: -Xms256m -Xmx512m.

试试这个:-Xms256m -Xmx512m。

#7


0  

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.

您应该非常期待在XLSX技术后面处理XML数据网格。您将从堆空间问题中解放出来。下面是教程:检查下面的两个链接。

http://poi.apache.org/spreadsheet/how-to.html

http://poi.apache.org/spreadsheet/how-to.html

http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java

http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java

Some basic knowledge of parsing and the use of the SAX-XML project is required.

需要一些解析和SAX-XML项目使用的基本知识。

#8


0  

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.

JVM使用固定的可用内存运行。一旦超过此内存,您将收到“java.lang.OutOfMemoryError”。JVM尝试在启动时对可用内存进行智能选择(详细信息请参阅Java设置),但您可以使用以下设置覆盖默认内存。

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.

要转换性能,可以在JVM中使用某些参数。Xms1024m—将JVM可用内存的最小值设置为1024兆x1800m—将JVM可用内存的最大值设置为1800兆。Java应用程序不能使用通过此参数定义的更多堆内存。

If you start your Java program from the command line use for example the following setting: java -Xmx1024m YourProgram.

如果您从命令行开始使用Java程序,例如以下设置:Java -Xmx1024m您的程序。

#9


0  

You can use SXSSF, A low-memory footprint SXSSF API built on top of XSSF. "http://poi.apache.org/spreadsheet/how-to.html#sxssf"

您可以使用SXSSF,这是一个构建在XSSF之上的低内存占用的SXSSF API。“http://poi.apache.org/spreadsheet/how-to.html # sxssf”