JDK-4524944 : Add method to StringBuffer to reduce capacity
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2001-11-08
  • Updated: 2002-08-14
  • Resolved: 2002-08-14
Related Reports
Duplicate :  
Description

Name: nt126004			Date: 11/08/2001


java version "1.4.0-beta3"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)


Once a StringBuffer has been created the length of its internal char[] (the
variable is called, "value") can only increase in size, it never decreases. The
programmer cannot shrink the size of this buffer. The "ensureCapacity()" method
allows the programmer to increase the size of the buffer -- We need a
corresponding way to decrease the size of the internal buffer:

I've seen cases where 99% of the time the StringBuffer's internal char[] needs
to be relatively small (say 100 bytes). The other 1% of the time it may need to
be 20,000 bytes or more. The problem is that the size of the internal char[]
always stays at its largest size. Once it reaches 20,000 bytes it can never be
made to shrink. This is a horrible waste of memory and a horrible design.

I would suggest a method that trims the internal char[] to the approximate
length required. A new method like, "trimToLength()", that sets the internal
char[] length to the number of characters actually being used. Another nice
possibility is an instance variable that tells StringBuffer to keep its
allocated char[] within a certain threshold of the required number of
characters. The main point is that we need a way to reduce the size of the
StringBuffer's internal char[] so it doesn't grow out of control.
(Review ID: 135208) 
======================================================================

Comments
EVALUATION Ability to trim StringBuffer in some way is a frequently requested feature and will be considered for feature releases. ###@###.### 2002-04-10
10-04-2002