How to Diagnose Java Memory Leaks Using jstat, jmap, and BTrace

The article walks through diagnosing a Java memory leak by examining Full GC symptoms, using jstat, jmap, and BTrace to identify excessive Hashtable entries, tracing the offending code, and preparing for targeted optimization.

ITFLY8 Architecture Home
ITFLY8 Architecture Home
ITFLY8 Architecture Home
How to Diagnose Java Memory Leaks Using jstat, jmap, and BTrace

This article documents a typical Java memory usage issue and walks through step‑by‑step analysis for newcomers.

Initial Symptom

The business system processing MQ messages slowed down, accumulating over 2 million messages; jstat showed frequent Full GC and eventually an OOM.

Further Analysis

Identifying which objects consume most memory, the author avoids full heap dumps and instead uses jmap -histo:live [pid], revealing that Hashtable entries exceed 50 million, occupying about 1.5 GB.

Locating the Code

Having pinpointed Hashtable as the culprit, the next step is to find the code inserting data. Using BTrace to trace Hashtable.put calls, a script TracingHashTable.java is written and executed with:

bin/btrace -cp build 4947 TracingHashTable.java

The stack traces show that after receiving a message, the code that queries the database via iBatis and MySQL JDBC driver puts many properties into the Hashtable, leading to the memory pressure.

With this investigation the problematic code segment is identified, ready for white‑box refactoring and optimization.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavamemory leaktroubleshootingBTracejmap
ITFLY8 Architecture Home
Written by

ITFLY8 Architecture Home

ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.