Skip to content

Instantly share code, notes, and snippets.

@rajeshpv
Created March 27, 2025 13:54
Show Gist options
  • Select an option

  • Save rajeshpv/723c64e0802cc1f5007a125bee2b6558 to your computer and use it in GitHub Desktop.

Select an option

Save rajeshpv/723c64e0802cc1f5007a125bee2b6558 to your computer and use it in GitHub Desktop.
jvm_stat

Capturing JVM heap allocation and comparing it between two executions of a Java application can be super insightful when you're analyzing memory usage, leaks, or performance. Here’s a practical way to do it:


🚀 Step-by-Step Guide

✅ 1. Enable JVM Heap Monitoring

Use the following Java options when running your app to capture memory usage:

java -Xmx512m -Xms512m -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:gc.log -jar your-app.jar

This:

  • Sets max and initial heap size.
  • Logs garbage collection and heap stats to gc.log.

✅ 2. Use jstat (Java Statistics Monitoring Tool)

Run jstat while your app is running:

jstat -gc <pid> 1000 10

Where:

  • <pid> = Process ID of the Java app.
  • 1000 = delay between samples (in ms).
  • 10 = number of samples.

This gives heap usage stats like:

  • S0C, S1C – survivor spaces
  • EC, EU – Eden space capacity and used
  • OC, OU – Old gen capacity and used
  • YGC, FGC – young/full GC counts

✅ 3. Heap Dump (Optional – for full analysis)

Capture a heap dump:

jmap -dump:format=b,file=heap1.hprof <pid>

Do this at both execution points (heap1.hprof and heap2.hprof).

You can analyze .hprof files using tools like:

  • Eclipse MAT (Memory Analyzer Tool)
  • VisualVM
  • JProfiler

✅ 4. Compare Between Runs

You can:

  • Diff the gc.log files.
  • Use Eclipse MAT to open both heap dumps and visually compare.
  • Look at jstat outputs side-by-side for patterns (like growing Old Gen memory or GC frequency).

🧠 Pro Tip

Automate it via a shell script:

#!/bin/bash
java -Xmx512m -Xms512m -Xloggc:gc_run1.log -jar your-app.jar
# then:
jstat -gc <pid> > jstat_run1.txt

Repeat for run2 and then use diff or Excel to compare jstat_run1.txt and jstat_run2.txt.


Want help writing a script to automate all this? Or comparing .hprof files with MAT?

@rajeshpv
Copy link
Author

rajeshpv commented Apr 1, 2025

Analyzing .hprof files (Java Heap Profile files) in Java 17 can help you troubleshoot memory leaks, check object allocations, or understand memory usage. Here’s how to do it:


🔧 Tools You Can Use (Java 17 Compatible):

1. JDK's jhat (deprecated in Java 9+)

Not available in Java 17, so skip unless you're using an older JDK.

2. Eclipse Memory Analyzer (MAT) – 💯 Most common

  • Download: [MAT from Eclipse](https://www.eclipse.org/mat/downloads.php)

  • Usage:

    1. Open MAT.
    2. File → Open Heap Dump → Choose your .hprof file.
    3. MAT will parse and index the file.
    4. Use tools like Dominator Tree, Histogram, Leak Suspects Report.
  • Great for:

    • Finding memory leaks 🔍
    • Understanding object retention 🌳
    • Visual exploration of heap data 📊

3. JVisualVM

  • Comes with most JDKs (Java 17+ needs manual plugin sometimes).
  • Install Plugin:
    • Open VisualVM → Tools → Plugins → Install “VisualVM-MBeans”, “VisualVM-Sampler”, etc.
  • Usage:
    • Open .hprof file from File → Load.

4. jhsdb (Java HotSpot Debugger)

Comes with JDK 17. Command-line tool.

  • Run it like this:

    jhsdb jmap --heap --binaryheap --dumpfile heapdump.hprof

    Or:

    jhsdb jmap --heap --exe java --core corefile
  • Good for detailed debugging, but less user-friendly.

5. Other Third-Party Tools:

  • YourKit
  • JProfiler
  • HeapHero (online tool, drag and drop)

🧠 What You Can Analyze in .hprof:

  • Heap Histogram – Object types and count
  • Dominator Tree – What’s holding memory?
  • Garbage Collection Roots
  • Reference Chains
  • Duplicate objects or classloaders
  • Potential memory leaks

🧪 Pro Tips:

  • Use MAT’s Leak Suspects Report for a quick analysis.
  • Filter with Object types (like java.util.HashMap, byte[], etc.).
  • Use OQL (Object Query Language) in MAT to write SQL-like queries on heap.

Need help with a specific .hprof file? I can guide you through interpreting it.

@rajeshpv
Copy link
Author

rajeshpv commented Apr 1, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment