liveBook · Manning

Reference

As explained in Troubleshooting Java, Second Edition

An object reference is a value that identifies an object and can keep that object reachable for memory-management purposes.

An object reference identifies an object and can keep it reachable. The garbage collector removes an object only when no remaining references make it reachable. section 5.1.1 section 10.2

Retaining references after objects are no longer needed creates a memory leak. Profiling and heap dumps reveal whether references are still required and where they are stored. section 5.1.1 section 10.2

Investigate suspicious object retention by:

  • Finding types with unusually many instances or large memory usage. section 10.2
  • Inspecting what each instance refers to and which objects refer to it. section 10.2
  • Locating the reference in application code and deciding whether the retained object is still needed. section 10.2

A retained Product instance

A Product instance refers to its name, while an Object array in an ArrayList keeps more than one million Product references. The ArrayList is stored in a static Main variable, making it a likely memory-leak lead. section 10.2

Figure 10.8 References to an instance. By using the heap dump, you can find, for each instance, what other instances were being referenced at the time the dump was generated. The profiling tool also tells you where a given reference is stored in the code. In this case, ArrayList, which holds over one million references, is a static variable in the Main class.

Shows how a heap dump identifies the references held for an instance and locates the code storing them. section 10.2

How Troubleshooting Java, Second Edition covers it

How deep it goes
Defines retention by references, then traces referrers and storage locations in heap dumps; it stops short of prescribing the code fix.
Examples
Uses Product instances retained through an ArrayList stored in a static Main variable.
What you can do after
Diagnose retained objects with VisualVM by locating referrers and the code-held reference.
How it connects
Connects garbage collection, memory leaks, memory profiling, heap dumps, and referrers.

Also explained in

Compare how 17 books explain reference →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Troubleshooting Java, Second Edition. The section links point to the parts of the book it draws on.