public interface DirectBufferDeallocator
ByteBuffer.allocateDirect(int):
---
This type handles explicit deallocation of memory allocated by direct ByteBuffers created via
ByteBuffer.allocateDirect(int).
The default implementation is a no-op to maintain compatibility accross the Java 8-9 transition.
The required approach for a functional implementation would be:
For JDK 8:
// compensate missing proper typing in JDK
if(!(directByteBuffer instanceof sun.nio.ch.DirectBuffer))
{
return; // or throw exception
}
sun.misc.Cleaner cleaner = ((sun.nio.ch.DirectBuffer)directByteBuffer).cleaner();
// cleaner can be null
if(cleaner != null)
{
cleaner.clean();
}
For JDK 9(+)
// compensate missing proper typing in JDK
if(!(directByteBuffer instanceof sun.nio.ch.DirectBuffer))
{
return; // or throw exception
}
jdk.internal.ref.Cleaner cleaner = ((sun.nio.ch.DirectBuffer)directByteBuffer).cleaner();
// cleaner can be null
if(cleaner != null)
{
cleaner.clean();
}
Also see:
http://stackoverflow.com/questions/8462200/examples-of-forcing-freeing-of-native-memory-direct-bytebuffer-has-allocated-us| Modifier and Type | Interface and Description |
|---|---|
static class |
DirectBufferDeallocator.NoOp |
| Modifier and Type | Method and Description |
|---|---|
boolean |
deallocateDirectBuffer(ByteBuffer directBuffer) |
static DirectBufferDeallocator |
NoOp() |
boolean deallocateDirectBuffer(ByteBuffer directBuffer)
static DirectBufferDeallocator NoOp()
Copyright © 2022 MicroStream Software. All rights reserved.