1 Reply Latest reply: Feb 20, 2009 8:05 PM by 843829 RSS

    -Xmx bug?

    843829
      Hello,

      I have the following Java program:
      public class Main {
          public static void main(String[] args) {
              int a[] = new int[1000000];
          }
      }
      When I run it with a limit of 2 MB like so
      $ java -Xmx2M Main
      it should crash with java.lang.OutOfMemoryError because the size of the array is approximately 4 MB. But on my computer (64bit Linux) the program runs without any Errors being thrown.

      Any comments? Can somebody reproduce it?

      --
      Jure
        • 1. Re: -Xmx bug?
          843829
          zbontarj wrote:
          ...
          When I run it with a limit of 2 MB like so
          $ java -Xmx2M Main
          it should crash with java.lang.OutOfMemoryError because the size of the array is approximately 4 MB. But on my computer (64bit Linux) the program runs without any Errors being thrown.
          Sun's JVM does some rounding of the heap size based on other variables. In particular, -XX:NewSize=<ns>, which is the initial size of the young generation and -XX:OldSize=<os>, the initial size of the old generation The default value of NewSize is platform-dependent, but in the range 1-3MiB; the default OldSize is 4M. The heap size (-Xmx) value is rounded up to match these.

          Clearly it's not ideal since the value you explicitly specified (-Xmx2m) is being changed to be compatible with default values that you didn't specify, instead of the other way around. FWIW, I think it was done this way to avoid being too pedantic and exiting with an error when incompatible settings were given on the command line, but clearly it could be improved. If it's important to you, you can file a bug.