Understanding JVM heap space:
Limited memory is allocated in JVM to run a java application. The memory is specified during the startup of applications.Read Also: Java.net.ConnectException: Connection Refused
-Xms is a VM option to specify heap memory. Heap size can be fixed or variable depending on the strategy of garbage collection. Maximum heap size can be specified by -Xmx option.
The main cause of heap memory error occurs for the following three reasons:
Reason 1:
If we run a java application without specifying memory we can get this kind of error.The error should be like this.
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.
Solution:
Most of the time this error occurs for 32 bit JVM. Because 32 bit JVM requires free space in memory to run the application but many times there is not enough memory to run java application.We should replace 32 bit JVM with 64 bit JVM in a 64-bit computer to resolve this issue or we can specify memory to run the application by using below command
java -Xmx256M JavaApplication
Reason 2:
If we assign memory to 32 bit JVM greater than the heap size of the JVM, we can get this kind of error.Suppose our JVM heap memory size is 2006M and we also assign the same size when we execute the program, we will get heap size error. For example, if we execute the following command, we will get
error in 32-bit JVM.
java -Xms2006M -Xmx2006M JavaApplication
Solution:
We should specify smaller memory as heap memory in 32 bit JVM as shown below or we should use 64 bit JVMjava -Xms1336M -Xmx1336M JavaApplication
Reason 3:
If we specify heap memory more than our physical memory, we will get this kind of error. Suppose we have 2GB ram but if we execute the following command we will get the error.java -Xms8192M -Xmx8192M JavaApplication
Solution:
We can get rid of this error by specifying a smaller heap memory.java -Xms1336M -Xmx1336M JavaApplication
Another way to resolve this issue, we should specify memory size in the _JAVA_OPTIONS variable in the path of the operation system. When we run the Java application it will take the memory size from the path. This path data should overcome the problems described above.
For Linux:
-bash-3.2$ export _JAVA_OPTIONS="-Xmx256M"
-bash-3.2$ javac JavaApplication.java
For Windows:
Follow the below path:
Start -> Control Panel -> System -> Advance(tab) -> Environment Vairables -> System Variables ->
Create New Variable Name: _JAVA_OPTIONS -> Variable Value: -Xmx256M
Nowadays, 64 bit systems are available. We should avoid 32-bit hardware architecture and 32-bit java versions.
That's all for today, please mention in comments in case you have any questions related to could not reserve enough space for object heap error.
0 Comments