In some cases, knowing the GC roots of a target object is helpful, but if the object has mul-
tiple references, it will have many GC roots. The references here are a tree structure in re-
verse. Say that there are two objects referring to a particular TreeMap$Entry object. Each of
those objects may be referred to by two other objects, each of which may be referred to by
three other objects, and so on. The explosion of references as the roots are traced back means
that there are likely multiple GC roots for any given object.
Instead, it can be more fruitful to play detective and find the lowest point in the object graph
where the target object is shared. This is done by examining the objects and their incoming
references, and tracing those incoming references until the duplicate path is identified. In this
case, references to the StockPriceHistoryImpl objects held in the tree map have two refer-
ents: the ConcurrentHashMap , which holds attribute data for the session, and the Weak-
HashMap , which holds the global cache.
In Figure 7-4 , the back traces are expanded enough to show only a little data about the two of
them. The way to conclude that it is the session data is to continue to expand the Concur-
rentHashMap path until it becomes clear that path is the session data. A similar logic applies
to the path for the WeakHashMap .
Figure 7-4. Back traces of object references in Memory Analyzer
The object types used in this example made the analysis a little easier than is often the case.
If the primary data in this application had been modeled as String objects instead of
BigDecimal objects, and stored in HashMap objects instead of TreeMap objects, things would
have been more difficult. There are hundreds of thousands of other strings, and tens of thou-
sands of other HashMap objects in the heap dump. Finding paths to the interesting objects,
then, takes some patience. As a general rule of thumb, start with collection objects (e.g.,
HashMap ) rather than the entries (e.g., HashMap$Entry ), and look for the biggest collections.