Files
javademos/lanes/src/UnsafeWarning.java
T

22 lines
840 B
Java

import java.lang.reflect.Field;
import sun.misc.Unsafe;
/**
* "sun.misc.Unsafe memory methods deprecated for removal": JEP 471 (23) deprecated them, JEP 498 (24) makes the first
* call print a warning at run time. This allocates and frees 8 bytes off-heap and shows what the JVM says.
* Chapter: docs/14-lanes-21-to-25.md
*/
public class UnsafeWarning {
@SuppressWarnings("removal")
public static void main(String[] args) throws Exception {
System.out.println("java.version = " + System.getProperty("java.version"));
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
Unsafe u = (Unsafe) f.get(null);
long addr = u.allocateMemory(8);
u.putLong(addr, 42L);
System.out.println("read back: " + u.getLong(addr));
u.freeMemory(addr);
}
}