在
Hotspot JVM上,我们能够直接对
内存进行读写操作。该类的
allocateMemory方法用于申请分配内存,
putAddress和
getAddress方法用于对直接内存进行读写。
本文将通过sun.misc.Unsafe给出一个直接读写内存的
例子。
注意:这只是一个例子,只是用来验证通过sun.misc.Unsafe来实现直接读写内存的可能性。但是,这样做并没有安全保证,而且稍微有点疏忽将可能导致JVM崩溃。
Unsafe类的三个方法:allocateMemory,putAddress和getAddress如下:
class="java" name="code">/**
* Fetches a native pointer from a given memory address. If the address is
* zero, or does not point into a block obtained from {@link
* #allocateMemory}, the results are undefined.
*
* <p> If the native pointer is less than bits wide, it is extended as
* an unsigned number to a Java long. The pointer may be indexed by any
* given byte offset, simply by adding that offset (as a simple integer) to
* the long representing the pointer. The number of bytes actually read
* from the target address maybe determined by consulting {@link
* #addressSize}.
*
* @see #allocateMemory
*/
public native long getAddress(long address);
/**
* Stores a native pointer into a given memory address. If the address is
* zero, or does not point into a block obtained from {@link
* #allocateMemory}, the results are undefined.
*
* <p> The number of bytes actually written at the target address maybe
* determined by consulting {@link #addressSize}.
*
* @see #getAddress(long)
*/
public native void putAddress(long address, long x);
/// wrappers for malloc, realloc, free:
/**
* Allocates a new block of native memory, of the given size in bytes. The
* contents of the memory are uninitialized; they will generally be
* garbage. The resulting native pointer will never be zero, and will be
* aligned for all value types. Dispose of this memory by calling {@link
* #freeMemory}, or resize it with {@link #reallocateMemory}.
*
* @throws IllegalArgumentException if the size is negative or too large
* for the native size_t type
*
* @throws OutOfMemoryError if the allocation is refused by the system
*
* @see #getByte(long)
* @see #putByte(long, byte)
*/
public native long allocateMemory(long bytes);
1. long allocateMemory(long bytes)
申请分配内存
2. long getAddress(long address) 和void putAddress(long address, long x)
对直接内存进行读写。
因为Unsafe这个类的访问是受限的,只有rt.jar中的类才能使用Unsafe的功能,它的构造方法是私有的,所以,我们不能通过new来创建实例。但是,可以通过反射的方法来获取Unsafe实例。
下面就是一个直接访问内存的一个例子:
import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class DirectMemoryAccess {
public static void main(String[] args) {
/*
* Unsafe的构造函数是私有的,不能通过new来获得实例。
*
* 通过反射来获取
*/
Unsafe unsafe = null;
Field field = null;
try {
field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
/*
* private static final Unsafe theUnsafe = new Unsafe();
*
* 因为field的修饰符为 private static final,
* 需要将setAccessible设置成true,否则会报java.lang.IllegalAccessException
*/
field.setAccessible(true);
unsafe = (Unsafe) field.get(null);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long oneHundred = 100;
byte size = 1;
/*
* 调用allocateMemory分配内存
*/
long memoryAddress = unsafe.allocateMemory(size);
/*
* 将100写入到内存中
*/
unsafe.putAddress(memoryAddress, oneHundred);
/*
* 内存中读取数据
*/
long readValue = unsafe.getAddress(memoryAddress);
System.out.println("Val : " + readValue);
}
}
输出结果:
Val : 100
如果,想要查阅Unsafe的源代码,请参考下面的链接.
http://www.docjar.com/html/api/sun/misc/Unsafe.java.html