Skip to content

Instantly share code, notes, and snippets.

@wizardjedi
Last active July 4, 2019 09:44
Show Gist options
  • Save wizardjedi/c1ab6a45615e443278e8 to your computer and use it in GitHub Desktop.
Save wizardjedi/c1ab6a45615e443278e8 to your computer and use it in GitHub Desktop.
Script for creating heap dump of running application using gdb
#!/bin/bash
# atlassian-heap-dump.sh - dump a heap using GDB for a crashed application
# Accepts a single argument: the PID of the JVM
# Author: James Gray ([email protected])
# Source: http://blogs.atlassian.com/2013/03/so-you-want-your-jvms-heap/
# Copyright Atlassian P/L
# License: MIT
# Are we root?
if [ $UID -ne 0 ]; then
echo "Be gone peon - you must be root"
exit 1
fi
# Did we get a command line argument?
if [ -z $1 ]; then
# 1st command line arg is empty...dump usage and quit
echo "Must have a JVM PID to dump"
echo -e "eg,\n$(basename $0) \n"
exit 1
fi
# OK, we have a PID, we are root...hit it
JVM_CORE=/tmp/jvm.core
JVM_HEAP=/tmp/application-name-$(date +'%Y%m%d').hprof
JMAP_OPTS="-dump:format=b,file=${JVM_HEAP} /usr/bin/java ${JVM_CORE}.${1}"
GCORE_OPTS="-o ${JVM_CORE} ${1}"
HERE="$(pwd)"
# Go to /tmp ... just so we know where we are.
cd /tmp
# Now run gdb and get the core:
echo "Dumping the core for PID: \"${1}\""
gcore ${GCORE_OPTS}
# Now get the heap and dump it to the preferred name:
echo "Core created at ${JVM_CORE}.${1} - YOU CAN NOW RESTART THE APPLICATION"
jmap ${JMAP_OPTS}
echo "Your JVM Heap is now available at: ${JVM_HEAP}"
# Clean up after ourselves:
echo "Deleting redundant core file"
rm -f ${JVM_CORE}.${1} >/dev/null 2>&1
# Go back to whence we came...
cd "${HERE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment