11

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
WolfmanDragon
  • 7,851
  • 14
  • 49
  • 61
  • Can you please add a link to Bloch's Builder Pattern? (i.e. original Bloch's description) – Roman Mar 15 '10 at 16:10
  • You can find Bloch's Builder Pattern in the Effective Java book or @ http://rwhansen.blogspot.com/2007/07/theres-builder-pattern-that-joshua.html – chburd Mar 15 '10 at 16:14
  • @chburd: I also noticed this first link in google, but I have doubts that it's the Bloch's original description. – Roman Mar 15 '10 at 16:18

3 Answers3

7

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.

Mnementh
  • 50,487
  • 48
  • 148
  • 202
  • +1, It is a non-trivial test to build, but I will do. I have no control over what JVM is used though, so i was looking for generalities. – WolfmanDragon Mar 15 '10 at 17:52
3

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.

matt b
  • 138,234
  • 66
  • 282
  • 345
  • 1
    I think he creates the millions of objects with the builder-pattern. That means for every of the million objects and additional temporary builder-object. – Mnementh Mar 15 '10 at 16:38
  • He could probably just add a .clear() method or something to the builder each iteration to empty it's contents. – Dark Castle Mar 15 '10 at 19:51
2

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.

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • Actually I'd expect less than 1s, even on an average netbook or upper-class smart phone. – sfussenegger Mar 15 '10 at 20:44
  • @sfusseneger: Yeap, I created 1m objects in a simple for loop and it took me about 400 ms ... I think 10 s should cover all the cases but I agree with you. – OscarRyz Mar 15 '10 at 21:56
  • This is probably not a good test as it takes the JVM a little while to warm up and optimize. The code will run a little slower while it's being optimized. – spierce7 Feb 02 '17 at 16:52