Maximum size for StringBuffer

Why is the restriction StringBufferlimited by its size?

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

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

Suppose we have 2 ^ 31-1 characters in StringBufferand add a few more characters to this StringBuffer. The member’s member variable will be increased by the number of characters added, and if the value of the Count variable is already at maximum (2 ^ 31-1), it will return to some negative value.

Why is this a mistake?

+5
source share
4 answers

stringbuffer , , , 2 ^ 31-1, ,

+13

StringBuffer char[].
Java , , Integer.MAX_VALUE - 1 (.. 2 ​​^ 31 - 1). , Java Integer.MAX_VALUE

+4

.

, 2012

. = 37748734

Java: 1.6.0_35

: Windows 7

: 32 (x86)

: 2

: Pentium Dual Core E5800 3,20

2016

. = 301989886

Java: 1.8

OS: Ubuntu 14 LTE and Windows 7

System Architecture: 64 bit (x86_64)

RAM: 8 GB

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

Run this program yourself

        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
+3
source

if you get about 2 ^ 31 characters in your buffer, you are doing it wrong and you would have run out of memory long ago.

0
source

All Articles