使用unsafe获取对象出错

SeaTalks 2016-10-27 02:14:19
我想用unsafe去allocate一块内存,然后在里面创建一个数组对象,然后等到用到的时候再取出来。
我写的Demo如下,但是总是报错,哪位大牛能帮忙解决一个,小弟不胜感激


public class Test {

private static int byteArrayBaseOffset;

private static Unsafe unsafe;

public static void main(String[] args) {
// TODO Auto-generated method stub

try{
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
unsafe = (Unsafe) theUnsafe.get(null);

int[] test1 = new int[5];
test1[0] =1 ;
test1[1] =2;
int[][] helper = new int[1][1];
helper[0] = test1;
@SuppressWarnings("restriction")
int offset = unsafe.arrayBaseOffset(int[][].class);
System.out.println("offset is :"+offset);
long loc = unsafe.getLong(helper,offset);
System.out.println("the address of test1 : " + loc);

long baseAddress = unsafe.allocateMemory(100);
System.out.println("baseAddress is :"+baseAddress);
int headersize = unsafe.arrayBaseOffset(int[].class);

//copy the header of test1 and the first element to allocated memory
for(int i =0;i<headersize+4;i++){
unsafe.putByte(baseAddress+i, unsafe.getByte(test1, i));
}


Object[] array = new Object[1];
int baseOffSet = unsafe.arrayBaseOffset(Object[].class);
System.out.println("baseOffset of a array is :"+baseOffSet);

//put the pointer of test1 into array[0] , it equals to "array[0] = test1;"
unsafe.putLong(array, baseOffSet, loc);
System.out.println("the first element of test1 :"+ ((int[])unsafe.getObject(array, baseOffSet))[0]);

//put the pointer of int array instance which we created in allocated memory into array[0]
unsafe.putLong(array, baseOffSet, baseAddress);
int[] t2 = (int[]) unsafe.getObject(array, baseOffSet);
System.out.println("the first elment of t2:"+t2[0]);

unsafe.freeMemory(baseAddress);
}catch(SecurityException s){
System.out.println("Security Exception occurred,message is :" + s.getMessage());
}catch(NoSuchFieldException n){
System.out.println("NoSuchField Exception occurred,message is :" + n.getMessage());
}catch(IllegalArgumentException i){
System.out.println("Illegal argument Exception occurred,message is :" + i.getMessage());
}catch(IllegalAccessException ia){
System.out.println("Illegal access Exception occurred,message is :" + ia.getMessage());
}
}

}


执行结果如下:
...全文
187 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
SeaTalks 2016-11-01
  • 打赏
  • 举报
回复
是由于Compressed oops的问题引起的 如果有兴趣的可以去看这里
sky_08_06_02 2016-10-28
  • 打赏
  • 举报
回复
SeaTalks 2016-10-27
  • 打赏
  • 举报
回复
引用 4 楼 woshimike 的回复:
jdk 1.8 win7 64位 表示运行正常
换成jdk1.8后可以了,但是jdk1.6和jdk1.7都不行,有谁知道原因吗?
SeaTalks 2016-10-27
  • 打赏
  • 举报
回复
引用 4 楼 woshimike 的回复:
jdk 1.8 win7 64位 表示运行正常
我换成jdk1.8试试
woshimike 2016-10-27
  • 打赏
  • 举报
回复
jdk 1.8 win7 64位 表示运行正常
SeaTalks 2016-10-27
  • 打赏
  • 举报
回复
自己能顶帖吗,试试
SeaTalks 2016-10-27
  • 打赏
  • 举报
回复
引用 1 楼 sinat_32502451 的回复:
是不是环境问题?我的跑出来没错啊。 offset is :12 the address of test1 : 4567018312 baseAddress is :54279528 baseOffset of a array is :12 the first element of test1 :1 the first elment of t2:1
你的是32位的机器吧,好像32位是可以的。但我是64位的
乐之者v 2016-10-27
  • 打赏
  • 举报
回复
是不是环境问题?我的跑出来没错啊。 offset is :12 the address of test1 : 4567018312 baseAddress is :54279528 baseOffset of a array is :12 the first element of test1 :1 the first elment of t2:1

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧