Learner
Learner

Reputation: 21445

Java reflection drawbacks - performance overhead

I am going through Java reflection drawbacks and came across below statement from Java docs:

Performance Overhead

Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.

Please help me in understanding this. What it means that reflection involves types that are dynamically resolved? What are the optimizations provided by JVM in general?

Upvotes: 1

Views: 317

Answers (1)

Marco Acierno
Marco Acierno

Reputation: 14847

As you can easily understand the JVM when starts your programs does a lot of improvements to improve the performance of your code (which is a great thing!) but when we are talking about reflection we are talking about a thing which changes in runtime (i could say it's nondeterministic.. the JVM cannot know what it will be until it execute it) so it cannot be improved by JVM because it behaviour could be different in some cases during runtime.

As i always say in this kind of question: here you could found answer of people who know very very good how reflection works.. search around too.

Upvotes: 2

Related Questions