[concurrency-interest] good design vs performance (object creation)

Dhanji R. Prasanna dhanji at gmail.com
Mon Dec 28 18:21:53 EST 2009


On Mon, Dec 28, 2009 at 11:59 AM, Ramesh Mandaleeka
<ramesh.mandaleeka at gmail.com> wrote:
> Hi Dhanji,
>
>>> A classic example of this is calling clear() on an expanding array list
>>> and forgetting to call trimToSize()
>
> Can you elaborate what do you mean? Is there any side effect by calling
> clear() method? Do we need to call trimToSize() method after clear() method?
>

The side effect is in the growth of the array *before* you call
clear(). If it grows very large then you call clear--it remains
largely empty, wasting memory and creating memory pressure. You would
be surprised how much of a problem this is in real production
environments. It almost always better for the GC to throw away the
entire array and grow it back to the desired size.

Dhanji.


More information about the Concurrency-interest mailing list