Analyzing Memory Consumption

An easy way to start looking for areas for optimization is to use the Component Report. Additionally, the following queries may be useful to perform the analysis manually:

Class Histogram Memory Analyzer provides a developer with a possibility to focus on a particular piece of code by using filters in the Class Histogram:
Filter of java.util.* in top row of histogram

Retained Set Looking what a set of objects retains (how much memory and what types of objects) may give some ideas where to optimize.
Collections query group A number of queries provided under the Collections query group give the opportunity to analyze the ways collections are used e.g. how much they are filled, what the sizes are, what the collision ratio (for hash maps) is, etc... See Analyzing Java Collection Usage.
Group by Value The Group by Value query provides the possibility to group a set of objects by the value of a certain field. It is very useful for searching redundant data.
Immediate Dominators When you have found a suspect, that consumes a lot of memory, you can use the dominators query to find out what keeps this suspect in memory. With this query you can also skip the dominators that are of no interest for you, e.g. java.* packages:
skip parameter in query wizard for immediate dominators query

image>
OQL

The two most common ways to "waste" memory are:

  • Inefficient use of data structures, like keeping millions of empty lists or HashMaps. With OQL you can easily find e.g. all instances of ArrayList which are empty and have never been modified:
    SELECT * FROM java.util.ArrayList WHERE size=0 AND modCount=0
  • A lot of redundant data, e.g. redundant Strings or char[] . Below you can find two examples of OQL queries to operate with Strings:
    SELECT * FROM java.lang.String s WHERE s.count >= 100
    SELECT * FROM java.lang.String s WHERE toString(s) LIKE ".*day"