New posts
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
/**
|
||||
* Smallest possible Unsafe memory access: allocate 8 bytes off-heap, write a long, read it back, free it.
|
||||
* Run it under each value of --sun-misc-unsafe-memory-access to see what the JVM does.
|
||||
*/
|
||||
public class Probe {
|
||||
public static void main(String[] args) {
|
||||
Unsafe u = UnsafeAccess.get();
|
||||
System.out.println("got Unsafe instance");
|
||||
try {
|
||||
long addr = u.allocateMemory(8);
|
||||
u.putLong(addr, 42L);
|
||||
System.out.println("read back " + u.getLong(addr));
|
||||
u.freeMemory(addr);
|
||||
System.out.println("allocate/put/get/free all succeeded");
|
||||
} catch (Throwable t) {
|
||||
System.out.println("FAILED: " + t);
|
||||
}
|
||||
try {
|
||||
long addr2 = u.allocateMemory(8);
|
||||
u.putLong(addr2, 7L);
|
||||
System.out.println("second use read " + u.getLong(addr2));
|
||||
u.freeMemory(addr2);
|
||||
} catch (Throwable t) {
|
||||
System.out.println("second use FAILED: " + t);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user