java 命令行中的Java堆空间错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15112796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:30:41  来源:igfitidea点击:

Java Heap Space error from command line

javaapache-poiout-of-memoryheap-memory

提问by TechyHarry

I have tried to create a small utility which reads excel and sends email. I am using ApachePOI library for that. When I executed the code from eclipse, initially I got java.lang.OutOfMemoryError: GC overhead limit exceedederror. Then I added -Xms1024min the VM Arguments of eclipse and program worked fine in eclipse.

我试图创建一个读取 excel 并发送电子邮件的小实用程序。我为此使用了 ApachePOI 库。当我从 Eclipse 执行代码时,最初出现java.lang.OutOfMemoryError: GC overhead limit exceeded错误。然后我添加-Xms1024m了 eclipse 的 VM Arguments 并且程序在 eclipse 中运行良好。

Then I exported the set of java programs and libraries into RunnableJar and bundled the dependent libraries.

然后我将这组java程序和库导出到RunnableJar并捆绑了依赖库。

Now from command line when i execute the command

现在当我执行命令时从命令行

java -Xms1024m -jar AutomateProcesses.jar

I am still getting the same error. I am not able to figure out the issue. could someone please help in this regard?

我仍然遇到同样的错误。我无法弄清楚这个问题。有人可以在这方面提供帮助吗?

采纳答案by hack_on

You could try

你可以试试

java -Xms512m -Xmx2048 -jar AutomateProcesses.jar

If this is just whats going on in this VM which is what I expect. If the a library in the java process is then spawning a separate process then the library may need some other options to configure it.

如果这正是我所期望的 VM 中发生的事情。如果 java 进程中的库生成了一个单独的进程,则该库可能需要一些其他选项来配置它。



EDIT:

编辑:

An answer to this questionpoints out that this error is because the GC is spending too much time trying to recover memory and not getting it. I am not very familiar with Appache POI but if it is talking to Excel from java then it is probably using COM calls into the Excel DLLs. It may be that there is a bug in the library or the way that you use it, leading to objects being locked out of garbage collection and thus the GC works very hard with little progress.

这个问题的答案指出,这个错误是因为 GC 花费了太多时间试图恢复内存而没有得到它。我对 Appache POI 不是很熟悉,但如果它是从 Java 与 Excel 对话,那么它可能使用 COM 调用到 Excel DLL。可能是库或您使用它的方式存在错误,导致对象被锁定在垃圾收集之外,因此 GC 工作非常努力,但进展甚微。

Can you try to isolate the code that has this problem into a smaller test case?

您能否尝试将出现此问题的代码隔离到一个较小的测试用例中?

This posthad a similar problem to such an extent that they re-wrote the way they were dealing with cells in Excel so as to avoid creating a large number of cellStyle objects.

这篇文章有一个类似的问题,以至于他们重写了他们在 Excel 中处理单元格的方式,以避免创建大量的 cellStyle 对象。

In a similar vein, this persongave up and wrote their data to CSV format.

同样,此人放弃并将他们的数据写入 CSV 格式。

回答by das Keks

To set the maximum heap size and allow the Java VM to allocate more memory, you have to use the command -Xmx1024M(or -Xmx1G).

要设置最大堆大小并允许 Java VM 分配更多内存,您必须使用命令-Xmx1024M(或-Xmx1G)。

-Xms sets the minimum respectively the initial heap size.

-Xms 分别设置最小初始堆大小。

回答by P-H

java.lang.OutOfMemoryError: GC overhead limit exceededis a symptom of a problem with the JVM garbage collection (too much time spent in GC). The error is actually preventing the whole JVM to go in hang state so you can gather some extra stats.

java.lang.OutOfMemoryError:超出 GC 开销限制是 JVM 垃圾收集问题的症状(在 GC 上花费了太多时间)。该错误实际上阻止了整个 JVM 进入挂起状态,因此您可以收集一些额外的统计信息。

Reading an Excel Sheet can be memory intensive depending how large the file is which will add pressure point to the GC process.

读取 Excel 工作表可能会占用大量内存,具体取决于文件的大小,这会给 GC 过程增加压力点。

Enabling and analyzing verbose:gc will definitely help. You could also generate a JVM Heap Dumpand determine where that memory is retained in your Java utility program. You will have to either adjust the Java heap size and / or resolve any faulty memory retention.

启用和分析 verbose:gc 肯定会有所帮助。您还可以生成JVM 堆转储并确定该内存在 Java 实用程序中的保留位置。您将不得不调整 Java 堆大小和/或解决任何错误的内存保留问题。