Java Reference 개념 정리

3887 단어
Strong Reference,    , java       ,  GC  Root Set       ,        Strong Reference。

Referent,    Weak, Soft, Phantom Reference         referent。              。
Weak Reference,    。   referent,          ,   ,  Weak Reference    , Weak Reference  Root Set   ,  referent  GC  。WR   ,    referent          ,  ReferenceQueue            referent      ,        clean  。(   ReferenceQueue        ,         ReferenceQueue   )

Soft Reference,    。   strong ,          Reference,   JVM Heap   Strong References, Full GC   heap           OOM ,SoftReferences  GC  。SR              cache(  remove)。
     Soft Reference       hashmap  ,    hashmap           heap,       remove。
 Heap    OOM     ,VM      map    Soft Reference.

Phantom Reference,       Reference,          ,    ,           referent 。
 GC        ,     Phantom reference    referent   strong, weak, soft   (   phantom       ,     ),GC   phantom reference    Reference queue。          queue remove/poll  ,  referent GC     。(   ReferenceQueue        ,        ReferenceQueue   )
  ,    ,GC               finalize()  ,        ,       。 Phantom Reference   (enqueue)     finalize ,     。   WeakReference   。WR         。                clean  。

    Phantom Reference  get    null?
  Phantom reference     (    ,           ),             get    referent,        。
package com.kenwublog.reference;
 
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.util.HashMap;
 
public class PhantomReferenceTest {
 
public static void main(String[] args) {
ReferenceQueue referenceQueue = new ReferenceQueue();
Object object = new Object() {
public String toString() {
return "Referenced Object";
}
};
 
Object data = new Object() {
public String toString() {
return "Data";
}
};
 
HashMap map = new HashMap();
Reference reference = null;
System.out.println("Testing PhantomReference.");
reference = new PhantomReference(object, referenceQueue);
 
map.put(reference, data);
 
System.out.println(reference.get()); // null
System.out.println(map.get(reference)); // Data
System.out.println(reference.isEnqueued()); // false
 
System.gc();
System.out.println(reference.get()); // null
System.out.println(map.get(reference)); // Data
System.out.println(reference.isEnqueued()); // false
 
object = null;
data = null;
 
System.gc();
System.out.println(reference.get()); // null
System.out.println(map.get(reference)); // Data
System.out.println(reference.isEnqueued()); // true, because object has been reclaimed.
}
 }



ReferenceQueue     weak, soft, phantom referent GC   ,         。        reference ,        ,phantom reference        ,weak soft   。

    :
GC  referent (phantom     ,finalize ), reference enqueue RQ ,      RQ remove     reference GC     。
remove      ,   referent    (GC   enqueue),remove         ,  referent    ,      referent   reference  。
  ,RQ             poll,            。


  
package com.kenwublog.reference;
 
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.HashMap;
 
public class ReferenceQueueTest {
 
public static void main(String[] args) {
final ReferenceQueue q = new ReferenceQueue();
String str = new String("AK47");
WeakReference wr = new WeakReference(str, q);
 
Thread t = new Thread(){
@Override
public void run() {
try {
Reference reference = q.remove();
System.out.println(reference + " event fired.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t.setDaemon(true);
t.start();
System.out.println("Reference Queue is listening.");
 
str = null; // clear strong reference
System.out.println("Ready to gc");
System.gc();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("wr.get: " + wr.get());
} 
}

좋은 웹페이지 즐겨찾기