Skip to content

Instantly share code, notes, and snippets.

@miguno
Last active April 7, 2023 19:24
Show Gist options
  • Save miguno/548cd72eaec017c475448cb9b2ced258 to your computer and use it in GitHub Desktop.
Save miguno/548cd72eaec017c475448cb9b2ced258 to your computer and use it in GitHub Desktop.
Taking Java JVM memory snapshots with gdb and jmap (for YourKit)

Notes

  • Good: very quick snapshotting even for large heap spaces (e.g. works if your JVM process would otherwise timeout and die)
  • Bad: such a memory snapshot contains significantly less useful information than YourKit's "enhanced" memory snapshots.

Usage

# 1. Find the relevant JVM pid on the target machine
$ ps ...

# 2. Perform a core dump with gdb
$ sudo gdb --pid=1234
(gdb) gcore /tmp/worker-jvm.core  # <<< careful, don't fill up the partition!
Saved corefile /tmp/worker-jvm.core
(gdb)detach  # <<< do this as soon as possible b/c JVM hangs until you detach
(gdb)quit

# 3. Extract JVM heap from core dump
$ sudo /path/to/jdk/bin/jmap \
           -dump:format=b,file=/tmp/worker-jvm.hprof \
           /usr/bin/java /tmp/worker-jvm.core

# 4. Open *.hprof file in YourKit via File > Open Snapshot...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment