1. Knowing which objects are consuming memory is the first step in knowing which
objects to optimize in your code.
2. Histograms are a quick and easy way to identify memory issues caused by creat-
ing too many objects of a certain type.
3. Heap dump analysis is the most powerful technique to track down memory usage,
though it requires some patience and effort to be utilized well.
Out of Memory Errors
The JVM throws an out of memory error under these circumstances:
▪ No native memory is available for the JVM.
▪ The permgen (in Java 7 and earlier) or metaspace (in Java 8) is out of memory.
▪ The Java heap itself is out of memory: the application has too many live objects for the
given heap size.
▪ The JVM is spending too much time performing GC.
The last two cases—involving the Java heap itself—are more common, but don't automatic-
ally conclude from an out of memory error that the heap is the problem. It is necessary to
look at why the out of memory error occurred (that reason is part of the output of the excep-
Out of native memory
The first case in this list—no native memory available for the JVM—occurs for reasons un-
related to the heap at all. In a 32-bit JVM, the maximum size of a process is 4 GB (3 GB on
some versions of Windows, and about 3.5 GB on some older versions of Linux). Specifying
a very large heap—say, 3.8 GB—brings the application size dangerously close to that limit.
Even in a 64-bit JVM, the operating system may not have sufficient virtual memory for
whatever the JVM requests.
This topic is addressed more fully in Chapter 8 . Be aware that if the message for the out of
memory error discusses allocation of native memory, then heap tuning isn't the answer: you