weakreference實現原理分析

 前言

若干年前看了Java的四種引用類型,只是簡單知道了不同類型的作用,但對其實現原理一直未能想明白,本文嘗試結合jdk,openjdk6的部分源碼分析弱引用實現的原理,供大家參考,部分技術細節沒有仔細研究,如有疑問歡迎留言討論

 實例分析

我們以WeakHashMap的處理過程爲例介紹一個weak reference的生命週期,首先我們調用WeakHashMap的put方法放入對象到Map中,WeakHashMap的Entry繼承了WeakReference

 

  1. private static class Entry<K,V> extends WeakReference<K> implements Map.Entry<K,V> { 
  2.         private V value; 
  3.         private final int hash; 
  4.         private Entry<K,V> next; 

下面是put的部分代碼

  1. Entry<K,V> e = tab[i]; 
  2.         tab[i] = new Entry<K,V>(k, value, queue, h, e); 
  3.         if (++size >= threshold) 
  4.             resize(tab.length * 2); 
  5.         return null; 
  6.     } 

注意new Entry傳遞了一個reference queue到構造函數中,此構造函數最終會調用Reference的構造函數

  1. Reference(T referent, ReferenceQueue<? super T> queue) { 
  2.     this.referent = referent; 
  3.     this.queue = (queue == null) ? ReferenceQueue.NULL : queue; 
  4.     } 

referent是我們之前傳入的hashmap的key對象,queue的作用是用來讀取referent被回收的weak reference,生產者是誰後續介紹,此時WeakHashMap中已經存在了一個對象,先將key對象的strong ref制空並嘗試觸發gc,比如使用System.gc()來顯式的觸發gc,然後調用WeakHashMap的size方法返回集合的個數,絕大多數情況下會是0,這個過程中發生了什麼呢?

第一步,key沒有可達的strong ref,僅僅存在一個weak reference的referent變量仍然指向了key,觸發GC時,以openjdk6的parNew爲例,jvm在young generation gc時會嘗試獲取Reference對象裏的靜態全局鎖

 

  1. /* Object used to synchronize with the garbage collector.  The collector 
  2.      * must acquire this lock at the beginning of each collection cycle.  It is 
  3.      * therefore critical that any code holding this lock complete as quickly 
  4.      * as possible, allocate no new objects, and avoid calling user code. 
  5.      */ 
  6.     static private class Lock { }; 
  7.     private static Lock lock = new Lock(); 

在openjdk6裏的部分源代碼,完整代碼請參考instanceRefKlass.cpp文件

  1. void instanceRefKlass::acquire_pending_list_lock(BasicLock *pending_list_basic_lock) { 
  2.   // we may enter this with pending exception set 
  3.   PRESERVE_EXCEPTION_MARK;  // exceptions are never thrown, needed for TRAPS argument 
  4.   Handle h_lock(THREAD, java_lang_ref_Reference::pending_list_lock()); 
  5.   ObjectSynchronizer::fast_enter(h_lock, pending_list_basic_lock, false, THREAD); 
  6.   assert(ObjectSynchronizer::current_thread_holds_lock( 
  7.            JavaThread::current(), h_lock), 
  8.          "Locking should have succeeded"); 
  9.   if (HAS_PENDING_EXCEPTION) CLEAR_PENDING_EXCEPTION; 

 此處代碼在parNew gc時執行,目的就是嘗試獲取全局鎖,在gc完成後,jvm會將key被回收的weak reference組成一個queue並賦值到Reference的pending屬性然後釋放鎖,參考方法:

  1. void instanceRefKlass::release_and_notify_pending_list_lock( 
  2.   BasicLock *pending_list_basic_lock) { 
  3.   // we may enter this with pending exception set 
  4.   PRESERVE_EXCEPTION_MARK;  // exceptions are never thrown, needed for TRAPS argument 
  5.   // 
  6.   Handle h_lock(THREAD, java_lang_ref_Reference::pending_list_lock()); 
  7.   assert(ObjectSynchronizer::current_thread_holds_lock( 
  8.            JavaThread::current(), h_lock), 
  9.          "Lock should be held"); 
  10.   // Notify waiters on pending lists lock if there is any reference. 
  11.   if (java_lang_ref_Reference::pending_list() != NULL) { 
  12.     ObjectSynchronizer::notifyall(h_lock, THREAD); 
  13.   } 
  14.   ObjectSynchronizer::fast_exit(h_lock(), pending_list_basic_lock, THREAD); 
  15.   if (HAS_PENDING_EXCEPTION) CLEAR_PENDING_EXCEPTION; 

在一次gc後,Reference對象的pending屬性不再爲空,讓我們看看Reference的部分代碼

首先是pending屬性的說明:

  1. /* List of References waiting to be enqueued.  The collector adds 
  2.  * References to this list, while the Reference-handler thread removes 
  3.  * them.  This list is protected by the above lock object. 
  4.  */ 
  5. private static Reference pending = null

接下來是Reference中的內部類ReferenceHandler,它繼承了Thread,看看run方法的代碼

  1. public void run() { 
  2.         for (;;) { 
  3.  
  4.         Reference r; 
  5.         synchronized (lock) { 
  6.             if (pending != null) { 
  7.             r = pending; 
  8.             Reference rn = r.next; 
  9.             pending = (rn == r) ? null : rn; 
  10.             r.next = r; 
  11.             } else { 
  12.             try { 
  13.                 lock.wait(); 
  14.             } catch (InterruptedException x) { } 
  15.             continue
  16.             } 
  17.         } 
  18.  
  19.         // Fast path for cleaners 
  20.         if (r instanceof Cleaner) { 
  21.             ((Cleaner)r).clean(); 
  22.             continue
  23.         } 
  24.  
  25.         ReferenceQueue q = r.queue; 
  26.         if (q != ReferenceQueue.NULL) q.enqueue(r); 
  27.         } 
  28.     } 
  29.     } 

一旦jvm notify了前面提到的鎖,這個線程就被激活並開始執行,作用是將之前jvm賦值過來的pending對象中的WeakReference對象enqueue到指定的隊列中,比如WeakHashMap內部定義的ReferenceQueue屬性

此時map的queue中保存了referent已經被回收的WeakReference隊列,也就是map的Entry對象,當調用size方法時,內部首先調用expungStaleEntries方法清除被回收掉的Entry,代碼如下

 

  1. private void expungeStaleEntries() { 
  2.     Entry<K,V> e; 
  3.         while ( (e = (Entry<K,V>) queue.poll()) != null) { 
  4.             int h = e.hash; 
  5.             int i = indexFor(h, table.length); 
  6.  
  7.             Entry<K,V> prev = table[i]; 
  8.             Entry<K,V> p = prev; 
  9.             while (p != null) { 
  10.                 Entry<K,V> next = p.next; 
  11.                 if (p == e) { 
  12.                     if (prev == e) 
  13.                         table[i] = next; 
  14.                     else 
  15.                         prev.next = next; 
  16.                     e.next = null;  // Help GC 
  17.                     e.value = null//  "   " 
  18.                     size--; 
  19.                     break
  20.                 } 
  21.                 prev = p; 
  22.                 p = next; 
  23.             } 
  24.         } 
  25.     } 

ok,就這樣map的廢棄Entry被clear,size返回爲0

經過簡單的測試程序發現:

一次gc未必能完全回收所有的weak ref

weak對象也可能會出現在old generation

 

參考:

 

http://weblogs.java.net/blog/2006/05/04/understanding-weak-references

 http://stackoverflow.com/questions/154724/when-would-you-use-a-weakhashmap-or-a-weakreference

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章