Bugs in java.util.ArrayList, java.util.Hashtable and java.io.ByteArrayOutputStream

Martin Buchholz martinrb at google.com
Tue Mar 9 02:20:08 UTC 2010


2010/3/5  <develop4lasu at gmail.com>:
> Hello,
>
> I'm using my own Collections if it's possible so I can add some thoughts:
>
> 1. I would decrease default array size to 4/6/8, for me it was few Mb more
> of free memory ( i suggest testing on application that use at least 300Mb)
>
> I would test:
>
> initial size: 4
> long newCapacity = ((long)oldCapacity) + (oldCapacity >> 1) + 2;
>
> initial size: 6
> long newCapacity = ((long)oldCapacity) + (oldCapacity >> 1) + 2;
>
> initial size: 8
> long newCapacity = ((long)oldCapacity) + (oldCapacity >> 1) + 2;
>
> initial size: 4
> long newCapacity = ((long)oldCapacity) + (oldCapacity >> 2) + 4;

I agree that smaller initial sizes would be better,
(and better yet would be to eventually shrink sizes of arrays!)
but it's very hard to change the default behavior of classes
in the JDK.  Java benchmarks typically do not test
memory-constrained environments, so the JDK
usually optimizes for time over space.

This is the kind of optimization that might better go
into the less conservative IcedTea fork.

> and then use:
>> (int)Math.min(newCapacity, Integer.MAX_VALUE);

The above expression always yields newCapacity.

Martin



More information about the core-libs-dev mailing list