Basic Java Optimisation Hints

6. Be afraid of Reflection and Serialization

Funkiest features are also it's greatest bottlenecks

Reflection is the abiliy to introspect a class and dynamically work out what methods and fields it has

Invoking a method through reflection is approximately one thousand times (1000x) slower than a normal method call

Serialisation is the ability to take a group of objects and dump them out into an array of bytes

Very handy for loading and saving data, and also useful for sharing data between machines (Java RMI)... But spectacularly slow

Often : abstract features = performance bottlenecks : Dynamic class loading, JDBC, parsing XML documents, LDAP directory access, CORBA networking...

Some of Java's funkiest features are also it's greatest bottlenecks. Reflection is the abiliy to introspect a class and dynamically work out what methods and fields it has. It can be quite useful when working with JavaBeans and sometimes gets used in event processing code. But it really bites when it comes to performance. Invoking a method through reflection is approximately one thousand times (1000x) slower than a normal method call. That's three orders of magnitude! Better go and put the kettle on, we could be here for a while...

Serialisation is the ability to take a group of objects and dump them out into an array of bytes. Very handy for loading and saving data, and also useful for sharing data between machines. Serialisation is used extensively in Java RMI to pass parameters back and forth. A great convenience, but also a great way to burn CPU cycles. Serialisation is spectacularly slow! Gob-smackingly inefficient, so use it only if you want your application to be gob-smackingly unresponsive.

In general you should treat any of Java's more abstract features as performance bottlenecks. Dynamic class loading, JDBC, parsing XML documents, LDAP directory access, CORBA networking it's all great stuff but none of it was designed by speed freaks. Handle with care.

N.B. : JavaTM Remote Method Invocation (RMI) enables the programmer to create distributed Java technology-based to Java technology-based applications, in which the methods of remote Java objects can be invoked from other Java virtual machines*, possibly on different hosts.