Java requires that when an array is allocated, all individual elements in the array must be
initialized to some default value ( null , 0 , or false as appropriate). This can be quite
time-consuming for large arrays.
Native NIO buffers
Allocating a direct java.nio.Buffer (that is, a buffer returned from calling the alloc-
ateDirect() method) is an expensive operation regardless of the size of the buffer. It is
better to create one large buffer and manage the buffers from that by slicing off portions
as required, and return them to be reused by future operations.
Instances of MessageDigest , Signature , and other security algorithms are expensive to
initialize. The Apache-based XML code uses thread-local variables to save these in-
String encoder and decoder objects
Various classes in the JDK create and reuse these objects. For the most part, these are
also soft references, as we'll see in the next section.
The BigDecimal class reuses a StringBuilder object when calculating intermediate
Random number generators
Instances of either the Random and—especially— SecureRandom classes are expensive to
Names obtained from DNS lookups
Network lookups are expensive.
ZIP encoders and decoders
In an interesting twist, these are not particularly expensive to initialize. They are,
however, quite expensive to free, because they rely on object finalization to ensure that