Is there a memory and performance hit taken from using Bloch's Builder Pattern?

℡╲_俬逩灬. 提交于 2019-12-10 12:44:14

问题


What is the memory and performance usage compared to creating a object with only a constructor?

The usage here is in creating a Set<Object> or List<Object> that may contain million plus entries and I am concerned with the overhead of using Bloch's Builder Pattern. I have used it in the past, but never in this large of a scope.

Reference: Item 2: Consider a builder when faced with many constructor parameters, reprinted in Creating and Destroying Java Objects: Part 1, excerpted from Effective Java Second Edition by Joshua Bloch.


回答1:


You have the additional Builder-object, that is discarded after the creation of the object. So you may have some impact on memory-usage and speed. But the Java-VM does optimize very strongly, especially the Server-VM (java -server), so the VM may optimize away the builder completely. So my suggestion is you should measure the real impact (as always if you care about performance) and decide if the impact is too big.




回答2:


It's hard to tell from your initial description but if you are concerned about passing a Collection<Object> with a ~million entries to a constructor vs to a Builder, then the cost of one additional (short-lived) object is barely worth discussing.




回答3:


The cost is negligible since the builder reference can be garbage collected immediately after building the object.

The impact of creating 1m of extra objects should be below 10s in any circumstance.



来源:https://stackoverflow.com/questions/2448523/is-there-a-memory-and-performance-hit-taken-from-using-blochs-builder-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!