Optimization of memory consumption of disk cache.
package com.orientechnologies.common.directmemory;
import com.kenai.jffi.MemoryIO;
+import java.lang.ref.WeakReference;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public final class OPointer {
+
private final long pointer;
private final int size;
- private final ByteBuffer byteBuffer;
+
+ private WeakReference<ByteBuffer> byteBuffer;
private int hash = 0;
OPointer(long pointer, int size) {
this.pointer = pointer;
this.size = size;
- this.byteBuffer =
- MemoryIO.getInstance().newDirectByteBuffer(pointer, size).order(ByteOrder.nativeOrder());
- assert this.byteBuffer.position() == 0;
}
public void clear() {
@@ -23,7 +23,19 @@ public final class OPointer {
}
public ByteBuffer getNativeByteBuffer() {
- return byteBuffer;
+ ByteBuffer buffer;
+ if (byteBuffer == null) {
+ buffer = createNativeBuffer();
+ byteBuffer = new WeakReference<>(buffer);
+ } else {
+ buffer = byteBuffer.get();
+ if (buffer == null) {
+ buffer = createNativeBuffer();
+ byteBuffer = new WeakReference<>(buffer);
+ }
+ }
+
+ return buffer;
}
long getNativePointer() {
@@ -34,14 +46,24 @@ public final class OPointer {
return size;
}
+ private ByteBuffer createNativeBuffer() {
+ return MemoryIO.getInstance().newDirectByteBuffer(pointer, size).order(ByteOrder.nativeOrder());
+ }
+
@Override
public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
OPointer pointer1 = (OPointer) o;
- if (pointer != pointer1.pointer) return false;
+ if (pointer != pointer1.pointer) {
+ return false;
+ }
return size == pointer1.size;
}
