Enable CollectSPRequestAllocationCallStacks with PowerShell

I got this message in the ULS log:

An SPRequest object was not disposed before the end of this thread. To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it.  This object will now be disposed.  Allocation Id: {646667A7-73BC-4DDD-B0FB-6EDFC315CCE7}  To determine where this object was allocated, set Microsoft.SharePoint.Administration.SPWebService.ContentService.CollectSPRequestAllocationCallStacks = true. 

The following PowerShell script will do that for me – and you 🙂

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
[System.Reflection.Assembly]::LoadFile($Env:CommonProgramFiles+"Microsoft SharedWeb Server Extensions14ISAPIMicrosoft.SharePoint.dll") | out-null

# Get Content Service of the farm
$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService

# Display and change the setting of property "CollectSPRequestAllocationCallStacks"
write-host "Current: " $contentService.CollectSPRequestAllocationCallStacks 
$contentService.CollectSPRequestAllocationCallStacks = $true

write-host "    New: " $contentService.CollectSPRequestAllocationCallStacks 

4 thoughts on “Enable CollectSPRequestAllocationCallStacks with PowerShell

  1. Awesome! Small suggestion:

    if you replace
    $contentService.CollectSPRequestAllocationCallStacks = $true
    $contentService.CollectSPRequestAllocationCallStacks = -not $contentService.CollectSPRequestAllocationCallStacks
    you can create a toggle script making it a solution that can set and unset as well.

  2. If you enable this and are still not seeing the callstacks does it mean there is nothing to show or is something else preventing the logging. Do you have to have a Verbose level of logging?

Leave a Reply

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