RFR: 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary

Martin Buchholz martinrb at google.com
Mon Feb 22 19:49:41 UTC 2016


Can you make the code look more like the recently optimized code in
ArrayList and Vector?

On Mon, Feb 22, 2016 at 11:20 AM, Ivan Gerasimov
<ivan.gerasimov at oracle.com> wrote:
> Hello!
>
> When the capacity of a StringBuilder needs to be increased (either due to
> append() or due to explicit call to ensureCapacity()), the new capacity is
> calculated as "twice the old capacity, plus 2", rounded down to
> Integer.MAX_VALUE:
> http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html#ensureCapacity-int-
> Because of that, StringBuilder throws OOM early, even though there may be
> room to grow.
>
> The proposed solution is to reserve a few bytes at the top of the range and
> only try to allocate them if absolutely required.
>
> The regression test is @ignored by default, as it is too greedy for memory.
>
> Would you please help review the fix?
>
> BUGURL: https://bugs.openjdk.java.net/browse/JDK-8149330
> WEBREV: http://cr.openjdk.java.net/~igerasim/8149330/00/webrev/
>
> Sincerely yours,
> Ivan



More information about the core-libs-dev mailing list