Files
javademos/unsafe-migration/broken/Probe.java
T
2026-11-06 21:43:48 +05:30

30 lines
1.0 KiB
Java

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);
}
}
}