I am parsing data where precision is not my main concern. I often get java.lang.OutOfMemoryError even if I use maximum Java heap size. So my main concern here is memory usage, and java heap space. Should I use double or float data type?
If your memory usage is related to a huge amount (many millions) of floating-point numbers (which can be verified with a decent memory profiler), then you're most probably storing them in some data structures like arrays or lists.
Recommendations (I guess, you are already following most of them...):
float
over double
if number range and precision are sufficient, as that consumes only half the size.java.lang.Float
or java.lang.Double
classes for storage, as they hav a considerable memory overhead compared to the naked scalar values.java.util.List
, as they store boxed java.lang.Float
instances instead of the naked numbers.But above that, have a decent memory profiler show you which instances occupy most of your memory. Maybe there are other memory consumers besides the float/double data.
EDIT:
The OP's recent comment "I consistently get OOM exceptions because I use a great number of ArrayLists with numbers" makes it clear. ArrayList<Float>
wastes a lot of memory when compared to float[]
(Stephen C gave detailed numbers in his answer), but gives the benefit of dynamic resizing.
So, I see the following possibilities:
float[]
arrays.ArrayList<Float>
while building one object (when size still increases), and then copy the contents to a float[]
array for long-term storage. Then the wasteful ArrayLists exist only for a limited timespan.FloatArrayList
class based on a float[]
array, resembling the ArrayList<Float>
as far as your code needs it (that can range from a very shallow implementation up to a full-featured List, maybe based on AbstractList
).