有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java GC未清除垃圾收集SoftReference

我试图理解Java中的软引用,它基本上确保在抛出StackOverflowerError之前清除软引用对象的内存

public class Temp 
{

    public static void main(String args[])
    {
        Temp temp2 = new Temp();
        SoftReference<Temp> sr=new SoftReference<Temp>(temp2);

        temp2=null;
        Temp temp=new Temp();
        temp.infinite(sr);

    }

    public void infinite(SoftReference sr)
    {

        try
        {
            infinite(sr);
        }
        catch(StackOverflowError ex)
        {
            System.out.println(sr.get());
            System.out.println(sr.isEnqueued());
        }

    }
}

然而,上述研究的结果并不乐观

test.Temp@7852e922
false

有人能解释一下为什么GC没有清除这个物体吗?我该怎么做


共 (0) 个答案