4 java arrays garbage-collection memory-management
有没有办法在java中固定一个字节数组,所以它永远不会被移动/压缩?
我正在开发一个应用程序,它的目的是在运行时有零GC,我想使用固定到内存映射区域的原始字节数组.有没有办法做到这一点或破解我的方式?
您可以使用ByteBuffer/allocateDirect()这将创建一个位于"c"空间的字节缓冲区,并且不使用堆,因此它不会被移动,并且可以有效地与JNI调用一起使用.
真的零垃圾收集?真?
如果是这样,我会建议两种选择之一:
编辑很久以后:
我刚想到您应该考虑静态分配字节数组.即,像这样:
/** 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1922 次 |
| 最近记录: |