Heap Space Nine: Explore your Java Memory with AI
Carl Chesser Carl Chesser
@che55er | che55er.io

Today’s path

Capture

Why we take a heap dump and the two common commands.

Understand

What a .hprof contains, and why it can be huge.

Analyze + Extend

How MAT reads, indexes, and queries the snapshot with Calcite for a richer query engine.

Connect

How MCP exposes heap analysis to an AI application.

Demonstrate

A live walk through the Java heap workbench.

Chapter #1

Capture the evidence

Why capture a heap dump?

Why capture a heap dump?

Reactive: an incident happened

OutOfMemoryError

Find which objects retained the heap and what application path kept them reachable.

Proactive: plan capacity

Size the container workload

Understand heap, object, and retention patterns before choosing memory limits and headroom.

A heap dump shows what the JVM was holding at that moment.

The map

JVM memory is bigger than the heap

Java heap
objects, arrays, class instances
Native memory
threads, code cache, metaspace, GC structures
Process + libraries
JVM, agents, mapped files, runtime overhead

Heap dump answers

What objects existed? How large were they? What kept them reachable?

It does not answer

What happened first, or which request created every object.

Size is not one number

Shallow heap

The memory occupied by the object itself, excluding the objects it references.

Retained heap

The memory that would become collectible if this object were removed from the graph.

GC root

A runtime-held reference, such as a thread or static field, that keeps objects reachable.

Object count

The number of instances of a class; many small objects can consume substantial memory in total.

jcmd: capture a heap dump

jcmd <PID> GC.heap_dump \
  /tmp/service.hprof

The target JVM must generally be owned by the same OS user. Choose a destination with enough free space.

jmap: the traditional JDK command

jmap -dump:format=b,\
file=/tmp/service.hprof <PID>

This produces the same kind of binary heap-dump artifact for analysis.

Chapter #2

Understand the snapshot

What is actually inside a .hprof?

What is actually inside a .hprof?

  • A snapshot of the JVM heap at one point in time.
  • Class metadata, object instances, arrays, references, and GC-root information.
  • Potentially sensitive data, including strings and application fields.
  • A potentially large binary file, often comparable to the live heap.

What can read a .hprof?

Eclipse MATDeep heap analysis, reports, retained sizes, and OQL.
https://eclipse.dev/mat/
VisualVMObject browsing, summaries, references, and OQL.
https://visualvm.github.io/
JOverflowA JDK Mission Control plugin for common heap anti-patterns.
https://github.com/openjdk/jmc

These tools are covered in the memory-analysis guide.

Why focus on Eclipse MAT?

Open source since 2008.

Mature enough for large production heap dumps.

Versatile enough for reports, object graphs, retained sizes, and OQL.

Licensed under the Eclipse Public License 2.0.

How MAT handles a large .hprof file

Object lookup.index
.o2hprof.index
.o2c.index
.idx.index
Reference graph.inbound.index
.outbound.index
.a2s.index
Retention.domIn.index
.domOut.index
.o2ret.index
Thread data.threads

Creates a file-based index to assist in different forms of lookups.

Chapter #3

Analyze + Extend

Read, query, and extend heap analysis.

What MAT already gives us

A mature engine for reading heap snapshots.

Indexes, retained sizes, dominators, references, reports, and OQL.

OQL is the bridge to questions

SELECT c.cacheLimit, c.innerCache.size
FROM cchesser.javaperf.workshop.cache.CleverCache c

OQL is not standard SQL.

MAT publishes a BNF grammar an agent can follow.

When OQL is not enough

MAT OQL provides essential capabilities for quering data; however, it is quite limited.

For joins, grouping, sorting, and aggregation, add Apache Calcite based on the mat-calcite-plugin.

MAT + Calcite: a richer query

SELECT toString(file) AS url,
       COUNT(*) AS copies,
       SUM(retainedSize(this)) AS retained
FROM java.net.URL
GROUP BY toString(file)
HAVING COUNT(*) > 1
ORDER BY retained DESC

Now the agent can ask distribution and relationship questions in SQL.

Calcite keeps the heap as the data source

MAT provides

Objects, fields, references, shallow size, retained size.

Calcite provides

Stronger relational query layer over MAT’s heap-backed schema.

Calcite distribution query result
Chapter #4

Connect analysis to an agent

The workbench turns tool results into a conversation.

Why add an agent?

Ask a memory question in plain language.

Let the model choose how to resolve the question with the available tools.

It can further explore the data set through a clearly defined query language (OQL).

Model Context Protocol (MCP)

An AI application is configured with the MCP interface (server).

The MCP server describes inputs and outputs.

The model understands this protocol and then can utilize this to utilize specific tools for specific use-cases.

Model Context Protocol diagram

Meet java-heap-mcp

AI application
MCP over stdio
MAT + Calcite

Server owns the session

Load a hprof file into a named project, build or reuse MAT indexes, and return a stable heap handle.

Responses stay bounded

Tools return focused JSON summaries and rows instead of sending the raw .hprof to the model.

A request through the server

heap_open_project
heap_get_overview
MAT snapshot
Results!

The model never needs direct access to the raw heap file, it is able to access through well-defined MCP tools.

13 tools, three jobs

Lifecycle

heap_load_dump
load/index a dump

heap_open_project
resume a cached project

heap_list_projects · heap_list_dumps
discover cached state

heap_unload_dump
release an active handle

Orient

heap_get_overview
summary, top classes, dominators

heap_get_histogram
sort by retained/shallow/count

heap_get_dominators
find retained-memory roots

heap_get_oql_grammar
learn the supported SQL/OQL

Investigate

heap_run_oql
query with limit + offset

heap_inspect_object
fields and references

heap_find_path_to_gc_roots
trace reachability

heap_find_leak_suspects
run MAT leak analysis

A tool chain follows the evidence

heap_get_overview
What is large?
heap_get_histogram
Which classes dominate?
heap_inspect_object
What does this object retain?
heap_find_path_to_gc_roots
Why is it alive?
heap_run_oql
Can we test a pattern?
heap_find_leak_suspects
What does MAT flag?

The model chooses the next tool from the returned evidence.

Natural language becomes a traceable run

Browser
local or frontier model
MCP
MAT

A2UI: Agent-to-UI

A2UI keeps the workbench simple

tool result
components[]
browser renderer

The model returns declarative JSON.

The UI renders summaries, tables, charts, and graphs.

What A2UI makes possible

The same tool result can become a table.

Numeric rows can become a chart.

References can become a graph.

Utilize your local filesystem for heap analysis

Browser local model (via ollama) MCP MAT heap dump.

You can also use any frontier model and coding agent to interact with this MCP server.

Just remember, your heap file contains sensitive data.
Chapter #5

Demo Time

Lessons Learned

  • MCP server functions be fast, but having an agent iterated with it can be slow.
  • OQL can still be confusing to an agent without alot of sufficient guidance (system prompt, grammar context).
  • Enabling advanced querying capabilities with Apache Calcite can expand how you can answer general questions.
  • Utilizing local models help isolate what content is being analyzed, but it can be too slow on some simple operations.
Heap Space Nine: Explore your Java Memory with AI

Thank You!

Carl Chesser Carl Chesser
@che55er · che55er.io
jvmperf.net
QR code linking to more information