JDK-6395952 : Concatenating String in jdk 5.0 will consume much more memory than in jdk 1.4.2
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 9.0pe
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-03-09
  • Updated: 2010-08-05
  • Resolved: 2006-06-15
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0u6Resolved
Related Reports
Duplicate :  
Description
$ E:/jdk14208/bin/java -verbose:gc -Xms32m -Xmx32m StringTest
[GC 2110K->96K(32576K), 0.0154693 secs] 
[GC 2204K->98K(32576K), 0.0136285 secs] 
[GC 2205K->99K(32576K), 0.0132961 secs] 
[GC 2208K->101K(32576K), 0.0130327 secs]

$ E:/jdk5006/bin/java -verbose:gc -Xms32m -Xmx32m StringTest
[GC 2112K->144K(32576K), 0.0159576 secs]
[GC 2253K->146K(32576K), 0.0146236 secs]
[GC 2258K->148K(32576K), 0.0139504 secs]
[GC 2260K->150K(32576K), 0.0135361 secs]
[GC 2261K->151K(32576K), 0.0135612 secs]
[GC 2263K->145K(32576K), 0.0136109 secs]
[GC 2256K->146K(32576K), 0.0135520 secs]
[GC 2251K->154K(32576K), 0.0136070 secs]

in jdk 1.4.2, StringBuffer.toString()
StringBuffer.java
     585     public synchronized String toString() {
     586         return new String(value, 0, count);
     587     }

String.java
     562     // Package private constructor which shares value array for speed.
     563     String(int offset, int count, char value[]) {
     564         this.value = value;
     565         this.offset = offset;
     566         this.count = count;
     567     }

there is no char[] copy operation here.

But, in jdk 5.0, when StringBuffer.toString() is called,
StringBuffer.java
     585     public synchronized String toString() {
     586         return new String(value, 0, count);
     587     }

String.java
     200     public String(char value[], int offset, int count) {
     201         if (offset < 0) {
     202             throw new StringIndexOutOfBoundsException(offset);
     203         }
     204         if (count < 0) {
     205             throw new StringIndexOutOfBoundsException(count);
     206         }
     207         // Note: offset or count might be near -1>>>1.
     208         if (offset > value.length - count) {
     209             throw new StringIndexOutOfBoundsException(offset + count);
     210         }
     211         char[] v = new char[count];
     212         System.arraycopy(value, offset, v, 0, count);
     213         this.offset = 0;
     214         this.count = count;
     215         this.value = v;
     216     }

you can see copy char[] via System.arraycopy() that would incur memory increase.

Comments
EVALUATION Please see the evaluation of 6219959 for a thorough explanation of the investigation into String/StringBuilder sharing in the 5.0 and later releases. This bug is being closed as a duplicate of 6219959, which itself is closed as "will not fix".
15-06-2006

EVALUATION This request appears to be a duplicate of 6219959. I expect to close it at such; however, before I take that step, it would be extremely helpful if you could provide the source to your StringTest benchmark. I assume that it is a reduction of a common scenario you have seen in code? *** (#1 of 1): [ UNSAVED ] ###@###.###
09-03-2006