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

Interning strings prevents their being garbage collected

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.1.1
    • hotspot
    • None
    • sparc
    • solaris_2.5.1



      Name: tb29552 Date: 08/18/97


      company - Electric Communities , email - ###@###.###

      Here is the source code for my test and the results. Please let
      me know if you need additional help.

      public class TestIntern {

      static public void main(String[] args) throws Throwable {

      String s = "TestMessage";

      try {
      for (;;) {
           int i = s.length()/2;
                      if (i > 250000) {
                   i = s.length()-1;
                      }
           s = s+s.substring(i);
           s = s.intern();
                  System.out.println("String length " + s.length()+", i="+i);
           }
      } catch(OutOfMemoryError e) {
      System.err.println("String length is " + s.length());
           e.printStackTrace();
      }
      }
      }

      Here's the result:

      groucho% java TestIntern
      String length 17, i=5
      String length 26, i=8
      String length 39, i=13
      String length 59, i=19
      String length 89, i=29
      String length 134, i=44
      String length 201, i=67
      String length 302, i=100
      String length 453, i=151
      String length 680, i=226
      String length 1020, i=340
      String length 1530, i=510
      String length 2295, i=765
      String length 3443, i=1147
      String length 5165, i=1721
      String length 7748, i=2582
      String length 11622, i=3874
      String length 17433, i=5811
      String length 26150, i=8716
      String length 39225, i=13075
      String length 58838, i=19612
      String length 88257, i=29419
      String length 132386, i=44128
      String length 198579, i=66193
      String length 297869, i=99289
      String length 446804, i=148934
      String length 670206, i=223402
      String length 670207, i=670205
      String length 670208, i=670206
      String length 670209, i=670207
      String length 670210, i=670208
      String length is 670210
      java.lang.OutOfMemoryError
              at java.lang.StringBuffer.<init>(StringBuffer.java)
              at java.lang.StringBuffer.<init>(StringBuffer.java)
              at TestIntern.main(TestIntern.java:15)

      If I comment out the s.intern() call, I get:

      ... deleted output lines
      String length 297869, i=99289
      String length 446804, i=148934
      String length 670206, i=223402
      String length 670207, i=670205
      String length 670208, i=670206
      String length 670209, i=670207
      String length 670210, i=670208
      String length 670211, i=670209
      String length 670212, i=670210
      String length 670213, i=670211
      String length 670214, i=670212
      String length 670215, i=670213
      String length 670216, i=670214
      String length 670217, i=670215
      String length 670218, i=670216
      String length 670219, i=670217
      String length 670220, i=670218
      String length 670221, i=670219
      String length 670222, i=670220
      String length 670223, i=670221
      String length 670224, i=670222
      String length 670225, i=670223
      String length 670226, i=670224
      String length 670227, i=670225
      String length 670228, i=670226

      Which I stopped with Control-C. My conclusion is that
      interning a string protects it from ever being garbage
      collected. Boo! Hiss!


      company - Electric Communities , email - ###@###.###
      ======================================================================

            apalanissunw Anand Palaniswamy (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: