在Hotspot JVM上,我們能夠直接對內存進行讀寫操作。該類的allocateMemory方法用于申請分配內存,putAddress和getAddress方法用于對直接內存進行讀寫。
本文將通過sun.misc.Unsafe給出一個直接讀寫內存的例子。
注意:這只是一個例子,只是用來驗證通過sun.misc.Unsafe來實現直接讀寫內存的可能性。但是,這樣做并沒有安全保證,而且稍微有點疏忽將可能導致JVM崩潰。
Unsafe類的三個方法:allocateMemory,putAddress和getAddress如下:
- /**
- * 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
原文 轉自 :http://blog.csdn.net/joe_007/article/details/38964407
文章列表