JDK-5089202 : cannot allocate a java.lang.StringBuffer object with size of 1GB
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 1.4.2_05,5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: solaris_9,solaris_10
  • CPU: generic,sparc
  • Submitted: 2004-08-19
  • Updated: 2012-10-08
  • Resolved: 2005-10-27
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b58Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Description
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

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang
22-09-2004

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

EVALUATION Will look into this in next release. ###@###.### 2004-08-26
26-08-2004