Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5089202

cannot allocate a java.lang.StringBuffer object with size of 1GB

XMLWordPrintable

    • b58
    • generic, sparc
    • solaris_9, solaris_10

      Cannot allocate a java.lang.StringBuffer object with size of 1GB, not even
      in 64-bit VM.

      The problem is easily reproducible:

      1. Testcase
      -----------
      % more Test.java
      import java.io.*;

      public class Test {

          public static void main(String argv[]) throws Exception {

              int size;
               size = 1073741824; // fails: 1GB
               size = 1073741800; // succeeds: 1GB-24
               size = 1073741808; // fails: 1GB-16
               size = 1073741804; // succeeds: 1GB-20
               size = 1073741805; // fails: 1GB-19

              Runtime runstats = Runtime.getRuntime();

              System.out.println("Max mem:" + runstats.maxMemory());
              System.out.println("Total mem:" + runstats.totalMemory());
              System.out.println("Free mem:" + runstats.freeMemory());

              System.out.println("Trying " + size );
              StringBuffer sb = new StringBuffer(size);
              System.out.println("StringBuffer of size: " + size + " allocated");

              System.out.println("Total mem:" + runstats.totalMemory());
              System.out.println("Free mem:" + runstats.freeMemory());
          }
      }

      %


      2. Compile
      ----------
      % /j2sdk1.4.2_05/bin/java -d64 -version
      java version "1.4.2_05"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_05-b04)
      Java HotSpot(TM) 64-Bit Server VM (build 1.4.2_05-b04, mixed mode)
      %
      % /j2sdk1.4.2_05/bin/javac Test.java
      %


      3. Run, trying 1GB
      ------------------
      % /j2sdk1.4.2_05/bin/java -d64 -Xms4g -Xmx8g Test
      Max mem:8505720832
      Total mem:4252893184
      Free mem:4252728928
      Trying 1073741824
      Exception in thread "main" java.lang.OutOfMemoryError
      %


      4. Trying almost 1GB (1GB-20B)
      ------------------------------

      % /j2sdk1.4.2_05/bin/javac Test.java
      % /j2sdk1.4.2_05/bin/java -d64 -Xms4g -Xmx8g Test
      Max mem:8505720832
      Total mem:4252893184
      Free mem:4252728928
      Trying 1073741804
      StringBuffer of size: 1073741804 allocated
      Total mem:4252958720
      Free mem:2105352568
      %


      5. Java 1.5
      -----------
      % /jdk1.5.0/bin/java -d64 -version
      java version "1.5.0-rc"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-rc-b63)
      Java HotSpot(TM) 64-Bit Server VM (build 1.5.0-rc-b63, mixed mode)
      %
      % /jdk1.5.0/bin/java -d64 -Xms4g -Xmx8g Test
      Max mem:7635533824
      Total mem:4116054016
      Free mem:4094577848
      Trying 1073741824
      Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
      %


      6. Documentation
      ----------------
       The API doc says, that argument is "int", which spans from -2GB to 2GB-1:

       StringBuffer(int length)
                Constructs a string buffer with no characters in it and an initial
                capacity specified by the length argument.

       No notice is given that there is a limit at about 1 GB.
      ###@###.### 10/21/04 17:28 GMT

            kamg Keith Mcguigan (Inactive)
            thlenz Thomas Lenz (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: