In Java, how do I find out what is my maximum heap size? <p>I have the following class in Java :</p> <pre><code>public class MemoryUsage { public static void main(String[] args) { Vector v = new Vector(); while (true) { byte b[] = new byte[1048576]; try { Thread.sleep(200); } catch (InterruptedException ie){} v.add(b); Runtime rt = Runtime.getRuntime(); System.out.println( "free memory: " + rt.freeMemory() ); } } public void showCurrentTime() { long time = System.currentTimeMillis(); System.out.println(time); } } </code></pre> <p>And I want to limit how much memory can be used by this program. I tried to set an initial and maximum size (from command-line), like follows :</p> <pre><code>C:\JMX_code\Memory_test&gt;java -Xms2m -Xmx4068M MemoryUsage Error occurred during initialization of VM The size of the object heap + VM data exceeds the maximum representable size </code></pre> <p>But this doesn't work. How do I figure out what is my own maximum heap size ?</p> <p>My Java version info. is as follows :</p> <blockquote> <p>Java(TM) SE Runtime Environment (build 1.7.0_02-b13) Java HotSpot(TM) 64-Bit Server VM (build 22.0-b10, mixed mode)</p> </blockquote>