How can I bind a byte array in Java?

Is there a way to bind a byte array in java, so it never moves / compacts?

I am working on an application that should have zero GCs at runtime, and I want to use primitive byte arrays that are bound to a memory display area. Is there any way to do this or hack my way to it?

+3
source share
4 answers

You can use ByteBuffer / allocateDirect (). This creates a byte buffer that is in the "c" space and does not use heap, so it will not move and can be used effectively with JNI calls.

+6

? ?

, :

  • JVM. , .
  • , , . , , 10 , , . , , , .

EDIT :

, . I.e, - :

/** Byte arrays that you absolutely have to keep. */
public class KeepForever {
    /** Note that I make no claims about thread safety in this simple example. */
    public static byte [] keepMe = new byte[100];
};

// Much later in example code ....

/** This should always succeed.  No worries about garbage collection. */
public void setMe(int index, byte newByte) {
    // Admittedly, this defeats several principles of OOD but it makes 
    // the point about the syntax.
    KeepForever.keepMe[index] = newByte;
}
+5

, , , GC'd. , , .

0

Source: https://habr.com/ru/post/1712360/


All Articles