2012-11-05

ByteBuffer.allocateDirect で確保したメモリを解放する方法

ByteBuffer.allocateDirect で確保したヒープ外のメモリを解放するには、オブジェクトの参照を全て外してから GC をする必要がある。別の方法としては、下記のページのように sun.misc.Cleaner を呼び出せば解放することができるようだ。
java - Examples of forcing freeing of native memory direct ByteBuffer has allocated, using sun.misc.Unsafe? - Stack Overflow
http://stackoverflow.com/questions/8462200/examples-of-forcing-freeing-of-native-memory-direct-bytebuffer-has-allocated-us
おそらく、Oracle (Sun) の Java のみ有効。

ググってたら、次のようなユーティリティのソースを見つけた。
ByteBufferUtil xref
http://static.netty.io/3.5/xref/org/jboss/netty/util/internal/ByteBufferUtil.html