神奇好望角 The Magical Cape of Good Hope

          庸人不必自擾,智者何需千慮?
          posts - 26, comments - 50, trackbacks - 0, articles - 11
            BlogJava :: 首頁 ::  :: 聯系 :: 聚合  :: 管理

          Java 中對象引用的類型

          Posted on 2007-12-02 20:43 蜀山兆孨龘 閱讀(1269) 評論(0)  編輯  收藏
          Java 中對象引用的類型 Object Reference Types in Java
            弱引用早有耳聞,但從來沒去認真看過。前天改編陳維雷先生的下雪動畫時,發現他使用了弱引用,于是趁機把 Java 的對象引用類型看了個究竟。     I've heard of weak reference for a long time, but have never study it seriously yet. The day before yesterday, when I was modifying Mr. William Chen's snowing animation, I saw weak reference was utilized, and then took the chance to read the details of Java's reference type.
            除了通常意義下的強引用,包 java.lang.ref 還定義了其他三種平時不太用到的引用:軟引用、弱引用和虛引用,但 API 文檔的解釋比較含糊。我在網上搜到了一些資料,簡單歸納一下。     Except the strong reference of common purpose, package java.lang.ref defines three other references which are less often used: soft reference, weak reference and phantom reference, but they have obscure explanations in the API documention. I searched online and got some stuffs and here are my summaries.
            強引用。當一個對象具有強引用時,Java 虛擬機寧愿拋出 OutOfMemeryError,也絕不讓垃圾回收器回收它。     Strong Reference. When an object holds strong references, Java Virtue Machine would rather throw an OutOfMemeryError than let garbage collector (GC) collect it.
            軟引用。當一個對象只具有軟引用時,垃圾回收器只在內存不足的時候才回收它。     Soft Reference. When an object holds only soft references, GC collects it only if there is not enough memory.
            弱引用。當一個對象只具有弱引用時,一旦被垃圾回收器發現就會被回收。因為垃圾回收器是一個優先級很低的線程,所以弱引用對象也不一定會馬上就會被回收。     Weak Reference. When an object holds only weak references, GC collects it as soon as finds it. GC is a thread of very low priority, so a weak reference object may not be collected immediately.
            虛引用。虛引用和對象的生命周期無關。虛引用必須和引用隊列聯合使用,對象將被回收前,其虛引用將被加入到引用隊列。虛引用只是用來監視對象的回收。     Phantom Reference. Phantom reference has nothing to do with the life cycle of an object. Phantom reference must be used together with reference queue, and the object's phantom reference will be added into that reference queue right before collected. Phantom reference is only used to monitor object collecting.
            從以上是否能看出,一個對象不能同時具有軟引用和弱引用?     From above shall we say that an object can't have a soft reference and a weak reference at the same time?

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 梁河县| 呼伦贝尔市| 安阳市| 旌德县| 苍南县| 车险| 北安市| 秀山| 合肥市| 通海县| 白山市| 平远县| 马关县| 谢通门县| 体育| 南皮县| 衡水市| 霍城县| 根河市| 水富县| 衡山县| 双辽市| 苏尼特右旗| 凤山市| 萨嘎县| 石门县| 唐海县| 子长县| 四平市| 家居| 海阳市| 轮台县| 大悟县| 行唐县| 博爱县| 大英县| 天津市| 福鼎市| 德州市| 年辖:市辖区| 邵阳县|