Automatically Generating SQL Server Memory Dumps at a Defined Interval

This post has been republished via RSS; it originally appeared at: SQL Server Support articles.

First published on MSDN on Mar 05, 2018
Over the years of troubleshooting SQL Server we have come to see the need for manually generating a memory dump of the SQL Server process for in-depth analysis. On several occasions, generating multiple memory dumps at a pre-defined interval has been required in order to see if the state of a particular thread or threads change over time. Yes, it is a long and arduous process to analyze multiple memory dumps, but in those rare cases that has been the only way to successfully discovering the root cause of an issue.

This has necessitated to develop some sort of automation in generating multiple memory dumps. Below is the text of a command file (.BAT or .CMD) that accomplishes this task.

Instructions:

    1. Copy the entire contents of the code snippet below and paste into a blank text file

 

    1. Save the file as AutomaticSQLDumpGeneration.bat

 

    1. Modify the service name, the folder name for the SQLDumper.exe, the time interval (in sec), and the number of dumps you want to generate.

 

    1. Double-click on the file to start it.



In the below sample file, setting are as follows

    • Defaults to a named instance called SQL2017 (thus 'MSSQL$SQL2017'). For default instance use 'MSSQLSERVER'

 

    • Folder is set to the SQL2017 tools folder \140 (for SQL 2016 use \130, for SQL 2014 use \120, for SQL 2012 use \110).

 

    • Time interval is 60 seconds (timeout /T 60 )

 

    • Number of dumps is set to 7 (if %counter% GTR 7 goto end)




@echo off

set /A counter=1

@echo.
@echo.
@echo the application we are generating a dump for is MSSQL$SQL2017
@echo.
@echo.
@echo.
@echo searching for MSSQL$SQL2017: tasklist /FO "TABLE" /FI "SERVICES eq MSSQL$SQL2017"

for /F "tokens=2" %%q IN ('tasklist /FO "TABLE" /FI "SERVICES eq MSSQL$SQL2017" /NH') do @set sqlpid=%%q


@echo.
@echo Process PID = %sqlpid%

:start

@echo generating a mini dump with indirect memory
"c:\Program Files\Microsoft SQL Server\140\Shared\Sqldumper.exe" %sqlpid% 0 0x0128 0 %1



timeout /T 60

set /A counter+=1
@echo counter is %counter%

if %counter% GTR 7 goto end

goto start

:end
@echo Done. Collected memory dumps.


Namaste!

Joseph

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.