Over a million developers have joined DZone.

The Performance Cost of Reflection

We all know that using the Reflection APIs in Java comes at great performance cost. But just how massive is the latency on using reflection instead of object orientation?

· Performance Zone

See Gartner’s latest research on the application performance monitoring landscape and how APM suites are becoming more and more critical to the business, brought to you in partnership with AppDynamics.

In Java, it’s widely admitted that reflection - usage of the java.reflect API, comes at a high cost in terms of performance. Older Java versions had huge performance overhead, while it seems that newer versions bring it in the acceptable range. But what does “acceptable” really mean?

This is the question I asked when commenting on a performance review that advised to replace code based on reflection by standard code. As many of our decisions are not based on facts but on beliefs, I decided to perform some tests to get metrics in Java 8.

Testing Protocol

In order to get realistic metrics through an unchallenged protocol, I used the excellent JMH testing framework. JMH has several advantages, among others:

  • An existing Maven artifact is readily available
  • Methods to benchmark have only to be annotated with @Benchmark
  • It handles the warming up of the JVM
  • It also handles the writing the results on the console

Here’s a JMH snippet:

public void executePerformanceTest() {
    // Code goes here

JMH will take care of executing the above executePerformanceTest() and taking care of measuring the time taken.

To highlight the cost of reflection, let’s check the difference between the time needed to access attributes with reflection and to call simple getters without.

// With reflection
Field firstName = clazz.getDeclaredField("firstName");
Field lastName = clazz.getDeclaredField("lastName");
Field birthDate = clazz.getDeclaredField("birthDate");
Field.setAccessible(new AccessibleObject[] { firstName, lastName, birthDate }, true);

// Without reflection

Checking Possible Optimizations

I was wondering if immutable data structures were compiled into optimized bytecode that could decrease the performance overhead of reflection.

Thus I created the same basic data structure in two different ways:

  • One mutable with a no-args constructor and setters
  • One immutable with final attributes and constructor initialization


Running the tests on my machine yields the following results:

# Run complete. Total time: 00:26:55

Benchmark                                    Mode  Cnt         Score        Error  Units
BenchmarkRun.runImmutableWithReflection     thrpt  200   2492673.501 ±  37994.941  ops/s
BenchmarkRun.runImmutableWithoutReflection  thrpt  200  26499946.587 ± 242499.198  ops/s
BenchmarkRun.runMutableWithReflection       thrpt  200   2505239.277 ±  27697.028  ops/s
BenchmarkRun.runMutableWithoutReflection    thrpt  200  26635097.050 ± 150798.911  ops/s

For table-minded readers:

Mutable26,635,097.050 ± 150,798.9112,505,239.277 ± 27,697.028
Immutable26,499,946.587 ± 242,499.1982,492,673.501 ± 37,994.941

And for bar chart interested readers (note the scale is linear):

Image title


Whatever the way results are displayed, it will take about 10 times more to access a field with reflection than without… on my machine. My guess is that this can be extrapolated to be true on any machine.

There’s a corollary conclusion: whatever you think holds true at a given time, you should always do some fact-checking to ensure a solid foundation for your decisions.

If you want to run the tests yourself, the project is available online in Maven format.

The Performance Zone is brought to you in partnership with AppDynamics.  See Gartner’s latest research on the application performance monitoring landscape and how APM suites are becoming more and more critical to the business.


Published at DZone with permission of Nicolas Frankel, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}