日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

c 与java 反射性能_谈谈Java 反射的快慢

發布時間:2023/11/27 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c 与java 反射性能_谈谈Java 反射的快慢 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【相關學習推薦:java基礎教程】

反射到底是好是壞

說到Java 中的反射,初學者在剛剛接觸到反射的各種高級特性時,往往表示十分興奮,甚至會在一些不需要使用反射的場景中強行使用反射來「炫技」。而經驗較為豐富的長者,看到反射時往往會發出靈魂三問:為什么要用反射?反射不會降低性能么?不用還有什么辦法可以解決這個問題?

那么今天我們就來深入探討下,反射到底對性能有多大影響?順便探討下,反射為什么對性能有影響?

編碼試驗

在我們分析具體原理之前,我們可以通過編寫代碼做實驗得出結論。

反射可能會涉及多種類型的操作,比如生成實例,獲取/設置變量屬性,調用方法等。經過簡單的思考,我們認為生成實例對性能的影響相對其他操作要大一些,所以我們采用生成實例來做試驗。

在如下代碼中,我們定義了一個類 InnerClass,我們測試分別使用new和反射來生成 MAX_TIMES個實例,并打印出耗時時間。public class MainActivity extends AppCompatActivity { private static final String TAG = "MainAc"; private final int MAX_TIMES = 100 * 1000; private InnerClass innerList[]; @Override

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

innerList = new InnerClass[MAX_TIMES]; long startTime = SystemClock.elapsedRealtime(); for (int i=0; i < MAX_TIMES; i++) {

innerList[i] = new InnerClass();

}

Log.e(TAG, "totalTime: " + (SystemClock.elapsedRealtime() - startTime)); long startTime2 = SystemClock.elapsedRealtime(); for (int i=0; i < MAX_TIMES; i++) {

innerList[i] = newInstanceByReflection();

}

Log.e(TAG, "totalTime2: " + (SystemClock.elapsedRealtime() - startTime2));

} public InnerClass newInstanceByReflection() {

Class clazz = InnerClass.class; try { return (InnerClass) clazz.getDeclaredConstructor().newInstance();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InstantiationException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

} return null;

} static class InnerClass {

}

}復制代碼

輸出日志:2020-03-19 22:34:49.738 2151-2151/? E/MainAc: totalTime: 15

2020-03-19 22:34:50.409 2151-2151/? E/MainAc: totalTime2: 670復制代碼

使用反射生成 10萬 個實例,耗時 670ms,明顯高于直接使用 new關鍵字的 15ms,所以反射性能低。別急,這個結論總結的還有點早,我們將要生成的實例總數改為 1000個試試,輸出日志:2020-03-19 22:39:21.287 3641-3641/com.example.myapplication E/MainAc: totalTime: 2

2020-03-19 22:39:21.296 3641-3641/com.example.myapplication E/MainAc: totalTime2: 9復制代碼

使用反射生成 1000 個實例,雖然需要9ms,高于new的 2ms,但是 9ms 和 2ms 的差距本身肉眼不可見,而且通常我們在業務中寫的反射一般來說執行頻率也未必會超過 1000 次,這種場景下,我們還能理直氣壯地說反射性能很低么?

很顯然,不能。

除了代碼執行耗時,我們再看看反射對內存的影響。我們仍然以生成 10萬 個實例為目標,對上述代碼做略微改動,依次只保留 new 方式和反射方式,然后運行程序,觀察內存占用情況。

使用 new 方式

使用反射

對比兩圖,我們可以看到第二張圖中多了很多 Constructor和Class對象實例,這兩部分占用的內存2.7M。因此,我們可以得出結論,反射會產生大量的臨時對象,并且會占用額外內存空間。

刨根問底:反射原理是什么

我們以前面試驗中反射生成實例的代碼為入口。

首先回顧下虛擬機中類的生命周期:加載,連接(驗證,準備,解析),初始化,使用,卸載。在加載的過程 中,虛擬機會把類的字節碼轉換成運行時數據結構,并保存在方法區,在內存中會生成一個代表這個類數據結構的 java.lang.Class 對象,后續訪問這個類的數據結構就可以通過這個 Class 對象來訪問。public InnerClass newInstanceByReflection() { // 獲取虛擬機中 InnerClass 類的 Class 對象

Class clazz = InnerClass.class; try { return (InnerClass) clazz.getDeclaredConstructor().newInstance();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InstantiationException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

} return null;

}復制代碼

代碼中 clazz.getDeclaredConstructor() 用于獲取類中定義的構造方法,由于我們沒有顯式定義構造方法,所以會返回編譯器為我們自己生成的默認無參構造方法。

下面我們看下 getDeclaredConstructor是如何返回構造方法的。以下均以 jdk 1.8代碼為源碼。@CallerSensitivepublic Constructor getDeclaredConstructor(Class>... parameterTypes)

throws NoSuchMethodException, SecurityException { // 權限檢查

checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); return getConstructor0(parameterTypes, Member.DECLARED);

}復制代碼

getDeclaredConstructor 方法首先做了權限檢查,然后直接調用 getConstructor0 方法。private Constructor getConstructor0(Class>[] parameterTypes, int which) throws NoSuchMethodException{ // privateGetDeclaredConstructors 方法是獲取所有的構造方法數組

Constructor[] constructors = privateGetDeclaredConstructors((which == Member.PUBLIC)); // 遍歷所有的構造方法數組,根據傳入的參數類型依次匹配,找到合適的構造方法后就會拷貝一份作為返回值

for (Constructor constructor : constructors) { if (arrayContentsEq(parameterTypes,

constructor.getParameterTypes())) { // 拷貝構造方法

return getReflectionFactory().copyConstructor(constructor);

}

} // 沒有找到的話,就拋出異常

throw new NoSuchMethodException(getName() + "." + argumentTypesToString(parameterTypes));

}復制代碼

getConstructor0 方法主要做了兩件事:獲取所有構造方法組成的數組

遍歷構造方法數組,找到匹配的

遍歷匹配沒啥好說的,我們重點看下第一件事,怎么獲取的所有構造方法數組,也就是這個方法 privateGetDeclaredConstructors。private Constructor[] privateGetDeclaredConstructors(boolean publicOnly) {

checkInitted();

Constructor[] res; // 獲取緩存的 ReflectionData 數據

ReflectionData rd = reflectionData(); // 如果緩存中有 ReflectionData,就先看看 ReflectionData 中的 publicConstructors 或 declaredConstructors是否為空

if (rd != null) {

res = publicOnly ? rd.publicConstructors : rd.declaredConstructors; if (res != null) return res;

} // 如果沒有緩存,或者緩存中構造方法數組為空

// No cached value available; request value from VM

// 對接口類型的字節碼特殊處理

if (isInterface()) { @SuppressWarnings("unchecked") // 如果是接口類型,那么生成一個長度為0的構造方法數組

Constructor[] temporaryRes = (Constructor[]) new Constructor>[0];

res = temporaryRes;

} else { // 如果不是接口類型,就調用 getDeclaredConstructors0 獲取構造方法數組

res = getDeclaredConstructors0(publicOnly);

} // 獲取到構造方法數組后,再賦值給緩存 ReflectionData 中的對應屬性

if (rd != null) { if (publicOnly) {

rd.publicConstructors = res;

} else {

rd.declaredConstructors = res;

}

} return res;

}復制代碼

上述代碼中我已經對關鍵代碼進行了注釋,在講解整個流程之前,我們看到了一個陌生的類型 ReflectionData。它對應的數據結構是:private static class ReflectionData { volatile Field[] declaredFields; volatile Field[] publicFields; volatile Method[] declaredMethods; volatile Method[] publicMethods; volatile Constructor[] declaredConstructors; volatile Constructor[] publicConstructors; // Intermediate results for getFields and getMethods

volatile Field[] declaredPublicFields; volatile Method[] declaredPublicMethods; volatile Class>[] interfaces; // Value of classRedefinedCount when we created this ReflectionData instance

final int redefinedCount;

ReflectionData(int redefinedCount) { this.redefinedCount = redefinedCount;

}

}復制代碼

ReflectionData 這個類就是用來保存從虛擬機中獲取到的一些數據。同時我們可以看到所有反射屬性都使用了 volatile關鍵字修飾。

獲取緩存的 ReflectionData 數據是通過調用reflectionData()方法獲取的。// 定義在 Class 類中的反射緩存對象private volatile transient SoftReference> reflectionData;private ReflectionData reflectionData() {

SoftReference> reflectionData = this.reflectionData; int classRedefinedCount = this.classRedefinedCount;

ReflectionData rd; if (useCaches &&

reflectionData != null &&

(rd = reflectionData.get()) != null &&

rd.redefinedCount == classRedefinedCount) { return rd;

} // else no SoftReference or cleared SoftReference or stale ReflectionData

// -> create and replace new instance

return newReflectionData(reflectionData, classRedefinedCount);

}復制代碼

我們可以看到 reflectionData實際上是一個軟引用,軟引用會在內存不足的情況下被虛擬機回收,所以reflectionData()方法在開始的地方,先判斷了是否可以使用緩存以及緩存是否失效,如果失效了,就會調用 newReflectionData方法生成一個新的 ReflectionData 實例。

接下來看看 newReflectionData 方法。private ReflectionData newReflectionData(SoftReference> oldReflectionData, int classRedefinedCount) { // 如果不允許使用緩存,直接返回 null

if (!useCaches) return null;

while (true) {

ReflectionData rd = new ReflectionData<>(classRedefinedCount); // try to CAS it...

if (Atomic.casReflectionData(this, oldReflectionData, new SoftReference<>(rd))) { return rd;

} // else retry

oldReflectionData = this.reflectionData;

classRedefinedCount = this.classRedefinedCount; if (oldReflectionData != null &&

(rd = oldReflectionData.get()) != null &&

rd.redefinedCount == classRedefinedCount) { return rd;

}

}

}復制代碼

newReflectionData中使用 volatile + 死循環 + CAS 機制 保證線程安全。注意到這里的死循環每執行一次都會構造一個新的 ReflectionData 實例。

你可能會有疑問,Class 中 reflectionData屬性什么時候被賦值的,其實是封裝在Atomic.casReflectionData這個方法里了,他會檢測當前Class對象中的reflectionData是否與oldReflectionData相等,如果相等,就會把new SoftReference<>(rd)賦值給 reflectionData。

到現在為止,關于 ReflectionData的背景知識都介紹完了。我們再回到 privateGetDeclaredConstructors中看看獲取構造方法的流程。

privateGetDeclaredConstructors流程圖

可以看到對于普通類,最終通過調用 getDeclaredConstructors0方法獲取的構造方法列表。private native Constructor[] getDeclaredConstructors0(boolean publicOnly);復制代碼

這個方法是 native 的,具體邏輯在 jdk 源碼中。

在 native/java/lang/Class_getDeclaredConstructors0.c 文件中,void getDeclaredConstructors0(Frame * frame){ // Frame 可以理解為調用native方法時,java層傳遞過來的數據的一種封裝

LocalVars * vars = frame->localVars;

Object * classObj = getLocalVarsThis(vars); // 取得java方法的入參

bool publicOnly = getLocalVarsBoolean(vars, 1);uint16_t constructorsCount = 0; // 獲取要查詢的類的 Class 對象

Class * c = classObj->extra; // 獲取這個類的所有構造方法,且數量保存在 constructorsCount 中

Method* * constructors = getClassConstructors(c, publicOnly, &constructorsCount);// 獲取 java 方法調用所屬的 classLoader

ClassLoader * classLoader = frame->method->classMember.attachClass->classLoader;// 拿到 Constructor 對應的 class 對象

Class * constructorClass = loadClass(classLoader, "java/lang/reflect/Constructor"); //創建一個長度為 constructorsCount 的數組保存構造方法

Object * constructorArr = newArray(arrayClass(constructorClass), constructorsCount);

pushOperandRef(frame->operandStack, constructorArr);// 后面是具體的賦值邏輯。將native中的Method對象轉化為java層的Constructor對象

if (constructorsCount > 0)

{

Thread * thread = frame->thread;

Object* * constructorObjs = getObjectRefs(constructorArr);

Method * constructorInitMethod = getClassConstructor(constructorClass, _constructorConstructorDescriptor);for (uint16_t i = 0; i < constructorsCount; i++)

{

Method * constructor = constructors[i];

Object * constructorObj = newObject(constructorClass);

constructorObj->extra = constructor;

constructorObjs[i] = constructorObj;

OperandStack * ops = newOperandStack(9);

pushOperandRef(ops, constructorObj);

pushOperandRef(ops, classObj);

pushOperandRef(ops, toClassArr(classLoader, methodParameterTypes(constructor), constructor->parsedDescriptor->parameterTypesCount));if (constructor->exceptions != NULL)

pushOperandRef(ops, toClassArr(classLoader, methodExceptionTypes(constructor), constructor->exceptions->number_of_exceptions));else

pushOperandRef(ops, toClassArr(classLoader, methodExceptionTypes(constructor), 0));

pushOperandInt(ops, constructor->classMember.accessFlags);

pushOperandInt(ops, 0);

pushOperandRef(ops, getSignatureStr(classLoader, constructor->classMember.signature)); // signature

pushOperandRef(ops, toByteArr(classLoader, constructor->classMember.annotationData, constructor->classMember.annotationDataLen));

pushOperandRef(ops, toByteArr(classLoader, constructor->parameterAnnotationData, constructor->parameterAnnotationDataLen));

Frame * shimFrame = newShimFrame(thread, ops);

pushThreadFrame(thread, shimFrame);// init constructorObj

InvokeMethod(shimFrame, constructorInitMethod);

}

}

}復制代碼

從上面的邏輯,可以知道獲取構造方法的核心方法是 getClassConstructors ,所在文件為 rtda/heap/class.c。Method* * getClassConstructors(Class * self, bool publicOnly, uint16_t * constructorsCount){ // 分配大小為 sizeof(Method) 的長度為 methodsCount 的連續內存地址,即數組

Method* * constructors = calloc(self->methodsCount, sizeof(Method));

*constructorsCount = 0; // 在native 層,構造方法和普通方法都存在 methods 中,逐一遍歷

for (uint16_t i = 0; i < self->methodsCount; i++)

{

Method * method = self->methods + i; // 判斷是否是構造方法

if (isMethodConstructor(method))

{ // 檢查權限

if (!publicOnly || isMethodPublic(method))

{ // 符合條件的構造方法依次存到數組中

constructors[*constructorsCount] = method;

(*constructorsCount)++;

}

}

}return constructors;

}復制代碼

可以看到getClassConstructors實際上就是對 methods 進行了一次過濾,過濾的條件為:1.是構造方法;2.權限一致。

isMethodConstructor 方法的判斷邏輯也是十分簡單,不是靜態方法,而且方法名是即可。bool isMethodConstructor(Method * self){return !isMethodStatic(self) && strcmp(self->classMember.name, "") == 0;

}復制代碼

所以核心的邏輯變成了Class中的 methods數組何時被初始化賦值的?我們刨根問底的追蹤下。

我們先找到類加載到虛擬機中的入口方法 loadNonArrayClass:Class * loadNonArrayClass(ClassLoader * classLoader, const char * className){int32_t classSize = 0;char * classContent = NULL;

Class * loadClass = NULL;

classSize = readClass(className, &classContent);if (classSize > 0 && classContent != NULL){#if 0

printf("class size:%d,class data:[", classSize);for (int32_t i = 0; i < classSize; i++)

{printf("0x%02x ", classContent[i]);

}printf("]\n");#endif

}if (classSize <= 0)

{printf("Could not found target class\n");exit(127);

}// 解析字節碼文件

loadClass = parseClassFile(classContent, classSize);

loadClass->classLoader = classLoader;// 加載

defineClass(classLoader, loadClass);// 鏈接

linkClass(classLoader, loadClass);//printf("[Loaded %s\n", loadClass->name);

return loadClass;

}復制代碼

在 parseClassFile方法中,調用了newClass方法。Class * parseClassFile(char * classContent, int32_t classSize){

ClassFile * classFile = NULL;

classFile = parseClassData(classContent, classSize);return newClass(classFile);

}復制代碼

newClass方法在rtda/heap/class.c文件中。Class * newClass(ClassFile * classFile){

Class * c = calloc(1, sizeof(Class));

c->accessFlags = classFile->accessFlags;

c->sourceFile = getClassSourceFileName(classFile);

newClassName(c, classFile);

newSuperClassName(c, classFile);

newInterfacesName(c, classFile);

newConstantPool(c, classFile);

newFields(c, classFile);

newMethods(c, classFile);return c;

}復制代碼

可以看到,在native層創建了一個Class對象,我們重點看newMethods(c, classFile)方法啊,這個方法定義在rtda/heap/method.c中。Method * newMethods(struct Class * c, ClassFile * classFile){

c->methodsCount = classFile->methodsCount;

c->methods = NULL;if (c->methodsCount == 0)return NULL;

c->methods = calloc(classFile->methodsCount, sizeof(Method));for (uint16_t i = 0; i < c->methodsCount; i++)

{

c->methods[i].classMember.attachClass = c;

copyMethodInfo(&c->methods[i], &classFile->methods[i], classFile);

copyAttributes(&c->methods[i], &classFile->methods[i], classFile);

MethodDescriptor * md = parseMethodDescriptor(c->methods[i].classMember.descriptor);

c->methods[i].parsedDescriptor = md;

calcArgSlotCount(&c->methods[i]);if (isMethodNative(&c->methods[i]))

{

injectCodeAttribute(&c->methods[i], md->returnType);

}

}

return NULL;

}復制代碼

上述代碼可以看出,實際上就是把ClassFile中解析到的方法逐一賦值給了 Class 對象的 methods 數組。

總算梳理清楚了,反射創建對象的調用鏈為:loadClass -> loadNonArrayClass -> parseClassFile -> newMethods -> Class 的 methods數組

privateGetDeclaredConstructors -> getDeclaredConstructors0 -> getClassConstructors (過濾Class 的 methods數組)復制代碼

到目前為止,我們搞明白反射時如何找到對應的構造方法的。下面我們來看 newInstance 方法。(InnerClass) clazz.getDeclaredConstructor().newInstance();復制代碼public T newInstance(Object ... initargs)

throws InstantiationException, IllegalAccessException,

IllegalArgumentException, InvocationTargetException { // 構造方法是否被重載了

if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {

Class> caller = Reflection.getCallerClass(); // 檢查權限

checkAccess(caller, clazz, null, modifiers);

}

} // 枚舉類型報錯

if ((clazz.getModifiers() & Modifier.ENUM) != 0) throw new IllegalArgumentException("Cannot reflectively create enum objects"); // ConstructorAccessor 是緩存的,如果為空,就去創建一個

ConstructorAccessor ca = constructorAccessor; // read volatile

if (ca == null) { // 創建 ConstructorAccessor

ca = acquireConstructorAccessor();

} @SuppressWarnings("unchecked") // 使用 ConstructorAccessor 的 newInstance 構造實例

T inst = (T) ca.newInstance(initargs); return inst;

}復制代碼

接著看下 acquireConstructorAccessor 方法。private ConstructorAccessor acquireConstructorAccessor() { // First check to see if one has been created yet, and take it

// if so.

ConstructorAccessor tmp = null; // 可以理解為緩存的對象

if (root != null) tmp = root.getConstructorAccessor(); if (tmp != null) {

constructorAccessor = tmp;

} else { // Otherwise fabricate one and propagate it up to the root

// 生成一個 ConstructorAccessor,并緩存起來

tmp = reflectionFactory.newConstructorAccessor(this);

setConstructorAccessor(tmp);

} return tmp;

}復制代碼

繼續走到newConstructorAccessor方法。public ConstructorAccessor newConstructorAccessor(Constructor> var1) {

checkInitted();

Class var2 = var1.getDeclaringClass(); // 如果是抽象類,報錯

if (Modifier.isAbstract(var2.getModifiers())) { return new InstantiationExceptionConstructorAccessorImpl((String)null);

}

// 如果 Class 類報錯

else if (var2 == Class.class) { return new InstantiationExceptionConstructorAccessorImpl("Can not instantiate java.lang.Class");

}

// 如果是 ConstructorAccessorImpl 的子類的話,返回 BootstrapConstructorAccessorImpl

else if (Reflection.isSubclassOf(var2, ConstructorAccessorImpl.class)) { return new BootstrapConstructorAccessorImpl(var1);

}

// 判斷 noInflation , 后面是判斷不是匿名類

else if (noInflation && !ReflectUtil.isVMAnonymousClass(var1.getDeclaringClass())) { return (new MethodAccessorGenerator()).generateConstructor(var1.getDeclaringClass(), var1.getParameterTypes(), var1.getExceptionTypes(), var1.getModifiers());

}

// 使用 NativeConstructorAccessorImpl 來生成實例

else {

NativeConstructorAccessorImpl var3 = new NativeConstructorAccessorImpl(var1);

DelegatingConstructorAccessorImpl var4 = new DelegatingConstructorAccessorImpl(var3);

var3.setParent(var4); return var4;

}

}復制代碼

具體邏輯,在上述代碼中已經注釋了。這里提一下 noInflation。

ReflectionFactory在執行所有方法前會檢查下是否執行過了checkInitted方法,這個方法會把noInflation的值和inflationThreshold從虛擬機的環境變量中讀取出來并賦值。

當noInflation 為 false而且不是匿名類時,就會使用MethodAccessorGenerator方式。否則就是用 NativeConstructorAccessorImpl的方式來生成。

默認noInflation 為false,所以我們先看native調用的方式。關注 NativeConstructorAccessorImpl類。class NativeConstructorAccessorImpl extends ConstructorAccessorImpl { private final Constructor> c; private DelegatingConstructorAccessorImpl parent; private int numInvocations;

NativeConstructorAccessorImpl(Constructor> var1) { this.c = var1;

} public Object newInstance(Object[] var1) throws InstantiationException, IllegalArgumentException, InvocationTargetException { if (++this.numInvocations > ReflectionFactory.inflationThreshold() && !ReflectUtil.isVMAnonymousClass(this.c.getDeclaringClass())) {

ConstructorAccessorImpl var2 = (ConstructorAccessorImpl)(new MethodAccessorGenerator()).generateConstructor(this.c.getDeclaringClass(), this.c.getParameterTypes(), this.c.getExceptionTypes(), this.c.getModifiers()); this.parent.setDelegate(var2);

} return newInstance0(this.c, var1);

} void setParent(DelegatingConstructorAccessorImpl var1) { this.parent = var1;

} private static native Object newInstance0(Constructor> var0, Object[] var1) throws InstantiationException, IllegalArgumentException, InvocationTargetException;

}復制代碼

我們可以看到 NativeConstructorAccessorImpl 中維護了一個計數器numInvocations,在每次調用newInstance方法生成實例時,就會對計數器自增,當計數器超過ReflectionFactory.inflationThreshold()的閾值,默認為15,就會使用 ConstructorAccessorImpl替換 NativeConstructorAccessorImpl,后面就會直接調用MethodAccessorGenerator中的方法了。

我們先看看沒到達閾值前,會調用native方法 newInstance0,這個方法定義在native/sun/reflect/NativeConstructorAccessorImpl.c中,具體newInstance0的流程我就不分析了,大致邏輯是操作堆棧執行方法。

然后我們再看看超過閾值后,執行的是 MethodAccessorGenerator生成構造器的方式。這種方式與newConstructorAccessor方法中noInflation 為 false的處理方式一樣。所以可以解釋為:java虛擬機在執行反射操作時,如果同一操作執行次數超過閾值,會從native生成實例的方式轉變為java生成實例的方式。

MethodAccessorGenerator的MethodAccessorGenerator方法如下。public ConstructorAccessor generateConstructor(Class> var1, Class>[] var2, Class>[] var3, int var4) { return (ConstructorAccessor)this.generate(var1, "", var2, Void.TYPE, var3, var4, true, false, (Class)null);

}復制代碼

繼續跟蹤下去可以發現,反射調用構造方法實際上是動態編寫字節碼,并且在虛擬機中把編好的字節碼加載成一個Class,這個Class實際上是 ConstructorAccessorImpl 類型的,然后調用這個動態類的newInstance方法?;乜磩倓偽覀兪崂淼膎ewConstructorAccessor代碼,可以看到第三個邏輯:// 如果是 ConstructorAccessorImpl 的子類的話,返回 BootstrapConstructorAccessorImpl else if (Reflection.isSubclassOf(var2, ConstructorAccessorImpl.class)) { return new BootstrapConstructorAccessorImpl(var1);

}

復制代碼

最終執行的是 BootstrapConstructorAccessorImpl的newInstance方法。class BootstrapConstructorAccessorImpl extends ConstructorAccessorImpl { private final Constructor> constructor;

BootstrapConstructorAccessorImpl(Constructor> var1) { this.constructor = var1;

} public Object newInstance(Object[] var1) throws IllegalArgumentException, InvocationTargetException { try { return UnsafeFieldAccessorImpl.unsafe.allocateInstance(this.constructor.getDeclaringClass());

} catch (InstantiationException var3) { throw new InvocationTargetException(var3);

}

}

}復制代碼

最后是通過使用Unsafe類分配了一個實例。

反射帶來的問題

到現在為止,我們已經把反射生成實例的所有流程都搞清楚了。回到文章開頭的問題,我們現在反思下,反射性能低么?為什么?反射調用過程中會產生大量的臨時對象,這些對象會占用內存,可能會導致頻繁 gc,從而影響性能。

反射調用方法時會從方法數組中遍歷查找,并且會檢查可見性等操作會耗時。

反射在達到一定次數時,會動態編寫字節碼并加載到內存中,這個字節碼沒有經過編譯器優化,也不能享受JIT優化。

反射一般會涉及自動裝箱/拆箱和類型轉換,都會帶來一定的資源開銷。

在Android中,我們可以在某些情況下對反射進行優化。舉個例子,EventBus 2.x 會在 register 方法運行時,遍歷所有方法找到回調方法;而EventBus 3.x 則在編譯期間,將所有回調方法的信息保存的自己定義的 SubscriberMethodInfo 中,這樣可以減少對運行時的性能影響。想了解更多相關學習,敬請關注php培訓欄目!

總結

以上是生活随笔為你收集整理的c 与java 反射性能_谈谈Java 反射的快慢的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。