首页   注册   登录
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
SunnyGrocery
V2EX  ›  Java

无法从 ReferenceQueue 中取出刚放进去的 Reference 元素,一直为 null

  •  
  •   SunnyGrocery · 64 天前 · 670 次点击
    这是一个创建于 64 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近在读 TIJ4,在 P518 页持有引用一节,书中讲述了有关 java.lang.ref 包下的 Reference 类

    Java 新手,查了些资料还是没有头绪,实在不行的话我换个 JDK 试下...
    以下是原书部分样例代码

    package sust.p600.p519;
    
    import java.lang.ref.Reference;
    import java.lang.ref.ReferenceQueue;
    import java.lang.ref.SoftReference;
    import java.lang.ref.WeakReference;
    import java.util.LinkedList;
    
    /**
     * Created by SunnyGrocery on 2019/11/17 18:05
     */
    public class References {
        private static ReferenceQueue<VeryBig> referenceQueue = new ReferenceQueue<>();
    
        public static void main(String[] args) {
            int size = 10;
    
            LinkedList<SoftReference<VeryBig>> referenceLinkedList = new LinkedList<>();
            for (int i = 0; i < size; i++) {
                referenceLinkedList.add(new SoftReference<>(
                        new VeryBig("Soft:" + i),
                        referenceQueue));
                System.out.println("Just created:" + referenceLinkedList.getLast());
                checkQueue();
            }
    
            LinkedList<WeakReference<VeryBig>> referenceLinkedList1 = new LinkedList<>();
            for (int i = 0; i < size; i++) {
                referenceLinkedList1.add(new WeakReference<>(
                        new VeryBig("Weak:" + i),
                        referenceQueue));
                System.out.println("Just created:" + referenceLinkedList1.getLast());
                checkQueue();
            }
    
        }
    
        private static void checkQueue() {
            Reference<? extends VeryBig> reference = referenceQueue.poll();
            if (reference != null) {
                System.out.println("In queue: " + reference.get());
            }
        }
    
    }
    
    class VeryBig {
        private static final int SiZE = 10000;
        private long[] la = new long[SiZE];
        private String ident;
    
        public VeryBig(String id) {
            ident = id;
        }
    
        @Override
        public String toString() {
            return "id = " + ident;
        }
    
        protected void finalize() {
            System.out.println("Finalizing:" + ident);
        }
    }
    
    

    输出

    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    Just created:[email protected]
    
    

    结果中没有 checkQueue 中当 reference != null 时的输出 也就是说, referenceQueue.poll() 一直为空

    2 回复  |  直到 2019-11-18 18:10:55 +08:00
    zhady009
        1
    zhady009   63 天前   ♥ 1
    堆大小多大? 我测试-Xmx3M 才能取出 或者可以加大 VeryBig 中的数组大小
    SunnyGrocery
        2
    SunnyGrocery   63 天前
    @zhady009 很大,默认是 2113929216bit,原来是和 JVM 堆大小有关,谢谢你噢
    关于   ·   FAQ   ·   API   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   2481 人在线   最高记录 5168   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.3 · 28ms · UTC 12:44 · PVG 20:44 · LAX 04:44 · JFK 07:44
    ♥ Do have faith in what you're doing.