简体   繁体   English

Apache POI XSSF的堆空间错误

[英]Heap space error with Apache POI XSSF

I am trying to parse a large excel file(.xlsx) using Apache POI XSSF library. 我正在尝试使用Apache POI XSSF库解析大型excel文件(.xlsx)。 After 100,000 rows it throws heap space error. 100,000行后,它将引发堆空间错误。 I tried increasing the memory but it does not help. 我尝试增加内存,但无济于事。 Is there a workaround for this problem? 有解决此问题的方法吗? Or can someone suggest me a another library to parse large excel files. 或者有人可以建议我使用另一个库来解析大型excel文件。

Thanks! 谢谢!

Try the latest (stable!) Version from Apache POI. 尝试使用Apache POI的最新版本(稳定!)。

Alternatives might be smartXLS 替代品可能是smartXLS

When facing the most common OutOfMemoryError, namely the one "java.lang.OutOfMemoryError: Java heap space", some simple aspects must first be understood. 当面对最常见的OutOfMemoryError,即一个“ java.lang.OutOfMemoryError:Java堆空间”时,必须首先理解一些简单的方面。

Java applications are allowed to use a limited amount of memory. Java应用程序只能使用有限的内存。 This limit is specified during application startup. 此限制是在应用程序启动期间指定的。 To make things more complex, Java memory is separated different regions named heap space and permgen. 为了使事情变得更复杂,Java内存将不同的区域分开,分别称为堆空间和permgen。

The size of those regions is set during the Java Virtual Machine (JVM) launch by specifying parameters such as -Xmx and -XX:MaxPermSize. 这些区域的大小是在Java虚拟机(JVM)启动期间通过指定-Xmx和-XX:MaxPermSize等参数来设置的。 If you do not explicitly set the sizes, platform-specific defaults will be used. 如果未明确设置大小,将使用特定于平台的默认值。

So – the “[java.lang.OutOfMemoryError: Java heap space][1]” error will be triggered when you try to add more data into the heap space area, but there is not enough room for it. 因此,当您尝试向堆空间区域中添加更多数据但没有足够的空间时,将触发“ [[java.lang.OutOfMemoryError:Java堆空间] [1]””错误。

Based on this simple description, you have two options 根据此简单描述,您有两个选择

  • Give more room to the data structures 给数据结构更多空间
  • Reduce the size of the data structures used 减少使用的数据结构的大小

Giving more room is easy - just increase the heap size by changing the -Xmx parameter, similar to the following example giving your Java process 1G of heap to play with: 留出更多空间很容易-只需通过更改-Xmx参数来增加堆大小,类似于以下示例,为您的Java进程提供了1G的堆:

java -Xmx1024m com.mycompany.MyClass

Reducing the size of the data structures typically takes more effort, but this might be necessary in order to get rid of the underlying problems - giving more room can sometimes just mask the symptoms and postpone the inevitable. 减小数据结构的大小通常需要花费更多的精力,但是为了摆脱潜在的问题,这可能是必要的-提供更多的空间有时可以掩盖症状并推迟不可避免的事情。 For example, when facing a memory leak you are just postponing the time when all the memory is filled with leaking garbage. 例如,面对内存泄漏时,您只是在延迟所有内存充满泄漏的垃圾的时间。

In your case, reading the data in smaller batches and processing each batch at the time might be an option. 在您的情况下,可以选择以较小的批次读取数据并同时处理每个批次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM