Skip to content

Instantly share code, notes, and snippets.

@isapir
Last active June 2, 2023 06:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save isapir/e2bf7595b926a94d54516f395f89e1c8 to your computer and use it in GitHub Desktop.
Save isapir/e2bf7595b926a94d54516f395f89e1c8 to your computer and use it in GitHub Desktop.
Dump Threads and Heap for a JVM that runs as Service
:: if the service is run in Session 0 then open a console for that session and run it from there
:: e.g. %PsExec% -s -h -d -i 0 cmd.exe
:: set the paths for your environment
set PsExec=C:\Apps\SysInternals\PsExec.exe
set JAVA_HOME=C:\Apps\Java\jdk1.8.0_121
set DUMP_DIR=C:\temp
@echo off
set PID=%1
if "%PID%"=="" (
echo usage: jvmdump.bat {pid}
exit /b
)
for /f "tokens=2,3,4 delims=/ " %%f in ('date /t') do set timestamp_d=%%h%%g%%f
for /f "tokens=1,2 delims=: " %%f in ('time /t') do set timestamp_t=%%f%%g
set timestamp=%timestamp_d%%timestamp_t%
echo datetime is: %timestamp%
set DUMP_DIR_TS="%DUMP_DIR%\jvmdump-%PID%-%timestamp%"
mkdir %DUMP_DIR_TS%
echo ### Version >>"%DUMP_DIR_TS%\%PID%-%timestamp%-jvm.log"
%PsExec% -s %JAVA_HOME%\bin\jcmd.exe %PID% VM.version >>"%DUMP_DIR_TS%\%PID%-%timestamp%-jvm.log"
echo. >>"%DUMP_DIR_TS%\%PID%-%timestamp%-jvm.log"
echo ### Uptime >>"%DUMP_DIR_TS%\%PID%-%timestamp%-jvm.log"
%PsExec% -s %JAVA_HOME%\bin\jcmd.exe %PID% VM.uptime >>"%DUMP_DIR_TS%\%PID%-%timestamp%-jvm.log"
echo. >>"%DUMP_DIR_TS%\%PID%-%timestamp%-jvm.log"
echo ### Command >>"%DUMP_DIR_TS%\%PID%-%timestamp%-jvm.log"
%PsExec% -s %JAVA_HOME%\bin\jcmd.exe %PID% VM.command_line >>"%DUMP_DIR_TS%\%PID%-%timestamp%-jvm.log"
echo. >>"%DUMP_DIR_TS%\%PID%-%timestamp%-jvm.log"
echo ### Flags >>"%DUMP_DIR_TS%\%PID%-%timestamp%-jvm.log"
%PsExec% -s %JAVA_HOME%\bin\jcmd.exe %PID% VM.flags >>"%DUMP_DIR_TS%\%PID%-%timestamp%-jvm.log"
echo. >>"%DUMP_DIR_TS%\%PID%-%timestamp%-jvm.log"
echo ### Properties >>"%DUMP_DIR_TS%\%PID%-%timestamp%-jvm.log"
%PsExec% -s %JAVA_HOME%\bin\jcmd.exe %PID% VM.system_properties >>"%DUMP_DIR_TS%\%PID%-%timestamp%-jvm.log"
%PsExec% -s %JAVA_HOME%\bin\jcmd.exe %PID% Thread.print -l >"%DUMP_DIR_TS%\%PID%-%timestamp%-threads.log"
%PsExec% -s %JAVA_HOME%\bin\jcmd.exe %PID% GC.heap_dump "%DUMP_DIR_TS%\%PID%-%timestamp%-heap.hprof"
echo Dumped to %DUMP_DIR_TS%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment