java.lang.OutOfMemoryError:Java 堆空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1523233/
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
java.lang.OutOfMemoryError: Java heap space
提问by Sammy Larbi
while using Jruby, i get this message.
使用 Jruby 时,我收到此消息。
Complete Java stackTrace
java.lang.OutOfMemoryError: Java heap space
how to resolve ?
如何解决?
回答by Sammy Larbi
TLDR: jruby -J-Xmx1024m script_you_want_to_run.rb
域名注册地址: jruby -J-Xmx1024m script_you_want_to_run.rb
As others have mentioned, your program is trying to allocate more memory than the max size the JVM is allowed to allocate.
正如其他人所提到的,您的程序正在尝试分配比 JVM 允许分配的最大大小更多的内存。
Also as others have mentioned, you can configure Java to allow more memory allocation by telling it via the command line with the argument -Xmx1024m (as an example).
同样正如其他人所提到的,您可以通过使用参数 -Xmx1024m(作为示例)通过命令行告诉它来配置 Java 以允许更多的内存分配。
-Xmx is the argument for max memory, and the 1024m would be the memory size (the final m for megabytes). I think JRuby starts the JVM with max memory set to 512m already, so you'll probably want to be going higher than that.
-Xmx 是最大内存的参数,1024m 将是内存大小(最后的 m 表示兆字节)。我认为 JRuby 启动 JVM 时的最大内存已设置为 512m,因此您可能希望比这更高。
To send the arguments to the JVM from the command line using jruby, you'll need to add -J in front of the argument, so your command line will look like this:
要使用 jruby 从命令行将参数发送到 JVM,您需要在参数前添加 -J,因此您的命令行将如下所示:
jruby -J-Xmx1024m script_you_want_to_run.rb
I also agree with the memory leak sentiments: If you're not really dealing with a ton of objects with the expectation that you might be seeing this error, then you might want to look into the possibility that your program is having unintended side effects.
我也同意内存泄漏的观点:如果您并没有真正处理大量对象并期望您可能会看到此错误,那么您可能需要调查您的程序是否有可能出现意外副作用。
回答by M. Scott Ford
Setting JRUBY_OPTSis the solution that worked best for me. It was mentioned in Koray's answer.
设置JRUBY_OPTS是最适合我的解决方案。Koray的回答中提到了这一点。
set JRUBY_OPTS=-J-Xmx2g
回答by Pascal Thivent
You can tune the JVM heap size using the -Xmxand -XmsJVM options: -Xmxfor maximum heap size, and -Xmsfor initial heap size. E.g.:
您可以使用-Xmx和-XmsJVM 选项调整 JVM 堆大小:-Xmx用于最大堆大小和-Xms初始堆大小。例如:
java -Xms128m -Xmx256m BigApp
I usually use the same settings for the initial and max heap size.
我通常对初始和最大堆大小使用相同的设置。
In your case, it's really hard to say how to size the JVM without more information on what you're doing, when the problem occurs... Or maybe you just have a memory leak somewhere and increasing the heap size won't help, it will just make the problem occur later. In this case, the only solution is to fix the leak.
在你的情况下,如果没有更多关于你正在做什么的信息,当问题发生时,很难说如何调整 JVM 的大小......或者也许你只是在某处有内存泄漏并且增加堆大小无济于事,它只会使问题稍后发生。在这种情况下,唯一的解决办法是修复泄漏。
Last be not least, always keep in mind that the bigger the heap, the longer the major GC.
最后同样重要的是,请始终记住,堆越大,主要 GC 的时间就越长。
回答by Koray Güclü
check the JVM version of the jruby with this command
使用此命令检查 jruby 的 JVM 版本
jruby -v
If it returns this string it means that you are using an 32bit JVM therefore you can not set max heap size to >=2gb
如果它返回此字符串,则表示您使用的是 32 位 JVM,因此您不能将最大堆大小设置为 >=2gb
jruby 1.7.3 (1.9.3p385) 2013-02-21 dac429b on Java HotSpot(TM) Client VM 1.6.0_37-b06 [Windows 7-x86]
You need to set your JAVA_HOME to your 64bit version of Java. e.g.
您需要将 JAVA_HOME 设置为 64 位版本的 Java。例如
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_09
You will see following output if you rub jruby -v command again
如果再次执行 jruby -v 命令,您将看到以下输出
jruby 1.7.3 (1.9.3p385) 2013-02-21 dac429b on Java HotSpot(TM) 64-Bit Server VM 1.7.0_09-b05 [Windows 7-amd64]
You can see that you are now using 64bit version of JVM. After that you can set either jruby opts environment parameter as follows
您可以看到您现在使用的是 64 位版本的 JVM。之后,您可以按如下方式设置 jruby opts 环境参数
set JRUBY_OPTS=-J-Xmx2g
or you can run your program as follows
或者您可以按如下方式运行您的程序
jruby -J-Xmx2g <your ruby program>
回答by Droo
Increasing the heap dump size might only be a band-aid. You need to generate a heap dump by adding the proper proper argument:
增加堆转储大小可能只是一个创可贴。您需要通过添加适当的参数来生成堆转储:
java -XX:+HeapDumpOnOutOfMemoryError -mn256m -mx512
This will generate a file similar to java_*.hprof. You can then use a bevy of open source tools to analyze the heap dump. Java 1.6 ships with JHat, which is kind of buggy and doesn't analyze large heaps well. I use this awesome Eclipse plugin: Eclipse Memory Analyzer.
这将生成一个类似于 java_*.hprof 的文件。然后,您可以使用一组开源工具来分析堆转储。Java 1.6 附带了 JHat,它有点问题并且不能很好地分析大堆。我使用了这个很棒的 Eclipse 插件: Eclipse Memory Analyzer。
Once a report is generated from the heap dump, you can see which classes are taking up the most memory and you can use that as a starting point for debugging your code to find any memory leaks.
从堆转储生成报告后,您可以查看哪些类占用了最多的内存,您可以将其用作调试代码以查找任何内存泄漏的起点。
回答by akf
you can set your max heap to a larger size on the command line:
您可以在命令行上将最大堆设置为更大的大小:
java -Xmx512m MyClass
回答by cletus
Either you're leaking memory and you need to find why or you need to give Java more heap space:
要么你正在泄漏内存,你需要找出原因,要么你需要给 Java 更多的堆空间:
java -Xmx512m ...
回答by user479283
The message "Java heap space" indicates that new objects could not be allocated in the Java Heap. The error does not necessarily imply application memory leak. The problem could be as simple as a configuration issue, where the specified heap size (or the default size, if not specified) is insufficient for the application.
消息“Java 堆空间”表示无法在 Java 堆中分配新对象。该错误并不一定意味着应用程序内存泄漏。该问题可能与配置问题一样简单,其中指定的堆大小(或默认大小,如果未指定)对于应用程序来说是不够的。
Potential sources of this Error and solutions: http://www.blogsoncloud.com/jsp/techSols/java-lang-OutOfMemoryError-Java-heap-space.jsp
此错误的潜在来源和解决方案:http: //www.blogsoncloud.com/jsp/techSols/java-lang-OutOfMemoryError-Java-heap-space.jsp
回答by johannzhaojohann
I encounter a similar problem and I cannot find a satisfactory solution after googling for a few hours. If all you need is to set the Xms and Xmx params, here is what works for me:
我遇到了类似的问题,在谷歌搜索了几个小时后我找不到满意的解决方案。如果您只需要设置 Xms 和 Xmx 参数,那么这对我有用:
ps aux | grep java
ps辅助| 爪哇
Find out what is actually running by jruby. Kill it (kill -9 process_id). Re-run the command with the native Java options on the command line.
找出 jruby 实际运行的内容。杀死它(kill -9 process_id)。在命令行上使用本机 Java 选项重新运行该命令。
Hope it helps.
希望能帮助到你。
By the way, I am using puma server with Jruby on Rails 4.0, and if anyone knows a more elegant solution, please let me know. I have tried everything (after_use_hook, .rvmrc, .bashrc, etc. etc.)
顺便说一句,我正在使用带有 Jruby on Rails 4.0 的 puma 服务器,如果有人知道更优雅的解决方案,请告诉我。我已经尝试了一切(after_use_hook、.rvmrc、.bashrc 等)
Thanks.
谢谢。

