Re: Short-lived Objects - good or bad?
Kenneth P. Turvey <kt-usenet@squeakydolphin.com> wrote:
I'd have to say that my experience has been different. I think it really
depends on the objects being created. A single new inside an inner loop
can be fine, but if that new results in many, many sub-objects being
created then you might have problems with it.
I wonder how much creating thousands of objects that don't live long could
hurt performance. Brian Goetz and other knowledgeable Java gurus have stated
that the cost of allocation is about 12 machine instructions, and
de-allocation from the young generation is essentially free. The overhead
comes from filling the young generation and invoking minor collections more
often, but if 98% of the objects have died you only incur the overhead of
copying the remaining 2%.
By keeping objects around you move them into the tenured generation. If such
objects refer to short-lived objects, you've defeated part of the advantage of
the nursery, and caused major collections on such objects when they finally do
die.
I'm going to stick with allocation of (possible thousands of) short-lived
objects in Java, rather than thinking I can outsmart the JVM.
Owen Jacobson said:
Out of curiousity, I benchmarked this a while ago on some of my own
code [0],
Out of curiosity, how many short-lived objects does that code produce during
the "allocate many" run?
--
Lew