Il seguente codice attribuisce una grande dimensione di memoria diretta ma non causare java.lang.OutOfMemoryError: memoria buffer diretto:Perché XX: MaxDirectMemorySize non può limitare Unsafe.allocateMemory?
//JVM args: -Xms10m -Xmx10m -XX:MaxDirectMemorySize=10m
public class DirectMemoryOOM {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
Field f = Unsafe.class.getDeclaredFields()[0];
f.setAccessible(true);
Unsafe us = (Unsafe) f.get(null);
long size = 1024 * 1024 * 1024;
while (true) {
long p = us.allocateMemory(size);
for (int i = 0; i < size; i++) {
us.putByte(p + i, Byte.MAX_VALUE);
}
}
}
}
Ma il codice seguente codice otterrà java.lang.OutOfMemoryError: Memoria buffer diretta. Ho visto la risposta da Java unsafe memory allocation limit, ma ByteBuffer.allocateDirect è implementato utilizzando Unsafe.allocateMemory()
//JVM args: -Xms10m -Xmx10m -XX:MaxDirectMemorySize=10m
public class DirectMemoryOOM {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
int size = 1024 * 1024;
System.out.println(sun.misc.VM.maxDirectMemory());
while (true) {
ByteBuffer.allocateDirect(size);
}
}
}
Perché il limite non riescono succede al primo?
Grazie per la risposta! –