Java:JITC的反映通胀是什么? [英] Java: what is JITC's reflection inflation?

查看:152
本文介绍了Java:JITC的反映通胀是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了这个有趣的术语,并在网上搜索了解更多信息。然而,我发现的信息是粗略的。有人可以吗。给我一个详细的解释,这是什么,为什么这有用?

I recently came across this interesting term and searched on Net to know more about it. However the info I found is sketchy. Could someone pl. give me a somewhat detailed explanation of what this is and why is this useful?

从我发现的信息来看,看起来这个机制使反射方法的执行更快,代价是创建了大量的动态类并占用了perm gen内存区域,但是我不确定。

From the info I found, it looks like this mechanism makes reflective method execution faster, at the expense of creating a lot of dynamic classes and hogging perm gen memory area, but I'm not sure about it.

推荐答案

是否有一些源代码在编写和编写代码来解决这个问题,这就是我的意思已经发现:

Did some source code digging and coding myself to figure this out, and here's what I've found out:

Java的'Method'类有一个'MethodAccessor'类型的成员变量'methodAccessor',它是一个方法'invoke'的接口,类似于方法的调用。方法调用委托给methodAccessor的调用。

Java's 'Method' class has a member variable 'methodAccessor' of type 'MethodAccessor' which is an interface with a method 'invoke', similar to Method's invoke. Methods's invoke delegates to methodAccessor's invoke.

如果启用了通胀(noInflation为false),这个访问器指向一个使用JNI来运行这个Java方法的实现(我想使用api的像GetObjectClass,GetMethodID和Call * Method)。这就像决斗调度一样,由于这个原因和其他原因,JNI的执行很慢。
是什么让JNI呼叫变慢?

If inflation is enabled (noInflation is false) this accessor points to an implementation which uses JNI to run this Java method (I think using api's like GetObjectClass, GetMethodID and Call*Method). This is like duel dispatching, and execution with JNI is slow due to this and other reasons. ( What makes JNI calls slow? )

通过反射执行15次方法('15'是默认值并且可以更改)并且noInflation为false后,基于JNI的访问器动态创建一个类(名称是动态生成的,例如,说'GeneratedMethodAccessor1'),它也有invoke方法。现在,在这个'invoke'方法中,它将第一个'obj'参数强制转换为其对应的类,然后在其上调用目标方法。然后,它创建此类的实例,并更改methodAccessor设置,以便此后每次执行该方法都委托给此实例而不是JNI访问器。这称为通货膨胀。

After 15 executions of a method through reflection ('15' is default and can be changed) and with noInflation false, the JNI based accessor creates a class on the fly (the name is dynamically generated, e.g. say 'GeneratedMethodAccessor1') which also has the invoke method. Now, within this 'invoke' method, it casts the first 'obj' argument to its corresponding class, and then calls the target method on it. It then creates an instance of this class, and changes the methodAccessor settings such that every execution of the method henceforth is delegated to this instance instead of JNI accessor. This is called inflation.

因为此实例属于委托给Java对象的Java类,所以此后的委托是普通的Java委托。它从未进入JNI,因此节省了开销,加上JITC可以对其进行其他优化,因为它变得高效。

Because this instance is of a Java class which delegates to a Java object, the delegation henceforth is a normal Java delegation. It never goes to JNI and hence saves that overhead, plus JITC can perform other optimization on it due to which it becomes efficient.

缺点是,如果有很多方法以这种方式膨胀,它们的类占用了permgen空间并且可能导致内存不足错误。

The downside is, if a lot of methods are inflated in this manner, their classes occupy permgen space and can possibly cause out of memory error.

详情请参阅:

http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/sun/reflect/ReflectionFactory.java

http://java.sun.com /docs/books/jni/html/fldmeth.html

http://anshuiitk.blogspot.com/2010/11/excessive-full-garbage-collection.html

这篇关于Java:JITC的反映通胀是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆