对String pool Object的弱引用

时间:2021-02-19 16:10:10

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

当我在*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

在gc之前输出:r =我在这里,静态=我在这里之后gc:r = null,static =我在这里

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

我无法理解gc之后的输出,其中是sr(WeakReference)引用的字符串对字符串池中字符串的强引用

3 个解决方案

#1


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.

sr不是垃圾收集,因为String内部缓存字符串。因此内部缓存仍然有引用,因此WeakRefence不会被垃圾回收。

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("...").

静态构造的字符串就像在sr的情况下一样被添加到缓存中。使用新Stirng(“...”)构造的字符串对象不是。因此,最好不要使用新的String(“...”)。

#2


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.

在第一种情况下,当你使用new String(“我在这里”)创建字符串对象时,所以始终在heap上创建对象。之后如果你调用System.gc();然后该对象可以直接用于垃圾收集。

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.

在第二种情况下,你传递字符串作为对象的引用。所以在这里它不会创建String的新对象,因为字符串被直接初始化为对象的引用。因此它将不可用于垃圾收集。因为这个字符串将保持在字符串池中。

#3


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()

String池中的对象不会被垃圾收集,因为它们不驻留在Heap中。如果要在池中放置新的String(),可以选择使用String#intern()

#1


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.

sr不是垃圾收集,因为String内部缓存字符串。因此内部缓存仍然有引用,因此WeakRefence不会被垃圾回收。

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("...").

静态构造的字符串就像在sr的情况下一样被添加到缓存中。使用新Stirng(“...”)构造的字符串对象不是。因此,最好不要使用新的String(“...”)。

#2


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.

在第一种情况下,当你使用new String(“我在这里”)创建字符串对象时,所以始终在heap上创建对象。之后如果你调用System.gc();然后该对象可以直接用于垃圾收集。

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.

在第二种情况下,你传递字符串作为对象的引用。所以在这里它不会创建String的新对象,因为字符串被直接初始化为对象的引用。因此它将不可用于垃圾收集。因为这个字符串将保持在字符串池中。

#3


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()

String池中的对象不会被垃圾收集,因为它们不驻留在Heap中。如果要在池中放置新的String(),可以选择使用String#intern()