322

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.

I tried to increase my virtual memory (page size) and RAM size, but to no avail.

How can I eliminate this error?

trincot
  • 317,000
  • 35
  • 244
  • 286
Narendra
  • 5,635
  • 10
  • 42
  • 54
  • possible duplicate of [Java Refuses to Start - Could not reserve enough space for object heap](http://stackoverflow.com/questions/1058471/java-refuses-to-start-could-not-reserve-enough-space-for-object-heap) – daveb Dec 09 '10 at 17:48
  • 2
    I also get this error when using jdk/jre 1.6 on my virtual machine, tried to change config values given in comments but this was not helping, after updating to jdk 1.7 the error is gone and larger Xmx parameters applied. Seems there are many changes with heap usage since java 1.6. – Alexander.Iljushkin Jan 29 '15 at 13:17
  • I got similar error on Ubuntu 20.04 after upgrading from Ubuntu 18.04. I was using OpenJDK version 12 earlier and even though OpenJDK version 12 was available for Ubuntu 20.04, it didn't work for a complex Java app. Upgrading to more modern OpenJDK version (`sudo apt install openjdk-17-jdk openjdk-12-jdk-`, the dash at the end of `openjdk-12-jdk` tells system to uninstall that package while installing another) things started to work as expected again. – Mikko Rantalainen Sep 23 '22 at 08:15
  • The error messages I got were `Not enough space available on the backing filesystem to hold the current max` and `Forcefully lowering max Java heap size to 0M`. The interesting thing was that I had `-Xmx3000m` and system had 16 GB free RAM and filesystem had ~60 GB free space. And `java` forcing the max heap size to zero is obviously not going to work so I guess that's some weird bug in JVM. – Mikko Rantalainen Sep 23 '22 at 08:21

27 Answers27

238

Run the JVM with -XX:MaxHeapSize=512m (or any big number as you need) (or -Xmx512m for short)

Hearen
  • 7,420
  • 4
  • 53
  • 63
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 42
    Or the shorter, -mx256m or -mx512m ;) – Peter Lawrey Dec 09 '10 at 19:55
  • 8
    Is -mx the same as -Xmx and -XX:MaxHeapSize? – Erty Seidohl Aug 20 '12 at 18:57
  • 1
    Good question. I don't remember... :) Probably they are the same, as Xmx is also about the maximum heap size – Bozho Aug 22 '12 at 09:01
  • 1
    Xmx256M -XX:MaxPermSize=512m solved my issue on installing Lift 2.5 in Windows 8.1 – Haider Ali Wajihi Jul 03 '14 at 07:46
  • 24
    Thanks.. turns out too big a number could also be an issue and can give the same error! – Dinesh Rajan Sep 03 '14 at 23:00
  • 24
    Anyone found a solution that actually work 100% of the time? This solution solves the problem temporarily but then suddenly it comes back. I've got 16GB ram and I'm tired of this sh*t. Everything was better in the old days :[ – Nilzor Oct 01 '14 at 13:06
  • 6
    Doesn't work for me on Windows 8 with x86 or x64 Java. – AndrewSmiley Jul 27 '15 at 13:13
  • 6
    -mx is the same as -Xmx in Java 7 as above. -mx is not available on Java 6 and below. -X indicates it is experimental, and after so many years eventually HotSpot team decided it is no longer experimental. – Daniel Baktiar Jan 17 '17 at 01:20
  • You should edit this option in the following file c:\Users\%USERNAME%\.gradle\gradle.properties, if you're using gradle. – Artem Mostyaev Dec 29 '17 at 15:18
  • If you really want to increase the heap size, install 64 bit java and run with -d64 – Kumar Feb 12 '18 at 02:36
  • https://gatling.io/docs/2.3/quickstart/ Please check warning section on official page. Gatling google group answer: https://groups.google.com/forum/#!topic/gatling/CJMHlbaof40 – Ananth Jun 10 '18 at 01:21
  • Can somebody explain to me why my pc try to initialize with the max memory? I try java -Xms1G -Xmx2G, but doesn't work when i don't have 2G extra, but 1G+ extra – Danielr Feb 22 '20 at 03:16
140

This can also be caused by setting something too large on a 32-bit HotSpot vm, for example:

-Xms1536m -Xmx1536m

where this might/would work:

-Xms1336m -Xmx1336m
djangofan
  • 28,471
  • 61
  • 196
  • 289
  • 5
    I forgot to mention that this problem would have to occur while launching in a 32-bit command shell. A 64-bit command shell may not have this problem. – djangofan Sep 10 '12 at 03:37
  • For me it failed with default value and with a value too big so make sure you try multiple values. Thanks for the answer! – Trax Aug 08 '19 at 16:17
  • 1
    Good tip that it fails when its is set to a value higher than what is available. For me it failed because it was set to 5G and its was running on a tiny instance, reducing to 512 seems to work better – PJUK Jan 15 '21 at 14:26
62

here is how to fix it:

  • Go to Start->Control Panel->System->Advanced(tab)->Environment Variables->System

    Variables->New: Variable name: _JAVA_OPTIONS Variable value: -Xmx512M Variable name: Path
    Variable value: %PATH%;C:\Program Files\Java\jre6\bin;F:\JDK\bin;

Change this to your appropriate path.

Hearen
  • 7,420
  • 4
  • 53
  • 63
Mohamed Adel
  • 1,980
  • 17
  • 23
52

I ran into this when using javac, and it doesn't seem to pick up on the command line options,

-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

-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.

Jens Timmerman
  • 9,316
  • 1
  • 42
  • 48
  • 2
    +1 for pointing out _JAVA_OPTIONS — in my case java is called from somewhere deep within a shell script to which I have no write access, so this option is preferable. – gerrit May 15 '15 at 09:50
  • Same here. I'm using university computers so I'm not an admin (can't change environmental variables) and the command line option was not working at all. Thanks so much! – Kimbluey Feb 11 '17 at 03:23
  • @Kimbluey the export _JAVA_OPTIONS command is actually in fact exactly what changing the environment variables is. You should generaly be able to set the environment variables in your own session when you call an executable, even if you can't change the global variables. – Jens Timmerman Jul 13 '23 at 15:54
45

32-bit Java requires contiguous free space in memory to run. If you specify a large heap size, there may not be so much contiguous free space in memory even if you have much more free space available than necessary.

Installing a 64-bit version of Java helps in these cases, the contiguous memory requirements only applies to 32-bit Java.

Hearen
  • 7,420
  • 4
  • 53
  • 63
JohnEye
  • 6,436
  • 4
  • 41
  • 67
  • 2
    I think this is the best answer in case you keep getting the error after using the -Xmx[bignumber]m option. Helped me running Apache jMeter properly. – RuudvK Apr 10 '18 at 13:18
  • I created a brand new project, with no code other than the auto generated main activity, and it produced this error – behelit Sep 11 '18 at 00:47
  • 1
    I second @RuudvK. Installing 64-bit allowed me to increase the max memory while clearing the memory allocation error. This should be the accepted answer. – J Weezy Jan 02 '19 at 23:26
  • Yes, just installing the 64 bit JDK made the error go away. – Glenn Grabbe Jan 05 '23 at 17:21
33

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.

java -d64 -Xmx512M mypackage.Test
Axl
  • 8,272
  • 2
  • 26
  • 18
  • 4
    This answer should be at the top. Spent two months battleing this issue, just to realise that installing 64 bit Java solved the issue (the -d64 option wasn't necessary in my case) – Nilzor Oct 27 '14 at 14:16
  • Picked up _JAVA_OPTIONS: -d64 -Xmx1024M Unrecognized option: -d64 The JVM could not be started. The maximum heap size (-Xmx) might be too large or an antivirus or firewall tool could block the execution. – Alexander Jun 15 '18 at 09:30
21

Open gradle.properties file in android folder.

Replace this line:

org.gradle.jvmargs=-Xmx1536M

with:

org.gradle.jvmargs=-Xmx512m

Explanation: Max limit from Gradle document:

If the requested build environment does not specify a maximum heap size, the Daemon will use up to 512MB of heap.

live-love
  • 48,840
  • 22
  • 240
  • 204
  • Perfecto! This solution works for me on below config : Version: 1.55.2 (system setup) Commit: 3c4e3df9e89829dce27b7b5c24508306b151f30d Date: 2021-04-13T09:35:57.887Z Electron: 11.3.0 Chrome: 87.0.4280.141 Node.js: 12.18.3 V8: 8.7.220.31-electron.0 OS: Windows_NT x64 10.0.19042 – Narendra Chandratre May 26 '21 at 11:21
  • 1
    I'm confused, the replaced size of 512m is actually lower than the original 1536M, right? How is a smaller size going to increase the max limit? It does not work for me, either. – Wouter van Koppen Jul 14 '21 at 09:44
14

I got the same error and resolved this by configuring it in the run.conf.bat

Run the JVM with the configuring run.conf.bat in Jboss5x

If free memory is not available AS you are passing in the statement then please make changes in run.conf.bat

set "JAVA_OPTS=-Xms512m -Xmx512m -XX:MaxPermSize=256m"
eduherminio
  • 1,514
  • 1
  • 15
  • 31
8

I had similar issues. I had installed 32 bit version of Java on a 64 bit machine.

By uninstalling that version and installing 64 bit version of Java. I was able to resolve the issue.

Hearen
  • 7,420
  • 4
  • 53
  • 63
Manu Sharma
  • 671
  • 7
  • 11
7

I know there are a lot of answers here already, but none of them helped me. In the end I opened the file /etc/elasticsearch/jvm.options and changed:

-Xms2G
-Xmx2G

to

-Xms256M
-Xmx256M

That solved it for me. Hopefully this helps someone else here.

Hearen
  • 7,420
  • 4
  • 53
  • 63
kramer65
  • 50,427
  • 120
  • 308
  • 488
6

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

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:

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.

(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.)

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
  • Same here, `java -version` failing, even though top showed some free still :| (also said Error occurred during initialization of VM Could not reserve enough space for the card marking array sometimes). Fix seemed to be to run top, look for which processes were using the most RAM (VIRT column), kill them [postgres, appdynamics for me] :| – rogerdpack Oct 24 '16 at 22:37
  • Could this be due to memory fragmentation? A reply above mentions contiguous free space. – Rick Moritz Jun 03 '20 at 12:04
6

Error :

For the error, "error occurred during initialization of vm could not reserve enough space for object heap jboss"

Root Cause :

  • Improper/insufficient memory allocation to our JVM as mentioned below.

  • e.g. JAVA_OPTS="-Xms1303m -Xmx1303m -XX:MaxPermSize=256m" in jboss-eap-6.2\bin\standalone.conf or "JAVA_OPTS=-Xms1G -Xmx1G -XX:MaxPermSize=256M" in jboss-eap-6.2\bin\standalone.conf.bat which is nothing but JVM memory allocation pool parameters.

Resolution :

  • Increase the heap size. To increase the heap size,
  • goto -> jboss-eap-6.2\bin\standalone.conf.bat or jboss-eap-6.2\bin\standalone.conf
  • change ->JAVA_OPTS="-Xms256m -Xmx512m -XX:MaxPermSize=256m" where -Xms is Minimum heap size and -Xmx is Maximum heap size.
  • Usually its not recommanded to have same size for min and max.

  • If you are running your application from eclipse,

  • Double click on the server
  • select 'open launch configuration' you will be redirected to the window 'Edit launch configuration properties'.
  • In this windown goto the tab '(x)=Arguments'.
  • In VM Arguments, define your heap size as mentioned below
  • "-Dprogram.name=JBossTools: JBoss EAP 6.1+ Runtime Server" -server -Xms256m -Xmx512m -XX:MaxPermSize=256m -Dorg.jboss.resolver.warning=true
Prabhakar
  • 311
  • 3
  • 7
  • A quote from the [Oracle Command Line Reference](https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html): "_For best performance, set -Xms to the same size as the maximum heap size, for example: `java -Xgcprio:throughput -Xmx:64m -Xms:64m myApp`_" – user1438038 Mar 03 '17 at 10:33
6

Suppose your class is called Test in package mypackage. Run your code like this:

java -Xmx1024m mypackage.Test

This will reserve 1024 MB of heap space for your code. If you want 512 MB, you can use:

java -Xmx512m mypackage.Test

Use little m in 1024m, 512m, etc

Sirex
  • 195
  • 2
  • 13
euphoria83
  • 14,768
  • 17
  • 63
  • 73
4

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.

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.

codeDr
  • 1,535
  • 17
  • 20
4

I had right amount of memory settings but for me it was using a 64bit intellij with 32 bit jvm. Once I switched to 64 bit VM, the error was gone.

vsingh
  • 6,365
  • 3
  • 53
  • 57
3

If you're running 32bit JVM, change heap size to smaller would probabaly help. You can do this by passing args to java directly or through enviroment variables like following,

java -Xms128M -Xmx512M
JAVA_OPTS="-Xms128M -Xmx512M"

For 64bit JVM, bigger heap size like -Xms512M -Xmx1536M should work.

Run java -version or java -d32, java--d64 for Java7 to check which version you're running.

h--n
  • 5,903
  • 4
  • 31
  • 32
2

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.

Community
  • 1
  • 1
daramasala
  • 3,040
  • 2
  • 26
  • 33
1

Anyway, here is how to fix it: Go to Start->Control Panel->System->Advanced(tab)->Environment Variables->System Variables->New: Variable name: _JAVA_OPTIONS Variable value: -Xmx512M

OR

Change the ant call as shown as below.

   <exec
        **<arg value="-J-Xmx512m" />**
    </exec>

It worked for me.

Sudhakar
  • 3,104
  • 2
  • 27
  • 36
1

Error occurred during initialization of VM Could not reserve enough space for 1572864KB object heap

I changed value of memory in settings.grade file 1536 to 512 and it helped

1

Go to Start->Control Panel->System->Advanced(tab)->Environment Variables->System Variables->New:

Variable name: _JAVA_OPTIONS
Variable value: -Xmx512M
Hearen
  • 7,420
  • 4
  • 53
  • 63
0

In case you are running a java program: - run your program in a terminal using the correct command for linux it would be 'java -jar myprogram.jar' and add -Xms256m -Xmx512m, for instance: 'java -jar myprogram.jar Xms256m -Xmx512m'

In case you are running a .sh script (linux, mac?) or a .bat script (windows) open the script and look for the java options if they are present and increase the memory.

If all of the above doesn't work, check your processes (ctrl+alt+delete on windows) (ps aux on linux/mac) and kill the processes which use allot of memory and are not necessary for your operating system! => Try to re-run your program.

Fico
  • 619
  • 8
  • 12
0

In CASSANDRA_HOME/bin/cassandra.bat you would find following configuration

REM JVM Opts we'll use in legacy run or installation
set JAVA_OPTS=-ea^
 -javaagent:"%CASSANDRA_HOME%\lib\jamm-0.3.0.jar"^
 -Xms**2G**^
 -Xmx**2G**^

You can reduce 2G to some smaller number for e.g. 1G or even lesser and it should work.

Same if you are running on unix box, change in .sh file appropriately.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Deepak Singhvi
  • 727
  • 6
  • 13
0

Sometimes it relates as

$ sysctl vm.overcommit_memory
vm.overcommit_memory = 2

If you set it to:

$ sysctl vm.overcommit_memory=0

It should work.

Hearen
  • 7,420
  • 4
  • 53
  • 63
kay
  • 693
  • 2
  • 7
  • 13
0

Replace -Xmx2G with -Xms512M or any greater memory size in cassandra.bat file in cassandra bin directory.

Hearen
  • 7,420
  • 4
  • 53
  • 63
kiran
  • 77
  • 1
  • 11
0

I got the same error and it got resolved when I deleted temp files using %temp% and restarting eclipse.

Viswanath Nuggu
  • 63
  • 1
  • 1
  • 9
0

In my case I couldn't increase org.gradle.jvmargs=-Xmx... in gradle.properties beyond 1GB. It didn't work because I had two Java installation on my machine, one 32 bit (Gradle was using this one) and the other 64 bit. I resolved the problem by adding JAVA_HOME environment variable pointing to 64 bit Java.

Michael Dz
  • 3,655
  • 8
  • 40
  • 74
-2

No need to do anything just chnage in POM file like below

<configuration>
    <maxmemory>1024M</maxmemory>
</configuration>
hyde
  • 60,639
  • 21
  • 115
  • 176
Harish
  • 1