Chris said:
Does this hold true when your objects are very large, though? What if
your object contains a byte [] of length 100K? Or 1Mb?
It is trivially true that object pooling /can/ make sense. The lifetime cost
of an object is the sum of
the time taken to allocate its store
the time taken to zero that store
the time taken in user-code initialising it
the time take to reclaim that store
If you use object pooling then the equation changes to
the time taken to reset the object's state
the time take to manage the pool
So if an object can be "reset" faster than it could be
created+zeroed+initialised then you have a potential saving. If that saving is
great enough to outweigh the costs described by other posters, then you are
making a nett profit.
But, it always possible (with sufficient ingenuity) to devise examples of
objects which take arbitrarily longer to initialise than to reset, so it is
always possible to devise examples of objects which would benefit from pooling.
Such examples don't often come up in real code, though. It would probably
require that the object had an extremely complicated initial internal state but
one to which it could be returned comparatively cheaply. (E.g. some
complicated pre-computed wordlist/dictionary).
Whether you can reach that position just by using larger and larger simple
arrays seems doubtful. Allocating a single object, even a large one, is not an
expensive operation in itself. It's difficult to imagine a scenario where the
initialisation (if any) of the array in user code was significantly cheaper
than resetting it. So that leaves only the time taken by the VM to zero the
store as a potentially worthwhile saving. That is obviously proportional to
the size of the array, so that's looking promising so far. But consider the
overall life of the array -- presumably the code is going to do something with
it, so it seems almost certain that most positions in the array will be written
to, and read, at least once by the application code. Both of those operations
will be at least as expensive as the initial zeroing (per slot), so the time
saved can be no more than 1/3 of the total runtime and, in realistic
applications, almost certainly much less -- which moves the potential saving
into the "it's not worth bothering with" category.
Of course, that last paragraph assumes that the VM/GC implementation is such
that it has no great difficulty with "churning" large objects -- that does
depend on the implementation. It would be interesting to how the numbers work
out in practise rather than in theory[*].
-- chris
[*] And please, don't anyone at this point produce that tired old cliché about
how "theory and practise are the same in theory [etc]"