獲得java物件的記憶體地址
阿新 • • 發佈:2018-12-17
Java不能直接訪問作業系統底層,而是通過本地方法來訪問。Unsafe類提供了硬體級別的原子操作,在java中記憶體中的物件地址是可變的,所以獲得的記憶體地址有可能會變化。要獲得記憶體地址也只能通過Unsafe的方法來獲得,下面類提供了獲取java物件記憶體地址的方法
package com.example.demo.othertest; import java.lang.reflect.Field; import sun.misc.Unsafe; public class Addresser { private static Unsafe unsafe; static { try { Field field = Unsafe.class.getDeclaredField("theUnsafe"); field.setAccessible(true); unsafe = (Unsafe) field.get(null); } catch (Exception e) { e.printStackTrace(); } } public static long addressOf(Object o) throws Exception { Object[] array = new Object[] { o }; long baseOffset = unsafe.arrayBaseOffset(Object[].class); //arrayBaseOffset方法是一個本地方法,可以獲取陣列第一個元素的偏移地址 int addressSize = unsafe.addressSize(); long objectAddress; switch (addressSize) { case 4: objectAddress = unsafe.getInt(array, baseOffset); //getInt方法獲取物件中offset偏移地址對應的int型field的值 break; case 8: objectAddress = unsafe.getLong(array, baseOffset); //getLong方法獲取物件中offset偏移地址對應的long型field的值 break; default: throw new Error("unsupported address size: " + addressSize); } return (objectAddress); } public static void main(String... args) throws Exception { Object mine = "Hello world".toCharArray(); //先把字串轉化為陣列物件 long address = addressOf(mine); System.out.println("Addess: " + address); // Verify address works - should see the characters in the array in the output printBytes(address, 27); } public static void printBytes(long objectAddress, int num) { for (long i = 0; i < num; i++) { int cur = unsafe.getByte(objectAddress + i); System.out.print((char) cur); } System.out.println(); } }