4

As I read the below sample code on wikipedia http://en.wikipedia.org/wiki/Weak_reference

import java.lang.ref.WeakReference;


    public class ReferenceTest {
            public static void main(String[] args) throws InterruptedException {

                WeakReference r = new WeakReference(new String("I'm here"));
                WeakReference sr = new WeakReference("I'm here");
                System.out.println("before gc: r=" + r.get() + ", static=" + sr.get());
                System.gc();
                Thread.sleep(100);

                // only r.get() becomes null
                System.out.println("after gc: r=" + r.get() + ", static=" + sr.get());

            }
    } 

Output before gc: r=I'm here, static=I'm here after gc: r=null, static=I'm here

I am not able to understand the output after gc, where is the strong reference to string referred by sr(WeakReference) to the string in string pool

Manish Doshi
  • 1,205
  • 1
  • 9
  • 17
lowLatency
  • 5,534
  • 12
  • 44
  • 70
  • @ManishDoshi If you're not in a position to comment usefully on this question you should be consulting the Javadoc and possibly asking your own question. – user207421 Jul 05 '13 at 10:23
  • Exactly same question: http://stackoverflow.com/questions/14494875/weakreference-string-didnt-garbage-collected-how – me_digvijay Jul 05 '13 at 14:37
  • @ManishDoni You don't seem to understand the StackOverflow site. The author is the guy asking the question. Others are supposed to either answer the question or provide useful or relevant comments, or ask questions that may elucidate the problem and lead to an answer. Yours is neither. It is in fact another question and can be answered in the ways I indicated. It is also illogical to ask a question of someone who possibly doesn't understand it himself, which may be why *he* is asking *his* question. – user207421 Jul 06 '13 at 02:06

3 Answers3

0

sr is not garbage collected because String internally caches Strings. Therefore the internal caches still has a reference and therefore the WeakRefence is not garbage collected.

Statically constructed String like in the case of sr are added to the cache. String objects constructed using new Stirng("...") are not. Therefore it is usally better to not use new String("...").

ssindelar
  • 2,833
  • 1
  • 17
  • 36
  • No. sr is not garbage-collected because literal strings are placed in the constant pool by the compiler. Caching by the String class has nothing to do with it. – user207421 Jul 10 '13 at 00:46
0

Here in first case, when you create object of string using new String("I'm here")so object are always created on heap.So after that if you call System.gc(); then that object can directly available for garbage collection.

While in second case, you are passing string as reference of object.So here it will not creat new object of String as string is directly initialized to reference of object.So it will be not available for garbage collection.Because this string will be stay in string-pool.

Manish Doshi
  • 1,205
  • 1
  • 9
  • 17
0

The objects in String pools will not be garbage collected as they do not reside in Heap. If you want to place a new String() in the pool, you have an option to use String#intern()

sadhu
  • 1,429
  • 8
  • 14