// The interval will be zero if the ref was accessed since the last scavenge/gc. if(interval <= _max_interval) { // 与_max_interval比较 return false; }
return true; }
1 2 3 4 5 6 7 8 9 10
// Capture state (of-the-VM) information needed to evaluate the policy void LRUMaxHeapPolicy::setup() { size_t max_heap = MaxHeapSize; max_heap -= Universe::get_heap_used_at_last_gc(); max_heap /= M;
// SoftReference对象记录两个时间,目的就是为了GC判断是否需要清除 public class SoftReference<T> extends Reference<T> { // 由JVM负责更新的,记录了上一次GC发生的时间。 static private long clock;
// 每次调用 get 方法都会更新,记录了当前Reference最后一次被访问的时间。 private long timestamp;
public SoftReference(T referent) { super(referent); this.timestamp = clock; }
public SoftReference(T referent, ReferenceQueue<? super T> q) { super(referent, q); this.timestamp = clock; }
// 和super.get的逻辑最大的不同,就在于每次调用get都会把上次发生GC的时间,也就是 // clock 更新到 timestamp 中去。 public T get() { T o = super.get(); if (o != null && this.timestamp != clock) this.timestamp = clock; return o; } }
public class PhantomReference<T> extends Reference<T> { // get方法永远是null,所以无法获得referent public T get() { return null; } public PhantomReference(T referent, ReferenceQueue<? super T> q) { super(referent, q); }
public class Cleaner extends PhantomReference<Object>{ // Reference需要Queue,但Cleaner自己管理ref,所以虚构个无用Queue private static final ReferenceQueue<Object> dummyQueue = new ReferenceQueue<>();
// 所有的cleaner都会被加到一个双向链表中去,确保回收前这些Cleaner都是存活的。 static private Cleaner first = null;
// clean方法会调用remove把当前的cleaner从链表中删除。 private static synchronized boolean remove(Cleaner cl) { // If already removed, do nothing if (cl.next == cl) return false;
// Update list if (first == cl) { if (cl.next != null) first = cl.next; else first = cl.prev; } if (cl.next != null) cl.next.prev = cl.prev; if (cl.prev != null) cl.prev.next = cl.next;
// Indicate removal by pointing the cleaner to itself cl.next = cl; cl.prev = cl; return true; }
// 用户自定义的一个Runnable对象, private final Runnable thunk;