Java Unsafe 类允许您按如下方式为对象分配内存,但是使用此方法您将如何在完成时释放分配的内存,因为它不提供内存地址...
Field f = Unsafe.class.getDeclaredField("theUnsafe"); //Internal reference
f.setAccessible(true);
Unsafe unsafe = (Unsafe) f.get(null);
//This creates an instance of player class without any initialization
Player p = (Player) unsafe.allocateInstance(Player.class);
有没有办法从对象引用访问内存地址,也许默认 hashCode 实现返回的整数会起作用,所以你可以这样做......
unsafe.freeMemory(p.hashCode());
好像不太对...