Java Unsafe 類(lèi)的講解
一、Unsafe類(lèi)是啥?
Java最初被設(shè)計(jì)為一種安全的受控環(huán)境。盡管如此,Java HotSpot還是包含了一個(gè)“后門(mén)”,提供了一些可以直接操控內(nèi)存和線程的低層次操作。這個(gè)后門(mén)類(lèi)——sun.misc.Unsafe——被JDK廣泛用于自己的包中,如java.nio和java.util.concurrent。但是絲毫不建議在生產(chǎn)環(huán)境中使用這個(gè)后門(mén)。因?yàn)檫@個(gè)API十分不安全、不輕便、而且不穩(wěn)定。這個(gè)不安全的類(lèi)提供了一個(gè)觀察HotSpot JVM內(nèi)部結(jié)構(gòu)并且可以對(duì)其進(jìn)行修改。有時(shí)它可以被用來(lái)在不適用C++調(diào)試的情況下學(xué)習(xí)虛擬機(jī)內(nèi)部結(jié)構(gòu),有時(shí)也可以被拿來(lái)做性能監(jiān)控和開(kāi)發(fā)工具。
二、為什么叫Unsafe?
Java官方不推薦使用Unsafe類(lèi),因?yàn)楣俜秸J(rèn)為,這個(gè)類(lèi)別人很難正確使用,非正確使用會(huì)給JVM帶來(lái)致命錯(cuò)誤。而且未來(lái)Java可能封閉丟棄這個(gè)類(lèi)。
三、如何使用Unsafe?
1. 獲取Unsafe實(shí)例
通讀Unsafe源碼,Unsafe提供了一個(gè)私有的靜態(tài)實(shí)例,并且通過(guò)檢查classloader是否為null來(lái)避免java程序直接使用unsafe:
//Unsafe源碼
private static final Unsafe theUnsafe;
@CallerSensitive
public static Unsafe getUnsafe() {
Class var0 = Reflection.getCallerClass();
if(var0.getClassLoader() != null) {
throw new SecurityException("Unsafe");
} else {
return theUnsafe;
}
}
我們可以通過(guò)如下代碼反射獲取Unsafe靜態(tài)類(lèi):
/**
* 獲取Unsafe
*/
Field f = null;
Unsafe unsafe = null;
try {
f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
unsafe = (Unsafe) f.get(null);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
2. 通過(guò)Unsafe分配使用堆外內(nèi)存
C++中有malloc,realloc和free方法來(lái)操作內(nèi)存。在Unsafe類(lèi)中對(duì)應(yīng)為:
//分配var1字節(jié)大小的內(nèi)存,返回起始地址偏移量 public native long allocateMemory(long var1); //重新給var1起始地址的內(nèi)存分配長(zhǎng)度為var3字節(jié)大小的內(nèi)存,返回新的內(nèi)存起始地址偏移量 public native long reallocateMemory(long var1, long var3); //釋放起始地址為var1的內(nèi)存 public native void freeMemory(long var1);
分配內(nèi)存方法還有重分配內(nèi)存方法都是分配的堆外內(nèi)存,返回的是一個(gè)long類(lèi)型的地址偏移量。這個(gè)偏移量在你的Java程序中每塊內(nèi)存都是唯一的。
舉例:
/**
* 在堆外分配一個(gè)byte
*/
long allocatedAddress = unsafe.allocateMemory(1L);
unsafe.putByte(allocatedAddress, (byte) 100);
byte shortValue = unsafe.getByte(allocatedAddress);
System.out.println(new StringBuilder().append("Address:").append(allocatedAddress).append(" Value:").append(shortValue));
/**
* 重新分配一個(gè)long
*/
allocatedAddress = unsafe.reallocateMemory(allocatedAddress, 8L);
unsafe.putLong(allocatedAddress, 1024L);
long longValue = unsafe.getLong(allocatedAddress);
System.out.println(new StringBuilder().append("Address:").append(allocatedAddress).append(" Value:").append(longValue));
/**
* Free掉,這個(gè)數(shù)據(jù)可能臟掉
*/
unsafe.freeMemory(allocatedAddress);
longValue = unsafe.getLong(allocatedAddress);
System.out.println(new StringBuilder().append("Address:").append(allocatedAddress).append(" Value:").append(longValue));
輸出:
Address:46490464 Value:100
Address:46490480 Value:1024
Address:46490480 Value:22
3. 操作類(lèi)對(duì)象
我們可以通過(guò)Unsafe類(lèi)來(lái)操作修改某一field。原理是首先獲取對(duì)象的基址(對(duì)象在內(nèi)存的偏移量起始地址)。之后獲取某個(gè)filed在這個(gè)對(duì)象對(duì)應(yīng)的類(lèi)中的偏移地址,兩者相加修改。
/**
* 獲取類(lèi)的某個(gè)對(duì)象的某個(gè)field偏移地址
*/
try {
f = SampleClass.class.getDeclaredField("i");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
long iFiledAddressShift = unsafe.objectFieldOffset(f);
SampleClass sampleClass = new SampleClass();
//獲取對(duì)象的偏移地址,需要將目標(biāo)對(duì)象設(shè)為輔助數(shù)組的第一個(gè)元素(也是唯一的元素)。由于這是一個(gè)復(fù)雜類(lèi)型元素(不是基本數(shù)據(jù)類(lèi)型),它的地址存儲(chǔ)在數(shù)組的第一個(gè)元素。然后,獲取輔助數(shù)組的基本偏移量。數(shù)組的基本偏移量是指數(shù)組對(duì)象的起始地址與數(shù)組第一個(gè)元素之間的偏移量。
Object helperArray[] = new Object[1];
helperArray[0] = sampleClass;
long baseOffset = unsafe.arrayBaseOffset(Object[].class);
long addressOfSampleClass = unsafe.getLong(helperArray, baseOffset);
int i = unsafe.getInt(addressOfSampleClass + iFiledAddressShift);
System.out.println(new StringBuilder().append(" Field I Address:").append(addressOfSampleClass).append("+").append(iFiledAddressShift).append(" Value:").append(i));
輸出:
Field I Address:3610777760+24 Value:5
4. 線程掛起和恢復(fù)
將一個(gè)線程進(jìn)行掛起是通過(guò)park方法實(shí)現(xiàn)的,調(diào)用 park后,線程將一直阻塞直到超時(shí)或者中斷等條件出現(xiàn)。unpark可以終止一個(gè)掛起的線程,使其恢復(fù)正常。整個(gè)并發(fā)框架中對(duì)線程的掛起操作被封裝在 LockSupport類(lèi)中,LockSupport類(lèi)中有各種版本pack方法,但最終都調(diào)用了Unsafe.park()方法。
public class LockSupport {
public static void unpark(Thread thread) {
if (thread != null)
unsafe.unpark(thread);
}
public static void park(Object blocker) {
Thread t = Thread.currentThread();
setBlocker(t, blocker);
unsafe.park(false, 0L);
setBlocker(t, null);
}
public static void parkNanos(Object blocker, long nanos) {
if (nanos > 0) {
Thread t = Thread.currentThread();
setBlocker(t, blocker);
unsafe.park(false, nanos);
setBlocker(t, null);
}
}
public static void parkUntil(Object blocker, long deadline) {
Thread t = Thread.currentThread();
setBlocker(t, blocker);
unsafe.park(true, deadline);
setBlocker(t, null);
}
public static void park() {
unsafe.park(false, 0L);
}
public static void parkNanos(long nanos) {
if (nanos > 0)
unsafe.park(false, nanos);
}
public static void parkUntil(long deadline) {
unsafe.park(true, deadline);
}
}
5. CAS操作
/** * 比較obj的offset處內(nèi)存位置中的值和期望的值,如果相同則更新。此更新是不可中斷的。 * * @param obj 需要更新的對(duì)象 * @param offset obj中整型field的偏移量 * @param expect 希望field中存在的值 * @param update 如果期望值expect與field的當(dāng)前值相同,設(shè)置filed的值為這個(gè)新值 * @return 如果field的值被更改返回true */ public native boolean compareAndSwapInt(Object obj, long offset, int expect, int update);
6. Clone
如何實(shí)現(xiàn)淺克隆?在clone(){…}方法中調(diào)用super.clone(),對(duì)嗎?這里存在的問(wèn)題是首先你必須繼續(xù)Cloneable接口,并且在所有你需要做淺克隆的對(duì)象中實(shí)現(xiàn)clone()方法,對(duì)于一個(gè)懶懶的程序員來(lái)說(shuō),這個(gè)工作量太大了。
我不推薦上面的做法而是直接使用Unsafe,我們可以?xún)H使用幾行代碼就實(shí)現(xiàn)淺克隆,并且它可以像某些工具類(lèi)一樣用于任意類(lèi)的克隆。
首先,我們需要一個(gè)計(jì)算Object大小的工具類(lèi):
class ObjectInfo {
/**
* Field name
*/
public final String name;
/**
* Field type name
*/
public final String type;
/**
* Field data formatted as string
*/
public final String contents;
/**
* Field offset from the start of parent object
*/
public final int offset;
/**
* Memory occupied by this field
*/
public final int length;
/**
* Offset of the first cell in the array
*/
public final int arrayBase;
/**
* Size of a cell in the array
*/
public final int arrayElementSize;
/**
* Memory occupied by underlying array (shallow), if this is array type
*/
public final int arraySize;
/**
* This object fields
*/
public final List<ObjectInfo> children;
public ObjectInfo(String name, String type, String contents, int offset, int length, int arraySize,
int arrayBase, int arrayElementSize) {
this.name = name;
this.type = type;
this.contents = contents;
this.offset = offset;
this.length = length;
this.arraySize = arraySize;
this.arrayBase = arrayBase;
this.arrayElementSize = arrayElementSize;
children = new ArrayList<ObjectInfo>(1);
}
public void addChild(final ObjectInfo info) {
if (info != null)
children.add(info);
}
/**
* Get the full amount of memory occupied by a given object. This value may be slightly less than
* an actual value because we don't worry about memory alignment - possible padding after the last object field.
* <p/>
* The result is equal to the last field offset + last field length + all array sizes + all child objects deep sizes
*
* @return Deep object size
*/
public long getDeepSize() {
//return length + arraySize + getUnderlyingSize( arraySize != 0 );
return addPaddingSize(arraySize + getUnderlyingSize(arraySize != 0));
}
long size = 0;
private long getUnderlyingSize(final boolean isArray) {
//long size = 0;
for (final ObjectInfo child : children)
size += child.arraySize + child.getUnderlyingSize(child.arraySize != 0);
if (!isArray && !children.isEmpty()) {
int tempSize = children.get(children.size() - 1).offset + children.get(children.size() - 1).length;
size += addPaddingSize(tempSize);
}
return size;
}
private static final class OffsetComparator implements Comparator<ObjectInfo> {
@Override
public int compare(final ObjectInfo o1, final ObjectInfo o2) {
return o1.offset - o2.offset; //safe because offsets are small non-negative numbers
}
}
//sort all children by their offset
public void sort() {
Collections.sort(children, new OffsetComparator());
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
toStringHelper(sb, 0);
return sb.toString();
}
private void toStringHelper(final StringBuilder sb, final int depth) {
depth(sb, depth).append("name=").append(name).append(", type=").append(type)
.append(", contents=").append(contents).append(", offset=").append(offset)
.append(", length=").append(length);
if (arraySize > 0) {
sb.append(", arrayBase=").append(arrayBase);
sb.append(", arrayElemSize=").append(arrayElementSize);
sb.append(", arraySize=").append(arraySize);
}
for (final ObjectInfo child : children) {
sb.append('\n');
child.toStringHelper(sb, depth + 1);
}
}
private StringBuilder depth(final StringBuilder sb, final int depth) {
for (int i = 0; i < depth; ++i)
sb.append("\t");
return sb;
}
private long addPaddingSize(long size) {
if (size % 8 != 0) {
return (size / 8 + 1) * 8;
}
return size;
}
}
class ClassIntrospector {
private static final Unsafe unsafe;
/**
* Size of any Object reference
*/
private static final int objectRefSize;
static {
try {
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
unsafe = (Unsafe) field.get(null);
objectRefSize = unsafe.arrayIndexScale(Object[].class);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Sizes of all primitive values
*/
private static final Map<Class, Integer> primitiveSizes;
static {
primitiveSizes = new HashMap<Class, Integer>(10);
primitiveSizes.put(byte.class, 1);
primitiveSizes.put(char.class, 2);
primitiveSizes.put(int.class, 4);
primitiveSizes.put(long.class, 8);
primitiveSizes.put(float.class, 4);
primitiveSizes.put(double.class, 8);
primitiveSizes.put(boolean.class, 1);
}
/**
* Get object information for any Java object. Do not pass primitives to
* this method because they will boxed and the information you will get will
* be related to a boxed version of your value.
*
* @param obj Object to introspect
* @return Object info
* @throws IllegalAccessException
*/
public ObjectInfo introspect(final Object obj)
throws IllegalAccessException {
try {
return introspect(obj, null);
} finally { // clean visited cache before returning in order to make
// this object reusable
m_visited.clear();
}
}
// we need to keep track of already visited objects in order to support
// cycles in the object graphs
private IdentityHashMap<Object, Boolean> m_visited = new IdentityHashMap<Object, Boolean>(
100);
private ObjectInfo introspect(final Object obj, final Field fld)
throws IllegalAccessException {
// use Field type only if the field contains null. In this case we will
// at least know what's expected to be
// stored in this field. Otherwise, if a field has interface type, we
// won't see what's really stored in it.
// Besides, we should be careful about primitives, because they are
// passed as boxed values in this method
// (first arg is object) - for them we should still rely on the field
// type.
boolean isPrimitive = fld != null && fld.getType().isPrimitive();
boolean isRecursive = false; // will be set to true if we have already
// seen this object
if (!isPrimitive) {
if (m_visited.containsKey(obj))
isRecursive = true;
m_visited.put(obj, true);
}
final Class type = (fld == null || (obj != null && !isPrimitive)) ? obj
.getClass() : fld.getType();
int arraySize = 0;
int baseOffset = 0;
int indexScale = 0;
if (type.isArray() && obj != null) {
baseOffset = unsafe.arrayBaseOffset(type);
indexScale = unsafe.arrayIndexScale(type);
arraySize = baseOffset + indexScale * Array.getLength(obj);
}
final ObjectInfo root;
if (fld == null) {
root = new ObjectInfo("", type.getCanonicalName(), getContents(obj,
type), 0, getShallowSize(type), arraySize, baseOffset,
indexScale);
} else {
final int offset = (int) unsafe.objectFieldOffset(fld);
root = new ObjectInfo(fld.getName(), type.getCanonicalName(),
getContents(obj, type), offset, getShallowSize(type),
arraySize, baseOffset, indexScale);
}
if (!isRecursive && obj != null) {
if (isObjectArray(type)) {
// introspect object arrays
final Object[] ar = (Object[]) obj;
for (final Object item : ar)
if (item != null)
root.addChild(introspect(item, null));
} else {
for (final Field field : getAllFields(type)) {
if ((field.getModifiers() & Modifier.STATIC) != 0) {
continue;
}
field.setAccessible(true);
root.addChild(introspect(field.get(obj), field));
}
}
}
root.sort(); // sort by offset
return root;
}
// get all fields for this class, including all superclasses fields
private static List<Field> getAllFields(final Class type) {
if (type.isPrimitive())
return Collections.emptyList();
Class cur = type;
final List<Field> res = new ArrayList<Field>(10);
while (true) {
Collections.addAll(res, cur.getDeclaredFields());
if (cur == Object.class)
break;
cur = cur.getSuperclass();
}
return res;
}
// check if it is an array of objects. I suspect there must be a more
// API-friendly way to make this check.
private static boolean isObjectArray(final Class type) {
if (!type.isArray())
return false;
if (type == byte[].class || type == boolean[].class
|| type == char[].class || type == short[].class
|| type == int[].class || type == long[].class
|| type == float[].class || type == double[].class)
return false;
return true;
}
// advanced toString logic
private static String getContents(final Object val, final Class type) {
if (val == null)
return "null";
if (type.isArray()) {
if (type == byte[].class)
return Arrays.toString((byte[]) val);
else if (type == boolean[].class)
return Arrays.toString((boolean[]) val);
else if (type == char[].class)
return Arrays.toString((char[]) val);
else if (type == short[].class)
return Arrays.toString((short[]) val);
else if (type == int[].class)
return Arrays.toString((int[]) val);
else if (type == long[].class)
return Arrays.toString((long[]) val);
else if (type == float[].class)
return Arrays.toString((float[]) val);
else if (type == double[].class)
return Arrays.toString((double[]) val);
else
return Arrays.toString((Object[]) val);
}
return val.toString();
}
// obtain a shallow size of a field of given class (primitive or object
// reference size)
private static int getShallowSize(final Class type) {
if (type.isPrimitive()) {
final Integer res = primitiveSizes.get(type);
return res != null ? res : 0;
} else
return objectRefSize;
}
}
我們通過(guò)這兩個(gè)類(lèi)計(jì)算一個(gè)Object的大小,通過(guò)Unsafe的 public native void copyMemory(Object var1, long var2, Object var4, long var5, long var7)方法來(lái)拷貝:
兩個(gè)工具方法:
private static Object helperArray[] = new Object[1];
/**
* 獲取對(duì)象起始位置偏移量
* @param unsafe
* @param object
* @return
*/
public static long getObjectAddress(Unsafe unsafe, Object object){
helperArray[0] = object;
long baseOffset = unsafe.arrayBaseOffset(Object[].class);
return unsafe.getLong(helperArray, baseOffset);
}
private final static ClassIntrospector ci = new ClassIntrospector();
/**
* 獲取Object的大小
* @param object
* @return
*/
public static long getObjectSize(Object object){
ObjectInfo res = null;
try {
res = ci.introspect(object);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return res.getDeepSize();
}
測(cè)試:
SampleClass sampleClass = new SampleClass(); sampleClass.setI(999); sampleClass.setL(999999999L); SampleClass sampleClassCopy = new SampleClass(); long copyAddress = getObjectAddress(unsafe,sampleClassCopy); unsafe.copyMemory(sampleClass, 0, null,copyAddress, getObjectSize(sampleClass)); i = unsafe.getInt(copyAddress + iFiledAddressShift); System.out.println(i); System.out.println(sampleClassCopy.getL());
輸出:
999
999999999
到此這篇關(guān)于 Java Unsafe 類(lèi)的講解的文章就介紹到這了,更多相關(guān) Java Unsafe 類(lèi)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java.lang.String和java.util.NClob互相轉(zhuǎn)換方式
這篇文章主要介紹了java.lang.String和java.util.NClob互相轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Gradle構(gòu)建基本的Web項(xiàng)目結(jié)構(gòu)
這篇文章主要為大家介紹了Gradle創(chuàng)建Web項(xiàng)目基本的框架結(jié)構(gòu)搭建,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
在Docker中部署Spring Boot項(xiàng)目過(guò)程詳解
這篇文章主要介紹了在Docker中部署Spring Boot項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
ReentrantLock從源碼解析Java多線程同步學(xué)習(xí)
這篇文章主要為大家介紹了ReentrantLock從源碼解析Java多線程同步學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Spring?Cloud?中自定義外部化擴(kuò)展機(jī)制原理及實(shí)戰(zhàn)記錄
這篇文章主要介紹了Spring?Cloud?中自定義外部化擴(kuò)展機(jī)制原理及實(shí)戰(zhàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
Spring?AOP手寫(xiě)動(dòng)態(tài)代理代碼實(shí)例
這篇文章主要介紹了Spring?AOP手寫(xiě)動(dòng)態(tài)代理代碼實(shí)例,AOP我們知道,是在不修改源代碼的情況下,為代碼添加一些新功能的技術(shù),通過(guò)動(dòng)態(tài)代理,可以在不修改原始類(lèi)代碼的前提下,對(duì)方法進(jìn)行攔截和增強(qiáng),需要的朋友可以參考下2024-01-01
詳解Spring Cloud Eureka多網(wǎng)卡配置總結(jié)
本篇文章主要介紹了詳解Spring Cloud Eureka多網(wǎng)卡配置總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
SpringBoot 整合 Lettuce Redis的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot 整合 Lettuce Redis的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Spring Boot使用過(guò)濾器Filter過(guò)程解析
這篇文章主要介紹了Spring Boot使用過(guò)濾器Filter過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01

