BeanUtils performance versus ReflectionToStringBuilder (for use in Bean classes)

I have a large number of Java bean classes in my web application, and I'm trying to find an easy way to implement toString() methods in these beans. The toString() method will be used for logging throughout the application and should print attribute value pairs of all attributes in the bean.

I try two alternatives:
1. BeanUtils.describe() (Apache commons-beanutils)
2. ReflectionToStringBuilder.toString() (Apache commons-lang)

Since this is a web application that is expected to have high traffic, the implementation should be lightweight and should not affect performance. (Memory usage, CPU usage, etc. are the main considerations).

I would like to know which one works best according to the above criteria. As far as I know, reflection is a difficult operation, but more detailed information and understanding of both of these options will help me choose the best solution.

+7
source share
4 answers

We use the ToStringBuilder.reflectionToString() methods in our toString() objects. We had no problems like this in a production environment. Of course, we rarely use the toString() method.

We also use BeanUtils.describe() , but for a different purpose. BeanUtils uses a PropertyUtilsBean , which stores the beans beans internal cache for which it performed an introspection. It would seem that this will give him a performance advantage over another, but will focus on the source of reflectionToString a bit, and it seems that since he ultimately relies on the java.lang.Class implementation, caching also plays there.

It either looks like a viable choice, but BeanUtils.describe() will return a property map where reflectionToString will return a formatted string. I think it depends on what you want to do with the exit.

I would suggest that if your application is highly dependent on calling toString() on your objects, using a specific implementation might be more useful.

+9
source

Personally, I prefer to generate the toString () method using Eclipse / IntelliJ, and then modify it as needed (include only important fields).

Right click -> Source -> Create toString (). Select fields. Done.

  • It takes less time than writing Builder code.
  • It will run faster.
  • It does not use pergene space (reflection may tend to eat hypergene)

This is the way I will go if you are concerned about performance.

+8
source

Be careful, as it is based on reflection, it will be slow.

In a recent web project, our base toString () method was ToStringBuilder.reflectionToString (this)

This method is called in sleep mode during save (via the Spring Data JPA repository). We have a rather large tree of objects with nested lists, which leads to a large amount of memory and CPU deletion during saving.

He almost drowned in the project.

+1
source

Just use the code generator in your IDE to generate the toString () method. This way you avoid overhead caused by the use of reflections. On a real production system, your toString () method can be called very often (100 / sec), which causes the garbage collector to work hard and pause the JVM. These pauses can be seconds or tens of seconds.

0
source

All Articles