[問題] 關於PhantomReference
小弟我最近在研究SoftReference、WeakReference、PhantomReference等功能
SoftReference、WeakReference很好理解, 沒什麼問題.
但我在研究PhantomReference時覺得我的code跟我對PhantomReference理解有點出入
以下是我測試的code, jvm 設定 -Xms5M -Xmx10M (有透過MBean確認過參數無誤)
(01)    private volatile boolean isRun = true;
(02)
(03)    private void test() throws InterruptedException {
(04)            ReferenceQueue<byte[]> rq = new ReferenceQueue<>();
(05)
(06)            pollingReferenceQueue(rq); // fork thread poll referenceQueue
(07)
(08)            try {
(09)                    List<Object> l = new ArrayList<>();
(10)
(11)                    int times = 20;
(12)                    long restMs = 1000;
(13)                    for (int i = 1; i <= times; i++) {
(14)                            byte[] buff = new byte[1024 * 1024];
(15)                            PhantomReference<byte[]> pr = new PhantomReference<>(buff, rq);
(16)                            l.add(pr); // 這邊打開讓pr被強引用, rq才會收到資料??
(17)
(18)                            System.gc();
(19)
(20)                            System.out.println("<main> " + "(" + i + ") " + pr);
(21)
(22)                            Thread.sleep(restMs);
(23)                    }
(24)
(25)            } finally {
(26)                    isRun = false;
(27)            }
(28)    }
(29)
(30)    private void pollingReferenceQueue(ReferenceQueue<byte[]> rq) {
(31)            new Thread(() -> {
(32)                    while (this.isRun) {
(33)                            Reference<? extends byte[]> v = rq.poll();
(34)
(35)                            if (v == null) {
(36)                                    System.out.println("<poll> " + null);
(37)                            } else {
(38)                                    System.out.println("<poll> recycle " + v.toString());
(39)                            }
(40)
(41)                            try {
(42)                                    Thread.sleep(100);
(43)                            } catch (InterruptedException e) {
(44)                                    e.printStackTrace();
(45)                            }
(46)                    }
(47)            }, "polling").start();
(48)    }
我的問題在第16行, 當沒有把pr給強引用的時候, 當要被GC的時候並不會丟到rq裡?
但當pr給強引用的時候, 要被回收時就會出現在rq裡?
我對ReferenceQueue的理解是
"當Reference所參照的物件準備要回收的時候, 其Reference會先放置ReferenceQueue"
"因此可以利用ReferenceQueue得知有某些物件沒有人使用準備要被回收了!"
所以我不能理解的是為什麼pr被強引用之後準備回收才會被丟到rq裡?
但pr沒有被強引用要被回收時卻沒有丟到rq裡? (因為沒有OOM)
不曉得有沒有大大能解惑, 是我理解錯誤還是我的code在使用上就錯了?
--
你只是大大的世界中小小的一個島嶼
在你懷中長大的我們 從未忘記
我要用全部的力氣唱出對你的深情
歌聲中 只是真心的讚美              929 吳志寧
也有感謝和依戀 疼惜和憂煩        全心全意愛你
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 110.26.10.55 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/java/M.1594536845.A.512.html
※ 編輯: NullLife (110.26.10.55 臺灣), 07/12/2020 14:55:54
※ 編輯: NullLife (110.26.10.55 臺灣), 07/12/2020 15:01:34
→
07/13 09:22, 
                                5年前
                            , 1F
07/13 09:22, 1F
→
07/13 09:22, 
                                5年前
                            , 2F
07/13 09:22, 2F
推
07/13 11:23, 
                                5年前
                            , 3F
07/13 11:23, 3F

→
07/13 11:23, 
                                5年前
                            , 4F
07/13 11:23, 4F
→
07/13 11:30, 
                                5年前
                            , 5F
07/13 11:30, 5F
啊啊啊 看起來就是它耶!! 原來doc就有描述了QQ 感謝大大
※ 編輯: NullLife (125.227.32.37 臺灣), 07/16/2020 18:57:30
→
07/16 19:52, 
                                5年前
                            , 6F
07/16 19:52, 6F
→
07/16 19:52, 
                                5年前
                            , 7F
07/16 19:52, 7F
推
07/16 20:22, 
                                5年前
                            , 8F
07/16 20:22, 8F
→
07/16 20:22, 
                                5年前
                            , 9F
07/16 20:22, 9F
→
07/16 20:22, 
                                5年前
                            , 10F
07/16 20:22, 10F
→
07/17 09:30, 
                                5年前
                            , 11F
07/17 09:30, 11F
→
07/17 09:30, 
                                5年前
                            , 12F
07/17 09:30, 12F
→
07/17 09:31, 
                                5年前
                            , 13F
07/17 09:31, 13F
→
07/17 09:31, 
                                5年前
                            , 14F
07/17 09:31, 14F
→
07/17 09:32, 
                                5年前
                            , 15F
07/17 09:32, 15F
→
07/17 09:33, 
                                5年前
                            , 16F
07/17 09:33, 16F
→
07/17 09:34, 
                                5年前
                            , 17F
07/17 09:34, 17F
→
07/17 09:34, 
                                5年前
                            , 18F
07/17 09:34, 18F
推
07/17 16:00, 
                                5年前
                            , 19F
07/17 16:00, 19F
→
07/17 16:00, 
                                5年前
                            , 20F
07/17 16:00, 20F
java 近期熱門文章
PTT數位生活區 即時熱門文章