Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6395952

Concatenating String in jdk 5.0 will consume much more memory than in jdk 1.4.2

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • 5.0u6
    • 9.0pe
    • core-libs
    • x86
    • windows_xp

      $ 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.

            kbr Kenneth Russell (Inactive)
            xiaojuzh Xiaojun Zhang (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: