【知識點】垃圾回收之引用計數之迴圈引用
阿新 • • 發佈:2019-02-18
引用計數常用來說明垃圾收集的工作方式,但似乎從未被應用與任何一種Java虛擬機器中實現。《java程式設計思想》
關於引用計數法,我們可以先看一段wiki上的描述:
As a collection algorithm, reference counting tracks, for each object, a count of the number of references to it held by other objects. If an object’s reference count reaches zero, the object has become inaccessible, and can be destroyed.
When an object is destroyed, any objects referenced by that object also have their reference counts decreased.
作為一種回收演算法,引用計數法記錄著每一個物件被其它物件所持有的引用數。如果一個物件的引用計數為零,那麼該物件就變成了所謂的不可達物件,亦即可以被回收的。
當一個物件被回收後,被該物件所引用的其它物件的引用計數都應該相應減少。
而所謂的迴圈引用(circular referrence)有是什麼意思呢?舉個簡單的例子:
public class MyObject {
public Object ref = null;
public static void main(String[] args) {
MyObject myObject1 = new MyObject();
MyObject myObject2 = new MyObject();
myObject1.ref = myObject2;
myObject2.ref = myObject1;
myObject1 = null;
myObject2 = null;
}
}