JDK-5030946 : StringBuffer should have a append(String, int start, int length) method
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2004-04-13
  • Updated: 2004-04-13
  • Resolved: 2004-04-13
Related Reports
Duplicate :  
Description

Name: jl125535			Date: 04/13/2004


A DESCRIPTION OF THE REQUEST :
java.lang.StringBuffer should have a append(String, int start, int length) method to avoid creating temporary Strings or CharSequence objects.

JUSTIFICATION :
I've a code block that needs to build new strings from parts of other strings that wastes 30Kbyte of memory (that gets collected by the GC) to construct ~1000 Strings.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
  To be able to write the test program like this:

NOTE: " Hallo " and " Welt "  are only place holders for String objects in the real application.

public class Test {
  public static void main(String[] args) {
    for(int i=0 ; i<1000 ; ++i) {
       StringBuffer sb = new StringBuffer();
       sb.append(" Hallo ", 1, 5);
       sb.append(" Welt ", 0, 5);
       System.out.println(sb.toString() );
    }
  }
}
ACTUAL -
The line from the hprof run:

TRACE 242:
	Test.main(Test.java:4)
TRACE 245:
	java.lang.String.substring(String.java:1446)
	Test.main(Test.java:5)
TRACE 247:
	java.lang.String.substring(String.java:1446)
	Test.main(Test.java:6)
TRACE 249:
	java.lang.StringBuffer.toString(StringBuffer.java:1256)
	Test.main(Test.java:7)

          percent         live       alloc'ed  stack class
 rank   self  accum    bytes objs   bytes objs trace name
    1 55.29% 55.29%   601448  220  601448  220     1 [I
    2  8.83% 64.12%    96000 2000   96000 2000   250 java.nio.HeapCharBuffer
    3  5.91% 70.03%    64304  431   64304  431     1 [C
    4  4.41% 74.44%    48000 1000   48000 1000   243 [C
    5  4.18% 78.62%    45416  214   45416  214     1 [B
    6  2.21% 80.83%    24000 1000   24000 1000   249 java.lang.String
    7  2.21% 83.03%    24000 1000   24000 1000   247 java.lang.String
    8  2.21% 85.24%    24000 1000   24000 1000   245 java.lang.String
    9  2.21% 87.45%    24000 1000   24000 1000   242 java.lang.StringBuffer


---------- BEGIN SOURCE ----------
public class Test {
  public static void main(String[] args) {
    for(int i=0 ; i<1000 ; ++i) {
       StringBuffer sb = new StringBuffer();
       sb.append(" Hallo ".substring(1, 5));
       sb.append(" Welt ".substring(0, 5));
       System.out.println(sb.toString() );
    }
  }
}
---------- END SOURCE ----------
(Incident Review ID: 250048) 
======================================================================

Comments
EVALUATION The method StringBuffer/StringBuilder.append(CharSequence, int start, int end) has already been added to Tiger. It will not waste memory since it grabs chars one at a time from the CharSequence. If the performance of this method is unsatisfactory then it can be optimized for String arguments. ###@###.### 2004-04-13
13-04-2004