Analyze objects in the JVM heap | IntelliJ IDEA (original) (raw)

When debugging, you can use the Memory tab to review the objects in the heap. IntelliJ IDEA lets you track individual class instances and analyze the conditions that cause objects to spawn or be retained.

This information is useful for detecting memory leaks and their causes. Sometimes, code examination alone might not provide clues, as some errors are easy to overlook. For example, inner classes may prevent the outer classes from becoming eligible for garbage collection, which may eventually result in an OutOfMemoryError. In such cases, combining the Memory tab with the Show Referring Objects option lets you easily find the leak.

Also, examining the memory usage helps you better understand what is going on under the hood and optimize the program by minimizing the creation of unnecessary objects.

Hide/show Memory tab

Memory tab

The Memory tab shows the following information:

Get the information on the number of objects

  1. Stop at a breakpoint or manually suspend the program.
  2. In the Memory tab, click Load classes (for performance, the Memory tab only collects the data on demand).
    The list of all loaded classes appears on the Memory tab. The number of corresponding live objects is shown in the Count column.

Sort and find classes

Get the diff between two execution points

Sometimes it is useful to know how many objects have been created since some point. For this, you can collect the information twice and compare it using the built-in Diff feature.

  1. Collect instances data at the starting point.
  2. Resume the program execution or step through the code.
  3. Collect instances data at the second point. The Diff column shows if the number of instances has changed.
    Diff in Memory tab

View instances

Calculate retained size

For each class instance, you can calculate its retained size. Retained size is the amount of memory that the object itself occupies together with all the objects it refers to and that are not referenced by other objects.

This can be useful when estimating the memory footprint of classes or their specific instances. Also, this can be useful when making a decision on which data structure to use (for example, ArrayList vs. LinkedList).

  1. Prior to running the application, make sure that the Attach memory agent option is enabled in .
  2. When viewing instances of a class, right-click an instance and click Calculate Retained Size.

Track new instances

In addition to getting the number of instances, you can record which particular instances were created and where in the call stack that happened.

  1. Right-click a class and select Track new instances.
    The Memory tab will now store the information on the instances of the selected class created after this moment. When there are new instances, their number will appear in parentheses in the Diff column.
  2. To view the list of new instances, click the number in the Diff column. In the dialog that opens, you can explore the contents of each object and see the stack trace of the thread where the corresponding constructor was called.

Customize the view

The options in Memory View Settings let you customize the way the Memory tab operates:

Last modified: 23 October 2024