Minggu, 08 Juni 2014

Invalid initial and maximum heap size in JVM - How to fix




I was getting "Invalid initial heap size: -Xms=1024M" while
starting JVM and even after changing maximum heap size from 1024 to 512M it keep
crashing by telling "
Invalid initial heap size: -Xms=512m , Could not
create the Java virtual machine".
 I check almost everything
starting form checking how much physical memory my machine has to any typo in JVM parameters, only to find out that instead of M, I had put MB there. Java
accepts both small case and capital case for Kilo, Mega and Gigs. you can use m
or M, g or G etc but never used MB, GB or KB. 
Similar problem can occur with maximum heap size specified by
-Xmx. Also from Java 6 update 18
there is change on default
heap size in JVM.  












Invalid initial and maximum heap size in JVM





Invalid initial and maximum heap size in JVM

Here is a list of common errors while specifying maximum
and minimum heap size in Java :









java -Xmx4056M -Xms4056M
HelloWorld


Issue:  Error
occurred during initialization of VM
, The size of the object heap +
VM data exceeds the maximum representable size





Cause:  value of either -Xms or -Xmx is higher
than or close to size of physical memory, as my machine has 4GB memory.









java -Xmx1056M -Xms2056M HelloWorld


Issue:  Error
occurred during initialization of VM , Incompatible minimum and maximum heap
sizes specified





Cause:  value of -Xms is higher
than
-Xmx









java -Xms2056M HelloWorld


Issue: Error occurred during initialization of
VM , Could not reserve enough space for object heap





Cause: Only -Xms was provided and -Xmx was not
provided. you will also get this error if you have typo and instead of
-Xmx you have
specified
-Xms two times, happened to my friend last time.









Command: java -Xms1024 M -Xmx1024M
HelloWorld


Issue: Error occurred during initialization of
VM , Too small initial heap





Cause: If you had space between 1024 and M than JVM assumes size of -Xms
as 1024 bytes only and print error that its too small for JVM to start.







Invalid heap size


Another scenario when "invalid heap size" issue comes while restarting JVM is, when you configure 64 bit JVM to accept memory more than 4GB but its running on 32 bit data model. This "invalid heap size" occurs particularly in Solaris box where J2SE installation continas both 32 bit and 64 bit J2SE implementation. On other environment like Windows and Linux 32 bit and 64 bit JVM are installed separately. 64 bit JVM installed on Solaris machines runs with 32 bit model if you don't specify either -d32 or -d64, which won't accept Maximum heap size of 4GB, hence "invalid heap size". You can resolve this issue by running Solaris JVM with option -d64.  -d64 command line option allows JVM to use 64 bit data model if available.


public class HelloWorld{
public static void main(String args[]){
System.out.println("HelloWorld to Java");
}
}






$ test@nykdev32:~ java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) Server VM (build 14.0-b16, mixed mode)

$ test@nykdev32:~ java -Xmx4096M HelloWorld
Invalid maximum heap size: -Xmx4096M
The specified size exceeds the maximum representable size.
Could not create the Java virtual machine.

$ test@nykdev32:~ java -d64 -Xmx4096M HelloWorld
HelloWorld to Java






If you run
Java program from command line than you will also get message say Could
not create the Java virtual machine
 with each of invalid heap error
but if you run your program from Eclipse you will not get message "Could
not create the Java virtual machine", instead you will just see error
message in console.





Regarding default
heap size in Java
, from Java 6 update 18 there are significant changes in
how JVM calculates default heap size in 32 and 64 bit machine and on client and
server JVM mode:





1) Initial heap space and maximum heap space is larger for improved
performance.







2) Default maximum heap space is 1/2 of physical memory of size upto 192
bytes and 1/4th of physical memory for size upto 1G. So for 1G machine maximum
heap size is 256MB 2.maximum heap size will not be used until program creates
enough object to fill initial heap space which will be much lesser but at-least
8 MB or 1/64th part of Physical memory upto 1G.







3) For Server Java virtual machine default maximum heap space is 1G for
4GB of physical memory on a 32
bit JVM
. for 64 bit JVM its 32G for a physical memory of 128GB.















Other Java tutorials you may find useful





























Source:http://javarevisited.blogspot.com/2012/12/invalid-initial-and-maximum-heap-size.html

Tidak ada komentar:

Posting Komentar