On the hotspot JVM, we are able to read and write directly to the memory. The Allocatememory method of this class is used to request the allocation of memory, and the Putaddress and GetAddress methods are used to read and write direct memory.
This article will give an example of direct read and write memory through Sun.misc.Unsafe.
Note: This is only an example of the possibility of using Sun.misc.Unsafe to read and write memory directly. However, there is no security guarantee, and a bit of negligence can cause the JVM to crash.
The three methods of the unsafe class: Allocatememory,putaddress and GetAddress are as follows:
Java code
- /**
- * Fetches a native pointer from a given memory address. If the address is
- * Zero, or does not point to a block obtained from {@link
- * #allocateMemory}, the results is undefined.
- *
- * <p> If The native pointer is less than bits wide, it's extended as
- * A unsigned number to a Java long. The pointer is indexed by any
- * Given byte offset, simply by adding this 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 to a block obtained from {@link
- * #allocateMemory}, the results is 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 is uninitialized; They'll generally be
- * Garbage. The resulting native pointer would never be zero, and would 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)
Request to allocate memory
2. Long getaddress (long address) and void putaddress (long address, long X)
Read and write to direct memory.
because the access to this class is restricted, only classes in Rt.jar can use the unsafe function, and its construction method is private, so we cannot create an instance from new. However, an unsafe instance can be obtained by means of reflection.
Here is an example of a direct memory access:
Java code
- Import Java.lang.reflect.Field;
- Import Sun.misc.Unsafe;
- Public class Directmemoryaccess {
- public static void Main (string[] args) {
- /*
- * Unsafe's constructor is private and cannot be obtained by new.
- *
- * Through reflection to get
- */
- unsafe unsafe = null;
- Field field = null;
- try {
- field = Sun.misc.Unsafe. Class.getdeclaredfield ("Theunsafe");
- /*
- * Private static final unsafe Theunsafe = new unsafe ();
- *
- * Because the field modifier is private static final,
- * Setaccessible must be set to true, otherwise it will be reported 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;
- /*
- * Call Allocatememory to allocate memory
- */
- Long memoryaddress = unsafe.allocatememory (size);
- /*
- * Write 100 to Memory
- */
- Unsafe.putaddress (memoryaddress, onehundred);
- /*
- * Read data in memory
- */
- Long readvalue = unsafe.getaddress (memoryaddress);
- System.out.println ("Val:" + readvalue);
- }
- }
Output Result:
val:100
If you would like to consult the source code of unsafe, please refer to the link below.
Http://www.docjar.com/html/api/sun/misc/Unsafe.java.html
Translated from: http://blog.csdn.net/joe_007/article/details/38964407
Java Direct Memory read and write examples