liveBook · Manning

Memory usage

As explained in Troubleshooting Java, Second Edition

Memory usage is the amount of runtime memory occupied or allocated by a process or its components.

Memory usage is the runtime memory occupied or allocated by a process or its components. Investigating it helps explain slowdowns, freezes, allocation pressure, and eventual application failure. section 5.2.2 section 9.3

Start with a high-level observation, then narrow the cause. VisualVM’s Monitor tab reveals abnormal memory consumption and GC activity; memory sampling shows which object types and instance counts occupy the most space. section 5.2.2 section 9.1

Use these diagnostic questions:

  • Does memory rise steadily, suggesting retained objects and a possible memory leak, or form peaks and valleys as GC reclaims memory? section 9.3 section 11.4.3
  • Do many instances consume memory, or do a few large instances fill the heap? section 9.1
  • Does frequent GC, especially full GC, indicate insufficient heap capacity rather than a leak? section 11.4.3

Sampling a memory-heavy endpoint

Call the endpoint, select VisualVM’s Sampler tab, start memory sampling, and sort results by occupied memory and instance count. A Product type from the application code may stand out among primitives, strings, arrays, and JDK objects. A few large video files would represent the alternative pattern: reduce memory pressure by processing fragments instead of complete files. section 9.1

Figure 9.1 The memory widget in the Monitor tab in VisualVM helps to identify whether the app spends more memory than usual at any given time. Often, widgets in the Monitor tab, such as CPU and memory consumption, give us clues on how to continue our investigation. When we see that the app consumes an abnormal amount of memory, we may decide to continue with memory profiling the execution.

Use the memory widget to identify abnormal consumption and decide whether deeper profiling is warranted. section 9.1

[GC pause (G1 Evacuation Pause) (mixed), 0.01506789 secs]
[GC pause (G1 Evacuation Pause) (mixed), 0.01783456 secs]
[GC pause (G1 Evacuation Pause) (mixed), 0.02021234 secs]

[Full GC (Allocation Failure) 8192M->6100M(8192M), 2.345678 secs]
[GC pause (G1 Evacuation Pause) (mixed), 0.01901234 secs]
[GC pause (G1 Evacuation Pause) (mixed), 0.02115678 secs]

[Full GC (Allocation Failure) 8192M->5800M(8192M), 2.678901 secs]
[GC pause (G1 Evacuation Pause) (mixed), 0.0987654 secs]
[GC pause (G1 Evacuation Pause) (mixed), 0.02254321 secs]

[Full GC (Allocation Failure) 8192M->5600M(8192M), 2.789012 secs]

Compare repeated full GC events with reclaimed heap values to recognize insufficient memory and its performance cost. section 11.4.3

How Troubleshooting Java, Second Edition covers it

How deep it goes
Explains monitoring, sampling, leak patterns, and heap pressure, while deferring detailed memory instrumentation and GC-log analysis.
Examples
Uses VisualVM widgets, sampled Product objects, large video files, memory graphs, and repeated full-GC log entries.
What it assumes
Assumes a running Java application and basic VisualVM access, while defining allocation, object retention, GC activity, and heap pressure through investigation.
What you can do after
Diagnose memory pressure with VisualVM sampling and GC graphs, distinguish leaks from insufficient allocation, and identify optimization targets.
How it connects
Connects memory usage with garbage collection, memory leaks, heap memory, memory profiling, CPU usage, and GC logs.

Also explained in

Compare how 7 books explain memory usage →

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.