Java programs can be very memory hungry. For example, a Double
object has 24 bytes: 8 bytes of data and 16 bytes of JVM-imposed overhead. In general, the objects that represent the primitive types are very expensive.
The same happens for any collection in the Java Standard Library. There are even some counterintuitive facts such as a HashSet
being more memory hungry than a HashMap
, since a HashSet
contains a HashMap
inside (http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html).
Could you come up with some advice when modeling data and delegation of objects in high performance settings so that these "weaknesses" of Java are mitigated?
Some techniques I use to reduce memory:
new String
to dispose of the old big string.array[x|y<<4]
for a 16xN array.StringBuilder
with an initial capacity chosen such that it prevents internal reallocation in a typical circumstance.
StringBuilder
instead of string concatenation, because the compiled class files use new StringBuilder()
without initial capacity to concatenate strings.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With