moving objects off heap

梦想的初衷 提交于 2019-12-03 12:30:17

问题


Did anyone try to move java objects off heap? I tried using serializing, deserializing and storing byte arrays by using Unsafe libraries. But complex objects with multiple objects in it making this a tedious process. Any better solutions?


回答1:


The unsafe field is your instance of Unsafe. If you don't know how to get it, simply use:

Unsafe unsafe = getUnsafe();
Unsafe getUnsafe() {
    try {
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        return (Unsafe) f.get(null);
    } catch (Exception x) {
        x.printStackTrace();
    }

    return null;
}

Keep in mind this should work for HotSpot JVM since the field name differs across different implementations.

You can move objects off the heap by:

1) Allocate the proper bytes to the memory address. You can read the int at the offset 4L, convert to unsigned long, and add 12L, and get the address at that location.

An example implementation:

public static long sizeOf(Object object) {
    return unsafe.getAddress( normalize( unsafe.getInt(object, 4L) ) + 12L );
}

public static long normalize(int value) {
   if(value >= 0) return value;
   return (~0L >>> 32) & value;
}

You can allocate the offheap space using:

Object object = // Your object
long size = sizeOf(object);
long address = unsafe.allocateMemory(size);

(Keep the address field handy)

2) Copy the object into the memory section specified by the address .

Example:

getUnsafe().copyMemory(
            object,
            0,
            null,
            address,
            size);

Done! Now your object is offheap.

To get your object back, simply execute following:

// Class wide field
Object anchor;

// Deserializarion
long offset = unsafe.objectFieldOffset(this.getClass().getDeclaredField("anchor"));

unsafe.putLong(this, offset, address);

Then, anchor should be your Object from offheap!

NOTE: Remember to release the resources of the allocated memory when you finish by doing:

unsafe.freeMemory(address);

(See, told you the address was important [as if it wasn't obvious already])

Source(s): http://highlyscalable.wordpress.com/2012/02/02/direct-memory-access-in-java/, a great guy named RawCode which I don't believe is here, but told me about how Unsafe works a while ago.




回答2:


There are some libraries which make this process at least less a fuss, at most store and more objects more efficiently to/from off-heap memory. Chronicle Values and Slab come to my mind first, but I remember there are some more.



来源:https://stackoverflow.com/questions/25257400/moving-objects-off-heap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!