I'm not sure if I should use @Builder
of Lombok, because in my opinion @Builder
creates two instances of classes.
myClassNameBuilder = MyClassName.builder(); // create ClassNameBuilder instance
myClassNameBuilder.build(); // create ClassName instance but ClassNameBuilder instance still exists in memory.
I think it is not good for performance, because garbage collector and JIT have to work on all instances of ClassNameBuilder.
My question: When should we use @Builder
?
In general, don't do optimization unless you know exactly that you have a performance problem and why it occurs.
The decision whether to use the builder pattern or not should be based upon a single aspect: Does it improve the code quality (i.e. maintainability, readability, ...)? If you later see that it causes a performance problem (which is unlikely), you could still find a solution, maybe even with keeping the builder pattern.
There are several reasons why it is unlikely that it causes problems: