Your problem is even worse than you realize, because ArrayList<Double>
is much less efficient than 8 bytes per entry. Each entry is actually an object, to which the ArrayList
keeps an array of references. A Double
object is probably about 12 bytes (4 bytes for some kind of type identifier, 8 bytes for the double
itself), and the reference to it adds another 4, bringing the total up to 16 bytes per entry, even excluding overhead for memory management and such.
If the constraints were a little wider, you could implement your own DoubleArray
that is backed by a double[]
but knows how to resize itself. However, the resizing means you'll have to keep a copy of both the old and the new array in memory at the same time, also blowing your memory limit.
That still leaves a few options though:
Loop through the input twice; once to count the entries, once to read them into a right-sized
double[]
. It depends on the nature of your input whether this is possible, of course.Make some assumption on the maximum input size (perhaps user-configurable), and allocate a
double[]
up front that is this fixed size. Use only the part of it that's filled.Use
float
instead ofdouble
to cut memory requirements in half, at the expense of some precision.Rethink your algorithm to avoid holding everything in memory at once.