public class Direct
extends java.lang.Object
Memory allocated for a direct buffer typically lies outside the
garbage-collected Java heap. That memory is freed when the direct
buffer is garbage collected. Unfortunately, garbage collection
normally occurs when insufficient space is available inside
the Java heap. If insufficient space outside the Java heap
is available, then allocation of a new direct buffer may fail with
a OutOfMemoryError
. This error may occur because
direct buffers that are garbage have not yet been collected, perhaps
because plenty of space is available inside the Java heap.
A solution to this problem is to perform garbage collection when
this error occurs. Normally, a OutOfMemoryError
is fatal. However, methods in this class that allocate direct buffers
will catch this error and call System.gc()
before
attempting to allocate the buffer a second time. If that second
attempt fails, then no further attempts are made and the error is
thrown again. There is no guarantee that this solution will work, but
we have not yet seen it fail in tests that repeatedly allocate direct
buffers that quickly become garbage.
Constructor and Description |
---|
Direct() |
Modifier and Type | Method and Description |
---|---|
static java.nio.ByteBuffer |
newByteBuffer(byte[] a)
Returns a new direct byte buffer.
|
static java.nio.ByteBuffer |
newByteBuffer(int capacity)
Returns a new direct byte buffer.
|
static java.nio.DoubleBuffer |
newDoubleBuffer(double[] a)
Returns a new direct double buffer.
|
static java.nio.DoubleBuffer |
newDoubleBuffer(int capacity)
Returns a new direct double buffer.
|
static java.nio.FloatBuffer |
newFloatBuffer(float[] a)
Returns a new direct float buffer.
|
static java.nio.FloatBuffer |
newFloatBuffer(int capacity)
Returns a new direct float buffer.
|
static java.nio.IntBuffer |
newIntBuffer(int capacity)
Returns a new direct int buffer.
|
static java.nio.IntBuffer |
newIntBuffer(int[] a)
Returns a new direct int buffer.
|
static java.nio.LongBuffer |
newLongBuffer(int capacity)
Returns a new direct long buffer.
|
static java.nio.LongBuffer |
newLongBuffer(long[] a)
Returns a new direct long buffer.
|
static java.nio.ShortBuffer |
newShortBuffer(int capacity)
Returns a new direct short buffer.
|
static java.nio.ShortBuffer |
newShortBuffer(short[] a)
Returns a new direct short buffer.
|
public static java.nio.ByteBuffer newByteBuffer(int capacity)
capacity
- the buffer capacity.public static java.nio.ByteBuffer newByteBuffer(byte[] a)
a
- the array.public static java.nio.DoubleBuffer newDoubleBuffer(int capacity)
capacity
- the buffer capacity.public static java.nio.DoubleBuffer newDoubleBuffer(double[] a)
a
- the array.public static java.nio.FloatBuffer newFloatBuffer(int capacity)
capacity
- the buffer capacity.public static java.nio.FloatBuffer newFloatBuffer(float[] a)
a
- the array.public static java.nio.IntBuffer newIntBuffer(int capacity)
capacity
- the buffer capacity.public static java.nio.IntBuffer newIntBuffer(int[] a)
a
- the array.public static java.nio.LongBuffer newLongBuffer(int capacity)
capacity
- the buffer capacity.public static java.nio.LongBuffer newLongBuffer(long[] a)
a
- the array.public static java.nio.ShortBuffer newShortBuffer(int capacity)
capacity
- the buffer capacity.public static java.nio.ShortBuffer newShortBuffer(short[] a)
a
- the array.