Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance cost of Java dynamic proxy

Many modern frameworks (Spring, Hibernate) provide very nice dynamic behaviors with use of Java dynamic proxies, but what's the exact performance cost associated with it? Are there public benchmarks available for Sun JVM?

like image 480
Gennady Shumakher Avatar asked Dec 06 '09 19:12

Gennady Shumakher


People also ask

What is dynamic proxy in Java?

A dynamic proxy class is a class that implements a list of interfaces specified at runtime such that a method invocation through one of the interfaces on an instance of the class will be encoded and dispatched to another object through a uniform interface.

How does JDK dynamic proxy work?

JDK Dynamic Proxies allow one to create implementations of Java interfaces at runtime by the means of Reflection. A proxy may be seen as a subject that will forward method calls to target instances and eventually return any result produced by the target instance to the caller.

Why proxy is used in Java?

Proxy is a structural design pattern that provides an object that acts as a substitute for a real service object used by a client. A proxy receives client requests, does some work (access control, caching, etc.) and then passes the request to a service object.

What is the type of dynamic proxy in Spring framework?

Spring used two types of proxy strategy one is JDK dynamic proxy and other one is CGLIB proxy. JDK dynamic proxy is available with the JDK. It can be only proxy by interface so target class needs to implement interface.


2 Answers

A few pointers:

  • Debunking myths: proxies impact performance (have a look at the comments too)
  • Java theory and practice: Decorating with dynamic proxies
  • Benchmarking the cost of dynamic proxies
like image 95
Pascal Thivent Avatar answered Oct 02 '22 23:10

Pascal Thivent


I don't know if there is any performance analysis in the framework you mentioned, but in my project lambdaj I made a very large use of dynamic proxy using the same technology (cglib). In the pdf that explains how my library works you can also find an interesting performance comparison on this subject.

like image 36
Mario Fusco Avatar answered Oct 02 '22 23:10

Mario Fusco