import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import sun.misc.Unsafe;
import java.util.Collections;
import java.lang.reflect.Field;
public static void main(String[] args) {
String a = new String("123"); String b = new String("123");
System.out.println(getAddresses(a));
System.out.println(getAddresses(b));
}
public static String getAddresses(Object... objects)
{
StringBuffer sb = new StringBuffer();
sb.append("0x");
// sun.arch.data.model=32 // 32 bit JVM
// sun.arch.data.model=64 // 64 bit JVM
boolean is64bit = Integer.parseInt(System.getProperty("sun.arch.data.model")) == 32 ? false : true;
Unsafe unsafe = getUnsafe();
long last = 0;
int offset = unsafe.arrayBaseOffset(objects.getClass());
int scale = unsafe.arrayIndexScale(objects.getClass());
switch (scale)
{
case 4:
long factor = is64bit ? 8 : 1;
final long i1 = (unsafe.getInt(objects, offset) & 0xFFFFFFFFL) * factor;
// 输出指针地址
sb.append(Long.toHexString(i1));
last = i1;
for (int i = 1; i < objects.length; i++)
{
final long i2 = (unsafe.getInt(objects, offset + i * 4) & 0xFFFFFFFFL) * factor;
if (i2 > last)
sb.append(", +" + Long.toHexString(i2 - last));
else
sb.append(", -" + Long.toHexString(last - i2));
last = i2;
}
break;
case 8:
throw new AssertionError("Not supported");
}
return sb.toString();
}
private static Unsafe getUnsafe()
{
try
{
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
return (Unsafe) theUnsafe.get(null);
}
catch (Exception e)
{
throw new AssertionError(e);
}
}
如果sun.misc.Unsafe找不到,请参考这篇文章https://www.cnblogs.com/chLxq/p/10788306.html