Max size for StringBuffer
Asked Answered
I

4

6

Why would the StringBuffer have a limit on its size?

I went through some of the links : http://www.coderanch.com/t/540346/java/java/maximum-size-hold-String-buffer.

Is that because of the count member variable, which is an int?

Suppose that we have 2^31-1 chars in StringBuffer and that we append some more chars to that StringBuffer. Count member variable would be incremented by the number of chars appended and if Count variable is already at its max (2^31-1), it would revert back to some negative value.

Why would it throw an error?

Indiaindiaman answered 10/8, 2012 at 20:17 Comment(2)
Don't use a StringBuffer when you can use a StringBuilder (it has the same limit though)Cowardice
A 2^32-1 StringBuilder would take 4 GB. Are you like to create such a long string?Cowardice
W
14

because stringbuffer internally uses an array and the maximum number of elements an array can accommodate is 2^31-1 if you increment after reaching this it will go to negative and throws the error

Winifield answered 10/8, 2012 at 20:19 Comment(3)
why is there a max limit on the number of elements in an array, is that mentioned int the java specs?Indiaindiaman
@Indiaindiaman random array access uses an integer for the position. ie: myArray[8]. How would you access elements at position MAX_INT+1?Gill
just try creating an array of size some long valueWinifield
A
5

StringBuffer uses a char[].
In Java an array can be indexed only via an integer which means the highest value the index of the array can be is Integer.MAX_VALUE - 1 (i.e. 2^31 - 1). Which means that the size of an array in Java can not be larger than Integer.MAX_VALUE

Aeriform answered 10/8, 2012 at 20:24 Comment(0)
S
3

Answer is different based on System architecture.

This old machine when I posted answer on 2012

Max Length of characters = 37748734

Java Version : 1.6.0_35

OS : Windows 7

System Architecture : 32bits (x86)

RAM : 2 GB

Processor : Pentium Dual Core E5800 3.20GHz

On 2016

Max Length of characters = 301989886

Java Version : 1.8

OS : Ubuntu 14 LTE and Windows 7

System Architecture : 64bits (x86_64)

RAM : 8 GB

Processor : Intel(R) Core(TM) i3-4130 CPU @ 3.40GHz

Run This program your self

        StringBuffer strbTest = new StringBuffer();
        long len = 0;
        try {
            System.out.println("Wait.... til number not generated.");

            while(true) {
                strbTest.append("a");
                len++;
            }
        } catch (OutOfMemoryError e) {
            System.out.println("Max length on your system is "+len);
            System.out.println("Error");
        }
        System.out.println("End");

Output

Max length on your system is 37748734
Supersaturate answered 2/11, 2012 at 12:4 Comment(4)
Yup , i ran that and got the exact same value --> 37748734. My system is also a C2D e7500 and 4 gigs of ram. I am running 32 bit windows. The Integer MAX VALUE is 2147483647.Furnace
I ran on and got different answer from I posted : 301989886, x86_64 machine 8 GB Ram, Java 1.8 and 4 GHz processor. Same number got on other x86_64 system.Supersaturate
What matters is the impl of the StringBuffer class in your JVM version and platform... when it needs to expand its capacity, it may use a load factor and, might use Integer.MAX_VALUE (2^31 - 1 as everyone is pointing out) for the maximum capacity before blowing up... so if you don't get that value it's because your implementation is just expanding the array until it detects overflow... this one tries Integer.MAX_VALUE before blowing: grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/…Beaird
Also, it's useless to post your machine's specs... it won't affect this, what will affect this is how much memory you give the JVM. That's what the -Xmx flag is for. If you don't specify that, you get the default the JVM decides (probably based on free memory), which is probably what you're seeing in your test.Beaird
P
0

if you are getting anywhere near 2^31 characters in your buffer you are doing it wrong and you would have run out of memory long ago.

Plumber answered 10/8, 2012 at 20:19 Comment(1)
I agree with you on the point that he is doing it wrong. However there is no reason to think he will be out of memory.Gill

© 2022 - 2024 — McMap. All rights reserved.