Chris’s SharePoint Reflections

Just another WordPress.com weblog




  • Chris Zhong

    IT consultant Australia








Archive for January, 2010

Dispose SPWeb/SPSite in MOSS/WSS

Posted by chrissyz on January 16, 2010

Why we need to dispose them?
In SharePoint, each SPWeb and SPSite object holds a reference to an SPRequest object which holds reference to a SharePoint COM object. In order to ensure that the SPRequest object will release the COM object as well as all the memory allocated by this COM object, it is necessary to dispose the unmanaged resources in a timely fashion. Though internally, .NET framework has a finalizer to release the native memory, however, as it runs on a single thread, it generally cannot keep up with cleaning these objects if they are being leaked many times in a second.
The consequence of not explicitly disposing unmanaged resources in a timely fashion can lead to not having enough memory and quickly consume memory. Furthermore, as each COM object is responsible to communicate with the backend SQL Server, in case the SPWeb object is not disposed when it is no longer used, then the connection to the SQL Server will stay open. Each connection to the database requires a TCP port for the communication. By default, only 5000-1023=3977 ports are available ( MaxUserPort Registry), which means on a single machine per default you cannot have more than 3977 open connections to other applications. So, besides the fact that not disposing SPWeb and SPSite will lead to higher memory consumption which finally can lead to out of the memory exceptions, the machine might also run out of TCP ports.

Symptoms/Issues which users can experience when the unmanaged objects are not disposed correctly
1. Database Connectivity
When object are not disposed correctly, you can find a lot of the following database related error messages in your SharePoint WFE event log and ULS log.
When all the TCP ports are in use, this will result in the following error message/event logs
• Event ID 3355 errors: Cannot connect to SQL Server. XXX not found. Additional error information from SQL Server is included below. [DBNETLIB][ConnectionOpen(Connection())]SQL Server does not exist or access denied.
• Event ID 27745 errors: Unable to connect to the database XXX. Check the database connection and make sure that the database server is running
• The ULS log shows the same database related error as above

2. ULS Logs errors
The ULS log will show errors when objects are not disposed correctly. The database related ULS log errors are already written down in the previous section. This section will focus on the SPDispose related error message in the ULS logs (by default located in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\LOGS). The error message will be like this

1/14/2010 14:15:48.91 w3wp.exe (0x145C) 0x04AC Windows SharePoint Services General 8l1n High 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: {951F9932-6C3F-4FC9-AFA8-824892FA8142} To determine where this object was allocated, create a registry key at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings. Then create a new DWORD named SPRequestStackTrace with the value 1 under this key

Best Practices:
1. Practice the best coding practice regarding dispose the SPWeb/SPSite. Some good resources can be found on the internet.

http://blogs.msdn.com/rogerla/archive/2008/10/04/updated-spsite-rootweb-dispose-guidance.aspx
http://msdn.microsoft.com/en-us/library/aa973248.aspx

http://solutionizing.net/2009/01/09/splist-parentweb-leaks-revisited/
http://furuknap.blogspot.com/2009/07/splistrootfolder-causes-dispose-problem.html
http://msdn.microsoft.com/en-us/library/bb687949.aspx

http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx

2. SPDispose Checker Tool
On MSDN a tool is available which can check your developed assemblies. This tool will check most of the common dispose issues, but will not check all issues that can occur related to disposal of SharePoint objects. So you should fix at least all issues which are found after running the SPDispose Checker and then continue applying the best coding practices

3. Be aware of the out-of –the-box memory hungry component
Be aware of some of the OOTB components can require huge amount of memory when incorrectly configured. Navigation control is a good example. For each item in a navigation control that has to be retrieved through the sharepoint site map provider SPWeb and SPSite will have to be created. Though the control itself ensure that those objects are disposed correctly before the request ends there are often several of these objects in parallel in memory while the control is being rendered.
4. Switching to 64-bit architecture
In 64-bit architecture, the virtual address space is no longer limited to 2GB. This also means that memory fragmentation will not have the negative effects as in 32-bit architecture. However, even in 64-bit architecture, you still have the same limitation regarding TCP ports.

Posted in development, Performance | 1 Comment »