设为首页 加入收藏

TOP

Java不能操作内存?Unsafe了解一下(二)
2023-08-26 21:11:18 】 浏览:124
Tags:Java 能操作 Unsafe 解一下
blic native void freeMemory(long address)

举个栗子:

public void test() throws Exception {
        Unsafe unsafe = getUnsafe();
        long address = unsafe.allocateMemory(8);
        System.out.println("allocate memory with 8 bytes, address=" + address);
        long data = 13579L;
        unsafe.putLong(address, data);
        System.out.println("direct put data to address, data=" + data);
        System.out.println("get address data=" + unsafe.getLong(address));
        long address1 = unsafe.allocateMemory(8);
        System.out.println("allocate memory with 8 bytes, address1=" + address);
        unsafe.copyMemory(address, address1, 8);
        System.out.println("copy memory with 8 bytes to address1=" + address1);
        System.out.println("get address1 data=" + unsafe.getLong(address1));
        unsafe.reallocateMemory(address1, 16);
        unsafe.setMemory(address1, 16, (byte)20);
        System.out.println("after setMemory address1=" + unsafe.getByte(address1));
        unsafe.freeMemory(address1);
        unsafe.freeMemory(address);
        System.out.println("free memory over");
        long[] l1 = new long[] {11, 22, 33, 44};
        long[] l2 = new long[4];
        long offset = unsafe.arrayBaseOffset(long[].class);
        unsafe.copyMemory(l1, offset, l2, offset, 32);
        System.out.println("l2=" + Arrays.toString(l2));
    }

输出结果:

allocate memory with 8 bytes, address=510790256
direct put data to address, data=13579
get address data=13579
allocate memory with 8 bytes, address1=510790256
copy memory with 8 bytes to address1=510788736
get address1 data=13579
after setMemory address1=20
free memory over
l2=[11, 22, 33, 44]

Unsafe操作类、对象及变量

下面介绍关于类、对象及变量相关的一些操作,还有一些其他的方法没有一一列出,大家可以自行研究。

//实例化对象,不调构造方法
Object allocateInstance(Class<?> cls)
//字段在内存中的地址相对于实例对象内存地址的偏移量
public long objectFieldOffset(Field f)
//字段在内存中的地址相对于class内存地址的偏移量
public long objectFieldOffset(Class<?> c, String name)
//静态字段在class对象中的偏移
public long staticFieldOffset(Field f)
//获得静态字段所对应类对象
public Object staticFieldBase(Field f)
//获取对象中指定偏移量的int值,这里还有基本类型的其他其中,比如char,boolean,long等
public native int getInt(Object o, long offset);
//将int值放入指定对象指定偏移量的位置,这里还有基本类型的其他其中,比如char,boolean,long等
public native void putInt(Object o, long offset, int x);
//获取obj对象指定offset的属性对象
public native Object getObject(Object obj, long offset);
//将newObj对象放入指定obj对象指定offset偏移量的位置
public native void putObject(Object obj, long offset, Object newObj);

实战一下吧

class Cat {
    private String name;
    private long speed;

    public Cat(String name, long speed) {
        this.name = name;
        this.speed = speed;
    }

    public Cat() {
        System.out.println("constructor...");
    }

    static {
        System.out.println("static...");
    }

    @Override
    public String toString() {
        return "Cat{" + "name='" + name + '\'' + ", speed=" + speed + '}';
    }
}

class Foo {
    private int age;
    private Cat cat;
    private static String defaultString = "default........";

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }
}

//测试方法
public void test1() throws Exception {
    // 使用allocateInstance方法创建一个Cat实例,这里不会调用构造方法,但是静态块会执行,所以会输出"static..."
    Cat cat = (Cat)unsafe.allocateInstance(Cat.class);
    System.out.print
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 2/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇10、Spring之AOP概述 下一篇new ArrayList 不当导致 CPU 飙升..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目