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 Shared\Web Server Extensions\14\ISAPI\Microsoft.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 

2 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s