WeakReference 不返回 null,尽管没有对实际引用对象的强引用

发布于 2024-12-11 05:07:06 字数 1163 浏览 0 评论 0原文

我正在阅读以下关于 java 中的弱引用的文章:-

了解弱引用。

完成理论部分后,尝试测试空条件的弱引用。但是,弱引用的 null 检查 在以下代码中永远不会返回 true :-

package com.weak;

import java.lang.ref.WeakReference;

class Widget{}

public class WeakReferenceDemo {

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

        Widget widget = new Widget() ;
        WeakReference<Widget> valueWrapper = new WeakReference<Widget>(widget) ;

        System.out.println( valueWrapper.get() );

        //here strong reference to object is lost
        widget = null ;

        int count = 0 ;

        //here checking for null condition
        while( valueWrapper.get() != null ){
            System.out.print(".");
            Thread.sleep(432) ;

            if(++count % 25 == 0)   System.out.println();
        }

        System.out.println( valueWrapper.get() );
    }
}

请建议,为什么 valueWrapper.get() 不返回 null,尽管 widget< /strong> 引用被分配空值。

谢谢。

I am going through the following post on Weak References in java :-

Understanding Weak References.

After going through the theoretical portion, trying to test weak reference for null condition. But, null check for weak reference never returns true in following code :-

package com.weak;

import java.lang.ref.WeakReference;

class Widget{}

public class WeakReferenceDemo {

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

        Widget widget = new Widget() ;
        WeakReference<Widget> valueWrapper = new WeakReference<Widget>(widget) ;

        System.out.println( valueWrapper.get() );

        //here strong reference to object is lost
        widget = null ;

        int count = 0 ;

        //here checking for null condition
        while( valueWrapper.get() != null ){
            System.out.print(".");
            Thread.sleep(432) ;

            if(++count % 25 == 0)   System.out.println();
        }

        System.out.println( valueWrapper.get() );
    }
}

Please suggest, why valueWrapper.get() doesn't return null, though widget reference is assigned null value.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弄潮 2024-12-18 05:07:06

不要等待垃圾回收,而是尝试使用 Runtime.getRuntime().gc() 自己调用它,然后检查 null 的弱引用。

弱可达对象仅在 GC 运行时才会被回收。由于您的程序不会在堆上实例化更多对象,因此如果不手动要求它运行,这种情况可能永远不会发生。

Rather than waiting for garbage collection, try calling it yourself with Runtime.getRuntime().gc(), then check the weak reference for null.

Weakly reachable objects are only reclaimed when GC runs. Since your program isn't instantiating any more objects onto the heap, this may never happen without manually asking it to run.

半世晨晓 2024-12-18 05:07:06

WeakReference.get() 不会返回 null,直到垃圾收集器确定该对象只能弱访问。这可能需要一些时间。

WeakReference.get() doesn't return null until the garbage collector has got around to determining that the object is only weakly reachable. That might take some time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文