Java 无法为对象堆保留足够的空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4401396/
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
Could not reserve enough space for object heap
提问by Narendra
I am getting the following exception repeatedly each time I try to run the program.
每次尝试运行该程序时,我都会反复收到以下异常。
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.
虚拟机初始化时出错
无法为对象堆保留足够的空间
无法创建Java虚拟机。
I tried to increase my virtual memory (page size) and RAM size, but to no avail.
我试图增加我的虚拟内存(页面大小)和 RAM 大小,但无济于事。
How can I eliminate this error?
我怎样才能消除这个错误?
采纳答案by Bozho
Run the JVM with -XX:MaxHeapSize=512m
(or any big number as you need) (or -Xmx512m
for short)
使用-XX:MaxHeapSize=512m
(或您需要的任何大数字)(或-Xmx512m
简称)运行 JVM
回答by daramasala
Assuming you have enough free memory and you setup you JVM arguments correctly, you might have a problem of memory fragmentation. Check Java maximum memory on Windows XP.
假设您有足够的空闲内存并且正确设置了 JVM 参数,您可能会遇到内存碎片问题。检查Windows XP 上的 Java 最大内存。
回答by euphoria83
Suppose your class is called Test
in package mypackage
. Run your code like this:
假设您的类Test
在 package 中被调用mypackage
。像这样运行你的代码:
java -Xmx1024m mypackage.Test
This will reserve 1024 MB of heap space for your code. If you want 512 MB, you can use:
这将为您的代码保留 1024 MB 的堆空间。如果你想要 512 MB,你可以使用:
java -Xmx512m mypackage.Test
Use little m in 1024m
, 512m
, etc
使用小米1024m
,512m
等
回答by Axl
Combined with -Xmx512M use -d64 to make sure you're running 64-bit VM. On a 64-bit machine I thought for sure I was running 64-bit virtual machine, but no. After installing 64-bit Java the -d64 option works and -Xmx allows much larger memory sizes.
结合 -Xmx512M 使用 -d64 以确保您运行的是 64 位 VM。在 64 位机器上,我确信我正在运行 64 位虚拟机,但没有。安装 64 位 Java 后,-d64 选项起作用,-Xmx 允许更大的内存大小。
java -d64 -Xmx512M mypackage.Test
回答by Ankam shyamsundar
I got the same error and resolved this by configuring it in the run.conf.bat
我遇到了同样的错误并通过在 run.conf.bat 中配置它来解决这个问题
Run the JVM with the configuring run.conf.bat in Jboss5x
在 Jboss5x 中配置 run.conf.bat 运行 JVM
If free memory is not available AS you are passing in the statement then please make changes in run.conf.bat
如果在您传入语句时没有可用内存,请在 run.conf.bat 中进行更改
set "JAVA_OPTS=-Xms512m -Xmx512m -XX:MaxPermSize=256m"
回答by djangofan
This can also be caused by setting something too large on a 32-bit HotSpot vm, for example:
这也可能是由于在 32 位 HotSpot 虚拟机上设置太大而导致的,例如:
-Xms1536m -Xmx1536m
where this might/wouldwork:
这可能/会起作用的地方:
-Xms1336m -Xmx1336m
回答by Harish
No need to do anything just chnage in POM file like below
无需做任何事情,只需在 POM 文件中更改如下所示
<configuration>
<maxmemory>1024M</maxmemory>
</configuration>
回答by codeDr
I recently faced this issue. I have 3 java applications that start with 1024m or 1280m heap size. Java is looking at the available space in swap, and if there is not enough memory available, the jvm exits.
我最近遇到了这个问题。我有 3 个以 1024m 或 1280m 堆大小开始的 Java 应用程序。Java 正在查看swap 中的可用空间,如果没有足够的可用内存,则jvm 退出。
To resolve the issue, I had to end several programs that had a large amount of virtual memory allocated.
为了解决这个问题,我不得不结束几个分配了大量虚拟内存的程序。
I was running on x86-64 linux with a 64-bit jvm.
我在带有 64 位 jvm 的 x86-64 linux 上运行。
回答by Jens Timmerman
I ran into this when using javac, and it doesn't seem to pick up on the command line options,
我在使用 javac 时遇到了这个问题,它似乎没有在命令行选项上找到,
-bash-3.2$ javac -Xmx256M HelloWorldApp.java
Error occurred during initialization of VM
Could not reserve enough space for object heap
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
so the solution here it so set _JAVA_OPTIONS
所以这里的解决方案是这样设置_JAVA_OPTIONS
-bash-3.2$ export _JAVA_OPTIONS="-Xmx256M"
-bash-3.2$ javac HelloWorldApp.java
Picked up _JAVA_OPTIONS: -Xmx256M
And this compiles fine.
这编译得很好。
This happens to me on machines with a lot of RAM, but with lower memory ulimits. Java decides to allocate a big heap because it detects the ram in the machine, but it's not allowed to allocate it because of ulimits.
这发生在我有大量 RAM 但内存 ulimits 较低的机器上。Java 决定分配一个大堆,因为它检测到机器中的 ram,但由于 ulimits 不允许分配它。
回答by Jon Schneider
Sometimes, this error indicates that physical memory and swap on the server actually are fully utilized!
有时,此错误表明服务器上的物理内存和交换实际上已被充分利用!
I was seeing this problem recently on a server running RedHat Enterprise Linux 5.7 with 48 GB of RAM. I found that even just running
我最近在运行 RedHat Enterprise Linux 5.7 和 48 GB RAM 的服务器上看到了这个问题。我发现即使只是跑步
java -version
caused the same error, which established that the problem was not specific to my application.
导致了同样的错误,这表明问题并非特定于我的应用程序。
Running
跑步
cat /proc/meminfo
reported that MemFree and SwapFree were both well under 1% of the MemTotal and SwapTotal values, respectively:
据报道,MemFree 和 SwapFree 分别远低于 MemTotal 和 SwapTotal 值的 1%:
MemTotal: 49300620 kB
MemFree: 146376 kB
...
SwapTotal: 4192956 kB
SwapFree: 1364 kB
Stopping a few other running applications on the machine brought the free memory figures up somewhat:
停止机器上其他一些正在运行的应用程序会使可用内存数字有所增加:
MemTotal: 49300620 kB
MemFree: 2908664 kB
...
SwapTotal: 4192956 kB
SwapFree: 1016052 kB
At this point, a new instance of Java would start up okay, and I was able to run my application.
此时,一个新的 Java 实例将正常启动,并且我能够运行我的应用程序。
(Obviously, for me, this was just a temporary solution; I still have an outstanding task to do a more thorough examination of the processes running on that machine to see if there's something that can be done to reduce the nominal memory utilization levels, without having to resort to stopping applications.)
(显然,对我来说,这只是一个临时解决方案;我仍然有一项出色的任务要对该机器上运行的进程进行更彻底的检查,以查看是否可以采取一些措施来降低标称内存利用率水平,而不需要不得不求助于停止应用程序。)