DirectByteBuffer 的释放
当直接内存使用完毕,DirectByteBuffer是可以被JVM回收的,但是通过它分配到的堆外内存却不能被JVM回收。那怎么办呢,如果有大量的堆外内存回收不了,就会造成内存泄漏。
那么这块直接内存是如何被回收的呢?
从清理器Cleaner入手:
1、从Cleaner的实现得知它是一个虚引用,在从虚引用的定义得知,虚引用必须与一个引用队列相关联,在jvm回收一个对象,发现它有虚引用时,就会把这个对象放入到引用队列中,做一系列操作。
sun.misc.Cleanser
public class Cleaner extends PhantomReference<Object> { // Dummy reference queue, needed because the PhantomReference constructor // insists that we pass a queue. Nothing will ever be placed on this queue // since the reference handler invokes cleaners explicitly. // private static final ReferenceQueue<Object> dummyQueue = new ReferenceQueue<>(); // Doubly-linked list of live cleaners, which prevents the cleaners // themselves from being GC'd before their referents // static private Cleaner first = null; private Cleaner next = null, prev = null; private static synchronized Cleaner add(Cleaner cl) { if (first != null) { cl.next = first; first.prev = cl; } first = cl; return cl; } 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; } private final Runnable thunk; private Cleaner(Object referent, Runnable thunk) { super(referent, dummyQueue); this.thunk = thunk; } /** * Creates a new cleaner. * * @param ob the referent object to be cleaned * @param thunk * The cleanup code to be run when the cleaner is invoked. The * cleanup code is run directly from the reference-handler thread, * so it should be as simple and straightforward as possible. * * @return The new cleaner */ public static Cleaner create(Object ob, Runnable thunk) { if (thunk == null) return null; return add(new Cleaner(ob, thunk)); } /** * Runs this cleaner, if it has not been run before. */ public void clean() { if (!remove(this)) return; try { thunk.run(); } catch (final Throwable x) { AccessController.doPrivileged(new PrivilegedAction<Void>() { public Void run() { if (System.err != null) new Error("Cleaner terminated abnormally", x) .printStackTrace(); System.exit(1); return null; }}); } } }
2. 虚引用的子类型.对象构造时传入一个Runnable
用于在引用处理线程执行时调用.
所以Cleanser在构造的时候会传进去一个runnable实现类 Deallocator
Cleaner是PhantomReference的子类,并通过自身的next和prev字段维护的一个双向链表。PhantomReference的作用在于跟踪垃圾回收过程,并不会对对象的垃圾回收过程造成任何的影响。
所以cleaner = Cleaner.create(this, new Deallocator(base, size, cap)); 用于对当前构造的DirectByteBuffer对象的垃圾回收过程进行跟踪。
当DirectByteBuffer对象从pending状态 ——> enqueue状态时,会触发Cleaner的clean(),而Cleaner的clean()的方法会实现通过unsafe对堆外内存的释放。
private static class Deallocator implements Runnable { private static Unsafe unsafe = Unsafe.getUnsafe(); private long address; private long size; private int capacity; private Deallocator(long address, long size, int capacity) { assert (address != 0); this.address = address; this.size = size; this.capacity = capacity; } public void run() { if (address == 0) { // Paranoia return; } unsafe.freeMemory(address); address = 0; Bits.unreserveMemory(size, capacity); } }
在网上还摘抄到这么一段
如果在程序中创建了DirectByteBuffer类实例A,当A 自身没有被引用时,在触发GC回收之前,jvm把A放在PhantomReference队列里,同时不断扫描PhantomReference队列,取出A,触发new Deallocator里的run方法回收堆外直接内存,同时回收A自身的堆内存.