Skip to content

Instantly share code, notes, and snippets.

@steveglachan
Last active August 29, 2015 14:21
Show Gist options
  • Save steveglachan/83578c9b7c46e9b0dc35 to your computer and use it in GitHub Desktop.
Save steveglachan/83578c9b7c46e9b0dc35 to your computer and use it in GitHub Desktop.
Short batch script used to dump and zip/archive the memory heap used by the Adobe ColdFusion (version 9) Jrun (J2EE Application Server) instance.The memory heap dump can then be analysed for memory leaks using VisualVM software.
:: ---
:: Script for JVM Memory Heap Dump
:: - Adobe ColdFusion 9, JRun, JDK 1.6.x, MS Windows
:: Dependencies:
:: - PSEXEC: https://technet.microsoft.com/en-au/sysinternals/bb897553.aspx
:: - JDK: The JDK library versions for JVM and JMAP utility must match (e.g. both 1.6.x)
:: - 7-ZIP: Command-line archiving utility
:: - *** RUN THIS BATCH SCRIPT AS ADMINISTRATOR ***
:: Recommended:
:: - VisualVM (https://visualvm.java.net/) for analysis of the memory heap dump
:: ---
@ECHO OFF
:: Default vars (change path and file name where appropriate):
SET PATHTODUMPDIR=C:\jvm-heap-dumps\
SET FILENAMEPREFIX=heapdump-LOCAL-
SET DUMPFILETYPE=.bin
SET ARCHIVEFILETYPE=.7z
IF NOT EXIST %PATHTODUMPDIR% (MKDIR %PATHTODUMPDIR%)
SET PATHTODUMPFILE=%PATHTODUMPDIR: =%%FILENAMEPREFIX%
SET PATHTODUMPFILE=%PATHTODUMPFILE: =%%DATE:/=-%@%TIME::=-%
SET DUMPFILE=%PATHTODUMPFILE: =%%DUMPFILETYPE%
SET ARCHIVEFILE=%PATHTODUMPFILE: =%%ARCHIVEFILETYPE%
:: Sample output files:
:: C:\jvm-heap-dumps\heapdump-LOCAL-Mon25-05-2015@17-14-48.23.bin (~177 MB)
:: C:\jvm-heap-dumps\heapdump-LOCAL-Mon25-05-2015@17-14-48.23.7z (~21 MB)
:: ---
:: PROCESSING
:: ---
:: Change Dir (optional):
CD C:\
:: Get target PID for JRun.exe and run Memory Dump:
FOR /f "tokens=2" %%a in ('tasklist^|FIND /i "jrun.exe"') do (CALL:doMemoryDumpFunc %%a)
:: Archive and compress the Memory Dump file:
CALL:zipBinaryDump %ARCHIVEFILE% %DUMPFILE%
PAUSE :: End of script
:: ---
:: FUNCTIONS
:: ---
:doMemoryDumpFunc
:: Dump memory to binary file path:
:: (Use PSEXEC: https://technet.microsoft.com/en-au/sysinternals/bb897553.aspx)
psexec -s "C:\Program Files\Java\jdk1.6.0_17\bin\jmap.exe" -dump:file=%DUMPFILE% %~1
:: Notify of completion:
ECHO Memory Dump Complete for Jrun.exe (PID:%~1).
GOTO:EOF
:: END FUNCTION
:zipBinaryDump
:: Compress file using 7Zip
"C:\Program Files\7-Zip\7z.exe" a -t7z %~1 %~2
GOTO:EOF
:: END FUNCTION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment