Java Heap Space error from command line

拈花ヽ惹草 提交于 2019-12-24 12:34:18

问题


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 exceeded error. Then I added -Xms1024m in the VM Arguments of eclipse and program worked fine in eclipse.

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

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?


回答1:


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.


EDIT:

An answer to this question points 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.

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

This post had 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.

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




回答2:


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

-Xms sets the minimum respectively the initial heap size.




回答3:


java.lang.OutOfMemoryError: GC overhead limit exceeded is 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.

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

Enabling and analyzing verbose:gc will definitely help. You could also generate a JVM Heap Dump and 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.



来源:https://stackoverflow.com/questions/15112796/java-heap-space-error-from-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!