Frankie
Frankie

Reputation: 25165

Java reflection run-time performance

This is an academical exercise (disclaimer).

I'm building an application that will profit from being as fast as possible as it will compete with others.

I know that declaring a class using reflection (example below) will suffer a huge penalty as opposed to a standard declaration.

Class mDefinition = Class.forName("MySpecialClassString");
Constructor mConstructor = mDefinition.getConstructor(new Class[]{MySpecialClass.class});
myClass = (MySpecialClass) mConstructor.newInstance(this);

However, after declaring myClass if I use it in a standard fashion myClass.myMethod() will I also suffer from performance hogs or will it be the same as if I had declared the class in a standard fashion?

Upvotes: 12

Views: 3058

Answers (3)

Paul Bellora
Paul Bellora

Reputation: 55233

Chris Thompson's answer is on the mark. However I'm confused by your code example.

this will dynamically load the class:

Class mDefinition = Class.forName("MySpecialClassString");

This will get a Contructor for your class, which takes an instance of that same class as an argument. Also note that you're accessing the class at compile time with MySpecialClass.class:

Constructor mConstructor = mDefinition.getConstructor(new Class[]{MySpecialClass.class});

This is instantiating a MySpecialClass by passing this into the constructor:

myClass = (MySpecialClass) mConstructor.newInstance(this);

Based on the constructor argument, does that mean we are in an instance method of MySpecialClass? Very confused.

EDIT: This is closer to what I would have expected to see:

Class<?> mDefinition = Class.forName("MySpecialClassString");

//constructor apparently takes this as argument
Class<?> constructorArgType = this.getClass(); //could be ThisClassName.class

Constructor<?> mConstructor = mDefinition.getConstructor(constructorArgType);

MySpecialInterface mySpecialInstance = (MySpecialInterface)mConstructor.newInstance(this);

where MySpecialInterface is an interface used to interact with your dynamically loaded classes:

interface MySpecialInterface {
    //methods used to interface with dynamically loaded classes
}

Anyway please let me know if I'm misunderstanding or off base here.

Upvotes: 2

Reverend Gonzo
Reverend Gonzo

Reputation: 40871

There will be a performance penalty when you first instantiate the object. Once the class is loaded, its the same as if it had been instantiated normally, and there will be no further performance penalty.

Going further, if you call methods using reflection, there will be a performance penalty for about fifteen times (default in Java), after which the reflected call will be rewritten by the JVM to be the exact same as a statically compiled call. Therefore, even repeatedly reflected method calls will not cause a performance decrease, once that bytecode has been recompiled by the JVM.

See these two link for more information on that:

Upvotes: 15

Chris Thompson
Chris Thompson

Reputation: 35598

Once the class has been loaded you should be fine. The overhead is associated with inspecting the runtime structures that represent the class, etc. Calling methods in a standard fashion should be fine, however if you start searching for methods by name or signature, that will incur additional overhead.

Upvotes: 7

Related Questions