Skip to content

Instantly share code, notes, and snippets.

@stefanJi
Forked from LanderlYoung/android_dump_java_heap.sh
Last active October 22, 2019 08:10
Show Gist options
  • Save stefanJi/c99b2d1eeb8590627dd3c5c032e097e4 to your computer and use it in GitHub Desktop.
Save stefanJi/c99b2d1eeb8590627dd3c5c032e097e4 to your computer and use it in GitHub Desktop.
script to dump android java heap and convert use hprof-conv
ADB=${ANDROID_HOME}/platform-tools/adb
HPROF_CONV=${ANDROID_HOME}/platform-tools/hprof-conv
# customized functions
# dump droid heap
function droid_hd() {
PACKAGE_NAME=$1
TIME=$(date +"%Y%m%d_%H%M%S")
FILE_NAME="${PACKAGE_NAME}-${TIME}-heap.hprof"
PATH_IN_PHONE="/data/local/tmp/${FILE_NAME}"
${ADB} shell rm ${PATH_IN_PHONE} 2> /dev/null
echo "cause GC for ${PACKAGE_NAME}"
${ADB} shell pkill -l 10 ${PACKAGE_NAME}
echo "> dump heap for ${PACKAGE_NAME}"
${ADB} shell "am dumpheap ${PACKAGE_NAME} ${PATH_IN_PHONE}"
if [[ $? != 0 ]]; then
echo
${ADB} devices
echo "run command:"
echo "\e[38;5;82mexport ANDROID_SERIAL="
return
fi
# I don't want to... But it smees adb shell can't block untils it's done!
echo "sleep 20 senconds to wait dumpheap finish"
sleep 20
echo "> list heap for ${PATH_IN_PHONE}"
${ADB} shell ls -lh ${PATH_IN_PHONE}
echo "> pull to computer"
${ADB} pull ${PATH_IN_PHONE} ./
echo "> delete device copy"
${ADB} shell rm ${PATH_IN_PHONE}
echo "> hprof-conv it"
${HPROF_CONV} -z ${FILE_NAME} convered-${FILE_NAME}
echo "done!"
}
droid_hd $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment